Docker containers can take up quite a bit of disk space, especially if used in a development environment. We create containers for Tomcat and they are upwards of 600MB. If you are developing and pushing code multiple times a day space gets chewed up fast. Our dev servers are in the cloud, and by default they don’t have tons of disk space. So using Docker with big images meant lots of regular maintenance on disk space.
I alleviated the problem by moving Docker’s directory structure to an attached drive instead of on the operating system drive. We are running Docker on Red Hat Enterprise Linux 7. In AWS I provisioned a larger drive and attached it to my RHEL instance, linking it to the /data
directory. I want my Docker files in /data/docker
. My first inclination was to make a symbolic link and move Docker, but I have found reports of this going awry with Docker. So reading some StackOverflow posts got me to this approach using /etc/sysconfig
. Here are the steps for RHEL (it may be different for other Linux distributions):
- Create a file at
/etc/sysconfig/docker
. Add this line to the file: DOCKER_OPTS=”-g /data/docker” - Edit
/lib/systemd/system/docker.service
- add
EnvironmentFile=-/etc/sysconfig/docker
under[Service]
- Add $DOCKER_OPTS to
ExecStart=/usr/bin/docker daemon $DOCKER_OPTS -H fd://
- Save the file
- add
- Reload the daemon configuration:
sudo systemctl daemon-reload
- Restart Docker:
sudo systemctl restart docker
After you restart Docker, check the /data/docker
directory, it should have lots of content now. Any images you pull and create will now be stored there.
Update
If you installed Docker via yum, this configuration change is lost when you perform a yum update where Docker gets updated.