Change data directory of Docker
In more recent Docker versions on Ubuntu you will edit /etc/default/daemon.json
like so:
{
"data-root": "/new/location"
}
I believe that in this guide you'll have a good explanation.
You can change Docker's storage base directory (where container and images go) using the -g option when starting the Docker daemon.
Ubuntu/Debian: edit your
/etc/default/docker
file with the-g
option:DOCKER_OPTS="-dns 8.8.8.8 -dns 8.8.4.4 -g /mnt"
Fedora/Centos: edit
/etc/sysconfig/docker
, and add the-g
option in the other_args variable: ex.other_args="-g /var/lib/testdir"
. If there's more than one option, make sure you enclose them in " ". After a restart, (service docker restart) Docker should use the new directory.Using a symlink is another method to change image storage.
Caution - These steps depend on your current /var/lib/docker being an actual directory (not a symlink to another location).
1) Stop docker:
service docker stop
. Verify no docker process is running ps faux2) Double check docker really isn't running. Take a look at the current docker directory: ls
/var/lib/docker/
2b) Make a backup -
tar -zcC /var/lib docker > /mnt/pd0/var_lib_docker-backup-$(date +%s).tar.gz
3) Move the
/var/lib/docker
directory to your new partition:mv /var/lib/docker /mnt/pd0/docker
4) Make a symlink:
ln -s /mnt/pd0/docker /var/lib/docker
5) Take a peek at the directory structure to make sure it looks like it did before the mv:
ls /var/lib/docker/
(note the trailing slash to6) Start docker back up
service docker start
7) restart your containersresolve the symlink)
To change the data
directory in docker
it needs to be run with the option -g /my/data
. In my /etc/default/docker
I set:
DOCKER_OPTS="-g /srv/docker"
See also my notes here.
To change the data
directory in lxc
I put in /etc/lxc/lxc.conf
:
lxc.lxcpath = /srv/lxc
Upgrade to Docker 1.13.0.
From the Release Notes:
- New
The storage location of the Linux volume can now be moved
To expand on @MattK's answer:
The Docker documentation on controlling the Docker daemon suggests that platform independent way to do this is:
edit the /etc/docker/daemon.json file to contain the line
{
"data-root": "/mnt/docker-data",
(...)
}
where /mnt/docker-data
is the directory where you want the docker images and containers to live.
Then
sudo systemctl restart docker
You can check whether it worked by running
docker info
and look for the contents of the line that start with Docker Root Dir:
.
See also https://stackoverflow.com/a/50217666/2209313 and https://unix.stackexchange.com/q/452368/36043.