Docker Image Storage - LVM Thinpool


Where should I store my Docker images?

In RHEl 7 or CentOS 7, the default Docker configuration uses devicemapper with loopback. This works perfect for testing or in a development environment...it's a bad idea for production.

Note: RHEL/CentOS Atomic Hosts are pre-configured for dedicated pools.

Steps

Edit the /etc/sysconfig/docker-storage-setup.

# vi /etc/sysconfig/docker-storage-setup

Add the following parameters to the /etc/sysconfig/docker-storage-setup file.

#STORAGE_DRIVER
#DEVS=/dev/sdd
VG=dockervg
GROWPART=enable
AUTO_EXTEND_POOL=enable
MIN_DATA_SIZE=8G
POOL_AUTOEXTEND_THRESHOLD=60
POOL_AUTOEXTEND_PERCENT=10

Stop Docker.

# systemctl stop docker

Remove any existing docker storage configuration file.

# rm -f /etc/sysconfig/docker-storage

Remove existing Docker location.

 # rm -f /var/lib/docker

Create a physical volume--in this example, /dev/sdd (and it is 20GB). Use the pvs and/or the fdisk -l command to verify you are working with the correct device/disk.

# pvcreate /dev/sdd

Create a volume group (i.e. dockervg). Volume group name must be the same name you specified in the /etc/sysconfig/docker-storage-setup file under the VG parameter.

# vgcreate dockervg /dev/sdd

Run the docker-storage-setup configuration tool.

# /bin/docker-storage-setup

Run the lvs command to check things out.

# lvs
LV          VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync
docker-pool dockervg twi-a-t---  8.00g             0.00   0.16   

Now we have a 8GB LVM thinpool for our Docker images. Wait...I thought the disk was 20GB.

Let's extend it. Run the vgdisplay command to check how much space is left in the volume group. In my case...3059 Extents are free

# vgdisplay dockervg
  --- Volume group ---
  ...
  Free  PE / Size       3059 / 12.00 GB

Now extend.

lvextend -l +3059 /dev/dockervg/docker-pool

Run the lvs command again.

# lvs
LV          VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync
docker-pool dockervg twi-a-t--- 19.95g             0.16   0.24 

Much better.

Crank Docker back up.

# systemctl start docker

Finally, verify.

# docker info
...
Storage Driver: devicemapper
Pool Name: dockervg-docker--pool
Pool Blocksize: 524.3 kB
Base Device Size: 10.74 GB
Backing Filesystem: xfs
Data file: 
Metadata file: 
Data Space Used: 34.6 MB
Data Space Total: 21.42 GB
Data Space Available: 21.39 GB
Metadata Space Used: 61.44 kB
Metadata Space Total: 25.17 MB
Metadata Space Available: 25.1 MB
Thin Pool Minimum Free Space: 2.142 GB
Udev Sync Supported: true
Deferred Removal Enabled: true
Deferred Deletion Enabled: false
Deferred Deleted Device Count: 0
Library Version: 1.02.135-RHEL7 (2016-11-16)

Naturally, you will want to keep your LVM thinpool "lean and clean". Check out How to delete unused Docker images for more details.

How to delete unused Docker images


To prevent your file system from filling up, one must purge old, unused Docker images.

There are many great references available on the web on how to accomplish this. This is what I have found to be most effective.

The goal is to query all running containers to determine whether an image is currently in use before attempting to delete (or remove) it.

Naturally, as a first step, you should remove all exited containers.

# docker rm -v $(docker ps -a -q -f status=exited)

You may also wish to remove created containers that are not running.

# docker rm -v $(docker ps -a -q -f status=created)

Then you can proceed with the Docker image cleanup.

Command

# for i in `docker images | tail -n +2 | awk '{print $1":"$2}'`; do if [[ `docker ps` != *$i* ]]; then docker rmi $i; fi; done

If you wish to leave specific images (say with the word "httpd" in the image name) untouched, then do the following by adding the grep command with the invert-match (-v) parameter.

# for i in `docker images | tail -n +2 | grep -i -v httpd | awk '{print $1":"$2}'`; do if [[ `docker ps` != *$i* ]]; then docker rmi $i; fi; done

Similarly, if you wish to retain multiple images with different words in the image name (i.e. httpd and nginx), do the following:

# for i in `docker images | tail -n +2 | grep -i -v 'httpd\|nginx' | awk '{print $1":"$2}'`; do if [[ `docker ps` != *$i* ]]; then docker rmi $i; fi; done