Resizing Docker containers

2019-06-24 14:18发布

I am trying to see if size of each docker container can be limited through some configuration.

Following this , I am able to increasing the size of a running container from 10G. Can I shrink it? Trying to shrink threw an error saying online shrinking not supported.

Ideally, I am looking for options to configure and set default container size which containers will inherit when getting created. Docker doc on devicemapper suggested --storage-opt while starting docker daemon. But it does not seem to work.

I would also like to know if size can be limited per container (say during docker run command).

Any pointer towards the correct direction is greatly appreciated. Thanks in advance

2条回答
冷血范
2楼-- · 2019-06-24 14:27

it works with devicemapper settins.

--storage-driver devicemapper --storage-opt dm.basesize=5G
查看更多
时光不老,我们不散
3楼-- · 2019-06-24 14:43

If you're using the devicemapper graph engine, then storage-opt dm.basesize is the way to go for what you are trying to do. You need to clean out your /var/lib/docker after changing the configuration (note this will wipe all images you currently have, even if you use direct block devices to provision the data and metadata pool devices).

Here are the steps I followed:

$ docker run -ti base/arch df -h /
Filesysem                                    Size  Used Avail Use% Mounted on
/dev/mapper/docker-254:7-73733-b8dc...6d28a  9.8G  335M  8.9G   4% /

So I currently have the default 10G base size.

First, change the configuration to add "--storage-opt dm.basesize=5G":

# vim /etc/systemd/system/docker.service
...
ExecStart=/usr/bin/docker -d -H fd:// -s devicemapper \
    --storage-opt dm.datadev=/dev/vg01/docker-dm-data \
    --storage-opt dm.metadatadev=/dev/vg01/docker-dm-meta \
    --storage-opt dm.basesize=5G

Then, stop Docker (make sure all containers are stopped beforehand, reload config, cleanout /var/lib/docker and restart Docker:

# systemctl stop docker
# systemctl daemon-reload
# rm -rf /var/lib/docker
# systemctl start docker

"docker images" should confirm no images are present.

And now rerun the test:

$ docker run -ti base/arch df -h /
Unable to find image 'base/arch:latest' locally
Pulling repository base/arch
a64697d71089: Download complete 
511136ea3c5a: Download complete 
4bbfef585917: Download complete 
Status: Downloaded newer image for base/arch:latest
Filesystem                                 Size  Used Avail Use% Mounted on
/dev/mapper/docker-254:7-66094...f635ac65  4.8G  322M  4.3G   7% /

And there we have a 5Gb base size.

查看更多
登录 后发表回答