限制Ubuntu-SSH镜像容器的磁盘大小

huangapple go评论91阅读模式
英文:

Limit Ubuntu-SSH Image Container's Disk Size

问题

  1. 我尝试过在compose文件中添加`storage_opt: size: 100G`,但在容器中运行df -H时,它返回了主机磁盘的可用大小和磁盘使用情况(在此示例中为已用400GB,可用1.9TB)。
  2. 我是否需要在主机上创建一个磁盘,并将其映射到我的容器的/dev/sdx磁盘?或者是否有一种更好的方法,容器可以访问主机磁盘作为完整的磁盘,但只能使用其中的一部分。
英文:

How can I limit the storage use of a Ubuntu 20.04 Image Container with SSH Enabled in Docker?

I understand that one should not use Docker as a VM Technology but this is only for educational purposes

docker-compose.yml:

  1. version: '3'
  2. services:
  3. ubuntu-vm:
  4. build: .
  5. image: ubuntu-ssh
  6. ports:
  7. - "2222:22"
  8. environment:
  9. - ROOT_PASSWORD=password
  10. restart: always
  11. networks:
  12. mynetwork:
  13. ipv4_address: 172.24.0.2
  14. networks:
  15. mynetwork:
  16. driver: bridge
  17. ipam:
  18. config:
  19. - subnet: 172.24.0.0/16

Dockerfile:

  1. FROM ubuntu:latest
  2. RUN apt-get update && \
  3. apt-get install -y openssh-server && \
  4. mkdir /var/run/sshd
  5. RUN echo 'root:password' | chpasswd
  6. RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
  7. EXPOSE 22
  8. CMD [ "/usr/sbin/sshd", "-D", "-e" ]

I have tried adding storage_opt: size: 100G to the compose file, but upon doing df -H in the container, it returned the host disk available size and disk usage (in this example 400gb used, 1.9tb available)

Will I have to create a disk in my host and map it my container's /dev/sdx disk? Or is there a better way to do it where the container does have access to the host disk as full but a chunk of it.

答案1

得分: 1

我已经尝试将 storage_opt: size: 100G 添加到compose文件中,但在容器中运行 df -H 时,返回的是主机磁盘的可用空间和磁盘使用情况(在这个示例中,已使用 400GB,可用 1.9TB)。

当然,因为 df -h 将显示 设备 的大小,而不是特定文件系统上的可用配额限制。创建一个大于 100G 的文件来检查。

  1. $ docker run -ti --rm --storage-opt size=5G alpine
  2. # dd if=/dev/random of=file bs=10M count=1023
  3. dd: error writing 'file': Quota exceeded
  4. 512+0 records in
  5. 511+0 records out

这很可能是Docker存储驱动程序特定的问题 - 我在btrfs上进行了测试。

英文:

> I have tried adding storage_opt: size: 100G to the compose file, but upon doing df -H in the container, it returned the host disk available size and disk usage (in this example 400gb used, 1.9tb available)

Sure, because df -h will show the size of the device, not available quota limits on specific filesystem. Create a file bigger than 100G to check.

  1. $ docker run -ti --rm --storage-opt size=5G alpine
  2. # dd if=/dev/random of=file bs=10M count=1023
  3. dd: error writing 'file': Quota exceeded
  4. 512+0 records in
  5. 511+0 records out

This is most probably docker storage driver specific - I tested on btrfs.

huangapple
  • 本文由 发表于 2023年6月19日 15:12:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定