14

How can I change the data directory of Docker where docker save the container (or where lxc save the container)? I have in my server a ssd and a hard drive ant I want that the container will be save on the hard drive. Thanks

ssd_rider
  • 310
  • 2
  • 4
  • 15
  • An easy and simple solution here that worked for me - https://www.guguweb.com/2019/02/07/how-to-move-docker-data-directory-to-another-location-on-ubuntu/ – Sadidul Islam May 21 '23 at 04:45

6 Answers6

13

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 faux

2) 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 to

6) Start docker back up service docker start

7) restart your containersresolve the symlink)

Conrado Fonseca
  • 231
  • 2
  • 6
  • what is the reason for 4)? Is it needed? In my case, I would like to have docker files to be on a mounted RAID, and not on the boot partition. – Roman Mik Feb 27 '17 at 16:34
12

In more recent Docker versions on Ubuntu you will edit /etc/default/daemon.json like so:

{
  "data-root": "/new/location"
}
Pablo Bianchi
  • 14,308
  • 4
  • 74
  • 117
MattK
  • 277
  • 2
  • 4
5

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 (create it if it doesn't already exist) 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.

Note that https://stackoverflow.com/a/68061807/2209313 suggests that if you installed Docker via Snap, you should find your daemon.json file in /var/snap/docker/current/config/daemon.json.

Wandering Logic
  • 251
  • 1
  • 3
  • 7
  • ```$ ls /etc/docker/daemon.json ls: cannot access '/etc/docker/daemon.json': No such file or directory``` – Luís de Sousa Jun 16 '22 at 06:58
  • @LuísdeSousa perhaps some of the suggestions in https://stackoverflow.com/questions/43689271/wheres-dockers-daemon-json-missing would help you find or create your daemon.json file if it is not in the default location specified by the Docker documentation I pointed to – Wandering Logic Jun 18 '22 at 17:45
2

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

Stuart Cardall
  • 329
  • 2
  • 7
0

After having upvoted the given answers by Stuart and Conrado, I realized it didn't work, and my system partition went full again.

I actually had to edit the systemd unit file, like described in the following blog article: https://linuxconfig.org/how-to-move-docker-s-default-var-lib-docker-to-another-directory-on-ubuntu-debian-linux.

All together, here are the steps :

  1. sudo systemctl stop docker docker.socket
  2. sudo nano /lib/systemd/system/docker.service
  3. Change the line ExecStart=/usr/bin/dockerd -H fd:// into ExecStart=/usr/bin/dockerd -g /new/path/docker -H fd:// and save the file
  4. sudo mkdir -p /new/path/docker
  5. sudo rsync -aqxP /var/lib/docker/ /new/path/docker
  6. sudo systemctl daemon-reload
  7. sudo systemctl start docker

Now, my system partition remain slim, the /var/lib/docker folder is not re-created.


Update following Dan's comment

As Dan stated, updating the Docker packages overrides my update and makes Docker using the default path again.

Following his advice, here is how to properly override the default storage path :

  1. sudo systemctl edit docker
  2. The above command creates an empty file where you need to copy/past the following :
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -g /new/path/docker -H fd:// --containerd=/run/containerd/containerd.sock
  1. systemctl status docker.service will ensure all is fine with the override of the Docker configuration
  2. sudo systemctl restart docker

Now Docker is using the new place, and you can update the Docker packages without loosing your configuration.

Note : The reason for the empty ExecStart= is to clear the value from the original file, and the second one fills it again.

ZedTuX
  • 643
  • 6
  • 9
  • 1
    The [docker documentation](https://docs.docker.com/config/daemon/systemd/#custom-docker-daemon-options) says that it should work when updating the `daemon.json` file. Have you restarted docker after changing the config? Otherwise, I wouldn't recommend editing Docker's main `systemd` file, instead, you should use `sudo systemctl edit docker` and override the lines that you want to be overridden. Otherwise, you may lose your changes during an upgrade. – Dan Mar 25 '22 at 14:31
  • Yep, I did restart the docker service, but I have updated the `/etc/default/docker` file instead since with Debian like distribution it should be done here, like it is said in the Conrado's answer. Anyway, thank you for sharing the `sudo systemctl edit docker` command ! – ZedTuX Mar 25 '22 at 14:39
0

Upgrade to Docker 1.13.0.

From the Release Notes:

- New
The storage location of the Linux volume can now be moved
Snowcrash
  • 334
  • 1
  • 4
  • 16