Maintaining adequate disk space is a crucial task we perform in our outsourced server management services. Lack of free space can often lead to server crashes.
In Docker machines, ‘/var/lib/docker’ is the directory where the Docker images and the container data are stored by default, when Docker is installed:
But when the /var partition gets filled up as more and more containers are created, the system may soon run out of space. That’s when we need to change the default directory for Docker images.
Today we’ll discuss how to change the default installation directory for Docker.
Setup and manage your Docker system
How to change Docker default directory
To change Docker’s images directory ‘/var/lib/docker’ to another partition with more space, the option ‘-g’ is used with the Docker daemon.
For instance, in our Docker machine, the default directory was ‘/var/lib/docker/aufs’ as seen:
Here’s how we changed it to another directory:
1. Since our /var partition was running out of space, we created a new partition ‘/mnt/docker’.
2. We then edited the Docker configuration file ‘/etc/default/docker’ and gave the start-up option -g to specify this new directory.
3. We then restarted the Docker service
root default # service docker restart docker stop/waiting docker start/running, process 20081
4. After the restart, Docker started using the new directory ‘/mnt/docker’ as seen here:
From now onwards, the newly created Docker containers and images would be stored in the new directory ‘/mnt/docker’.
[ Are your spending too much time managing your Docker containers? Our Docker experts take care of your infrastructure and ensure its smooth functioning. ]
How to change the existing Docker image directory
In some cases, it may be required to move the already existing data also from the old directory to the new one.
To move the old images and containers, we first moved the directory contents and then created a symbolic link with these steps:
1) Stop the docker service
service docker stop
2) Backup the existing directory ‘/var/lib/docker’.
3) Move the old directory to the newly created partition ‘/mnt/docker’
mv /var/lib/docker /mnt/docker
4) Create a symlink between the two directories:
ln -s /mnt/docker /var/lib/docker
5) Start the docker service
service docker start
6) Confirm that the containers are working fine.
[ Running a Docker infrastructure doesn’t have to be hard, or costly. Get world class Docker management services at affordable pricing. ]
In short..
Today we discussed how to change the default directory of Docker. The directory changes should be done with special caution to avoid any mess up.
Other points to be considered are: proper mounting of the new partition, assigning adequate permissions and ownership to the folders, usage of Docker containers, etc.
At Bobcares, we have 24/7 custom monitoring scripts to check the disk space usage. Whenever disk usage goes above 75%, our engineers take prompt measures to ensure enough disk space.
Our team also set custom scripts to rotate log files and delete unwanted files to prevent disk usage from over-shooting. Backups are always taken as a precaution.
We also give recommendations to server owners on how to manage their server resources effectively. If you’d like to know how to get the best out of your servers, we’d be happy to talk to you.
0 Comments