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

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

Limit Ubuntu-SSH Image Container's Disk Size

问题

我尝试过在compose文件中添加`storage_opt: size: 100G`,但在容器中运行df -H时,它返回了主机磁盘的可用大小和磁盘使用情况(在此示例中为已用400GB,可用1.9TB)。

我是否需要在主机上创建一个磁盘,并将其映射到我的容器的/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:

version: '3'

services:
  ubuntu-vm:
    build: .
    image: ubuntu-ssh
    ports:
      - "2222:22"
    environment:
      - ROOT_PASSWORD=password
    restart: always
    networks:
      mynetwork:
        ipv4_address: 172.24.0.2

networks:
  mynetwork:
    driver: bridge
    ipam:
      config:
        - subnet: 172.24.0.0/16

Dockerfile:

FROM ubuntu:latest

RUN apt-get update && \
    apt-get install -y openssh-server && \
    mkdir /var/run/sshd

RUN echo 'root:password' | chpasswd

RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

EXPOSE 22

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 的文件来检查。

$ docker run -ti --rm --storage-opt size=5G alpine
# dd if=/dev/random of=file bs=10M count=1023
dd: error writing 'file': Quota exceeded
512+0 records in
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.

$ docker run -ti --rm --storage-opt size=5G alpine
# dd if=/dev/random of=file bs=10M count=1023
dd: error writing 'file': Quota exceeded
512+0 records in
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:

确定