269

From what I can tell, docker images are installed to /var/lib/docker as they are pulled. Is there a way to change this location, such as to a mounted volume like /mnt?

mgalgs
  • 15,671
  • 11
  • 61
  • 74
Michael Barton
  • 8,868
  • 9
  • 35
  • 43

20 Answers20

190

With recent versions of Docker, you would set the value of the data-root parameter to your custom path, in /etc/docker/daemon.json (according to https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file).

With older versions, you can change Docker's storage base directory (where container and images go) using the -goption when starting the Docker daemon. (check docker --help). You can have this setting applied automatically when Docker starts by adding it to /etc/default/docker

mbarthelemy
  • 12,465
  • 4
  • 41
  • 43
  • Thanks for your answer. Could you tell me the how to add this requirement in the docker upstart file? Is the case that I add the -g flag to the DOCKER_OPTS variable? – Michael Barton Jun 23 '14 at 14:29
  • Sorry for the confusion, that was the file I was referring to. In '/etc/defaults/docker.io' there is a line 'DOCKER_OPTS="-dns 8.8.8.8 -dns 8.8.4.4"'. Is that the option I should use to set the graph directory with the -g flag? – Michael Barton Jun 23 '14 at 15:04
  • 9
    Yes, change this line to `DOCKER_OPTS="-dns 8.8.8.8 -dns 8.8.4.4 -g /mnt"` – mbarthelemy Jun 23 '14 at 17:05
  • 7
    Doesn't work in Fedora. See my answer if your distro is Fedora – Vitor Nov 27 '14 at 11:19
  • 4
    You might need to implement this [solution](http://stackoverflow.com/questions/30127580/docker-opts-in-etc-default-docker-ignored-on-debian8) to a bug in some versions of Debian or Ubuntu if docker ignores your `/etc/default/docker` file. – nedim Jul 03 '15 at 13:58
  • if you only want to change this setting only change to: `DOCKER_OPTS="-g /mnt"` ommit the -dns parameters – HackerBaloo Oct 16 '15 at 05:06
  • Thanks. This worked fine. The problem is I get this error when downloading new images: "failed to register layer: Untar re-exec error: exit status 1: output: link /bin/dnsdomainname /bin/domainname: operation not permitted" Any ideas? – chosenbreed37 May 06 '16 at 18:34
  • Setting this flag in /etc/default/docker is not recommended. There is a comment in the /etc/default/docker file pointing to this [guideline](https://docs.docker.com/v1.11/engine/reference/commandline/daemon/#daemon-configuration-file). – ivandov Feb 07 '17 at 18:23
  • Where is documented the `-g` option? I didn't find on the `dockerd` man page. – Manoel Vilela Oct 09 '17 at 18:10
  • @ManoelVilela That's for old Docker releases - my initial answer was written 3 years ago ;) – mbarthelemy Oct 10 '17 at 04:05
  • It's still working on `17.09.0-ce`, but I didn't find yet any docs about that. – Manoel Vilela Oct 10 '17 at 16:54
  • For further readers: this solution works on CentOS 7, with docker-ce server version 19.3 – Stefano Mozart Jun 19 '20 at 20:15
  • I tried a few different things, but editing the `/etc/docker/daemon.json` config is what ended up working for me. Good instructions here: https://evodify.com/change-docker-storage-location – SlothFriend Nov 03 '21 at 16:25
170

Following advice from comments I utilize Docker systemd documentation to improve this answer. Below procedure doesn't require reboot and is much cleaner.

First create directory and file for custom configuration:

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo $EDITOR /etc/systemd/system/docker.service.d/docker-storage.conf

For docker version before 17.06-ce paste:

[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// --graph="/mnt"

For docker after 17.06-ce paste:

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// --data-root="/mnt"

Alternative method through daemon.json

I recently tried above procedure with 17.09-ce on Fedora 25 and it seem to not work. Instead of that simple modification in /etc/docker/daemon.json do the trick:

{
    "graph": "/mnt",
    "storage-driver": "overlay"
}

Despite the method you have to reload configuration and restart Docker:

sudo systemctl daemon-reload
sudo systemctl restart docker

To confirm that Docker was reconfigured:

docker info|grep "loop file"

In recent version (17.03) different command is required:

docker info|grep "Docker Root Dir"

Output should look like this:

 Data loop file: /mnt/devicemapper/devicemapper/data
 Metadata loop file: /mnt/devicemapper/devicemapper/metadata

Or:

 Docker Root Dir: /mnt

Then you can safely remove old Docker storage:

rm -rf /var/lib/docker
Piotr Król
  • 3,350
  • 2
  • 23
  • 25
  • 9
    It's only method which helped me on Ubuntu 16.04! – Vadim May 05 '16 at 12:04
  • 1
    This link https://docs.docker.com/engine/admin/systemd/ explains how to configure Docker with systemd, which works for newer linux distribution such as Ubuntu 16.04. – Joseph Hui Jun 26 '16 at 10:43
  • 8
    The `docker info` statement is probably outdated. On Ubuntu 16.04, the check for reconfigured is `sudo docker info | grep "Docker Root Dir"`. Otherwise, good answer, should be the accepted one ;) – Guillaume Perrot Aug 04 '16 at 01:34
  • @GuillaumePerrot I just tested on Debian Sid with Docker 1.12.0 and still have `Data loop file` and `Metadata loop file`. Apparently on Ubuntu you have different version. – Piotr Król Aug 15 '16 at 19:14
  • It worked for some time, but from some time I receive an error during starting the docker service: `Error starting daemon: error initializing graphdriver: invalid argument` (see https://github.com/docker/docker/issues/14026). After deleting `/etc/systemd/system/docker.service.d/docker-storage.conf` everything is OK. Probable reason was upgrading the kernel I did in the meantime. – TOUDIdel Sep 26 '16 at 20:14
  • Confirmed working May 26th, 2017 on Xubuntu 16.04 LTS with Docker version 17.03.1-ce, build c6d412e. – Yngvar Kristiansen May 26 '17 at 07:53
  • 1
    On debian, the file to edit seems to be /lib/systemd/system/docker.service (see https://linuxconfig.org/how-to-move-docker-s-default-var-lib-docker-to-another-directory-on-ubuntu-debian-linux). – head7 Jun 16 '17 at 04:28
  • 5
    In new version of Linux (4.10) and Docker (docker-ce 17.06) the line must be changed as follows: /usr/bin/dockerd -H fd:// --data-root="/mnt" – lorenzo-bettini Jun 30 '17 at 14:21
  • @lorenzo-bettini this is change in Docker. Kernel version is not related with that. Anyway thanks for the hint. – Piotr Król Jul 01 '17 at 12:18
  • be careful if you also have a _docker.conf_ in _/etc/systemd/system/docker.service.d/_ – Belun Sep 04 '17 at 11:46
  • 2
    If you see `Docker Root Dir: /"/mnt"` when you runs `docker info|grep "Docker Root Dir"`, you have to replace `--data-root="/mnt"` into `--data-root=/mnt` inside the file `docker-storage.conf` – Ser Sep 12 '17 at 00:45
  • 1
    Note that the new storage location should not be an NTFS partition, see [here](https://github.com/moby/moby/issues/23930), if you do want that use the vfs driver, but see mentioned thread for caveats. – Adversus May 24 '18 at 07:14
  • /etc/docker/daemon.json works for 19.03.12, os Ubuntu 18.04.3 LTS – oudi Dec 03 '20 at 05:14
136

For new docker versions we need to use data-root as graph is deprecated in v17.05.0: official deprecated docs

Edit /etc/docker/daemon.json (if it doesn’t exist, create it) and include:

{
  "data-root": "/new/path/to/docker-data"
}

Then restart Docker with:

sudo systemctl daemon-reload
sudo systemctl restart docker
  • A more detailed step-by-step explanation (including moving data) using Docker Storage with data-root can be found in: Blog post
  • In case of Windows a similar post Windows specific
andreas-supersmart
  • 4,114
  • 2
  • 18
  • 13
  • 2
    your comment helped me find this page: https://adriel.co.nz/blog/2018/01/25/change-docker-data-directory-in-debian-jessie/ thanks! – nasatome Aug 25 '18 at 23:55
  • 18
    I think it's time to give this answer the "accepted" flag, because it's working in the current version. – The Bndr Feb 14 '19 at 09:52
  • I comment here to let people know which version could use this, after https://github.com/docker/docker.github.io/pull/5978 release, It change officail website. – zhongjiajie Mar 27 '19 at 01:17
  • 2
    This is the correct answer for latest docker. I would just like to add, after adding changes to `daemon.json` and restarting docker service, just confirm that the path has been picked up by docker using `docker info|grep "Docker Root Dir"` (as mentioned by @piotr-kr in another answer) – Tuhin Jan 10 '21 at 22:08
  • This worked for me on Fedora 33. I didn't have the file, created it, works well. – Nick Ribal Mar 07 '21 at 05:16
  • Worked for me on Ubuntu 21.04. Thanks! – Vladir Parrado Cruz Sep 21 '21 at 19:42
  • Thanks! Please note the original directory still exists **after** this change. It looks like it can be safely deleted `sudo rm -rf /var/lib/docker`. – igops Feb 28 '23 at 12:08
68

Much easier way to do so:

Stop docker service

sudo systemctl stop docker

Move existing docker directory to new location

sudo mv /var/lib/docker/ /path/to/new/docker/

Create symbolic link

sudo ln -s /path/to/new/docker/ /var/lib/docker

Start docker service

sudo systemctl start docker
Community
  • 1
  • 1
fnjn
  • 1,201
  • 12
  • 9
41

Since I haven't found the correct instructions for doing this in Fedora (EDIT: people pointed in comments that this should also work on CentOS and Suse) (/etc/default/docker isn't used there), I'm adding my answer here:

You have to edit /etc/sysconfig/docker, and add the -g option in the OPTIONS variable. If there's more than one option, make sure you enclose them in "". In my case, that file contained:

OPTIONS=--selinux-enabled

so it would become

OPTIONS="--selinux-enabled -g /mnt"

After a restart (systemctl restart docker) , Docker should use the new directory

Vitor
  • 2,734
  • 29
  • 40
  • On CentOS 6.5/6.6 and probably 7, you use the same /etc/sysconfig/docker file, but you add the arguments to the variable name other_args. – dlaidlaw Jan 08 '15 at 18:11
  • The option is called DOCKER_OPTS now (docker version 1.7.0). – nedim Jul 03 '15 at 13:55
  • 6
    From version 1.8 of Docker onward, using the Docker configuration files in /etc/sysconfig is deprecated. Instead, you should use systemd drop-in configuration files in etc/systemd/system/docker.service.d. After adding or modifying a drop-in file while the docker service is running, run the command systemctl daemon-reload to tell systemd to reload the configuration for the service. https://docs.oracle.com/cd/E52668_01/E54669/html/section_kfy_f2z_fp.html – Pablo Marin-Garcia Nov 16 '15 at 00:34
  • and use drop-in files http://docs.docker.com/engine/articles/systemd/#custom-docker-daemon-options – Pablo Marin-Garcia Nov 16 '15 at 00:40
24

Don't use a symbolic Link to move the docker folder to /mnt (for example). This may cause in trouble with the docker rm command.

Better use the -g Option for docker. On Ubuntu you can set it permanently in /etc/default/docker.io. Enhance or replace the DOCKER_OPTS Line.

Here an example: `DOCKER_OPTS="-g /mnt/somewhere/else/docker/"

suther
  • 12,600
  • 4
  • 62
  • 99
15

This solution works on Red Hat 7.2 & Docker 1.12.0

Edit the file /lib/systemd/system/docker.service in your text editor.

add -g /path/to/docker/ at the end of ExecStart directive. The complete line should look like this.

ExecStart=/usr/bin/dockerd -g /path/to/docker/

Execute the below command

systemctl daemon-reload
systemctl restart docker

Execute the command to check docker directory

docker info | grep "loop file\|Dir"

If you have /etc/sysconfig/docker file in Red Hat or docker 1.7.1 check this answer.

Community
  • 1
  • 1
Sudeepta
  • 2,194
  • 4
  • 20
  • 24
  • my case: centos7, latest docker; i had to call "systemctl status docker" to find the "docker.service" file and added the option "-g" like in the answer. Thanks – Dee Jul 09 '18 at 07:06
13

In CentOS 6.5

service docker stop
mkdir /data/docker  (new directory)
vi /etc/sysconfig/docker

add following line

other_args=" -g /data/docker -p /var/run/docker.pid"

then save the file and start docker again

service docker start

and will make repository file in /data/docker

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
sean kim
  • 131
  • 1
  • 3
13

Copy-and-paste version of the winner answer :)

Create this file with only this content:

$ sudo vi /etc/docker/daemon.json

  {
      "graph": "/my-docker-images"
  }

Tested on Ubuntu 16.04.2 LTS in docker 1.12.6

Topera
  • 12,223
  • 15
  • 67
  • 104
  • 2
    The `graph` attribute in daemon.json is deprecated as of v17.05.0, use `data-root` instead, cf. https://stackoverflow.com/a/50217666/743507 – dschulten Jan 25 '19 at 08:07
6

For Debian/Ubuntu or Fedora, you can probably use the other answers. But if you don't have files under /etc/default/docker or /etc/sysconfig/docker, and your system is running systemd, you may want to follow this answer by h3nrik. I am using Arch, and this works for me.

Basically, you need to configure systemd to read the new docker image location as an environment variable, and pass that environment variable into the Docker daemon execution script.

For completeness, here is h3nrick's answer:


Do you have a /lib/systemd/system/docker.service file?

If so, edit it so that the Docker service uses the usual /etc/default/docker as an environment file: EnvironmentFile=-/etc/default/docker.

In the /etc/default/docker file then add DOCKER_OPTS="-g /home/rseixas/Programs/Docker/images".

At the end just do a systemctl daemon-reload && systemctl restart docker.

For further information please also have a look at the documentation.

Community
  • 1
  • 1
modulitos
  • 14,737
  • 16
  • 67
  • 110
  • If you follow current docker install instructions for Debian, you get SysVinit scripts but they aren't used. See https://github.com/docker/docker/issues/9889#issuecomment-109766580 – Simon Woodside Feb 12 '17 at 04:57
6

The official way of doing this based on this Post-installation steps for Linux guide and what I found while web-crawling is as follows:

  1. Override the docker service conf:

     sudo systemctl edit docker.service
    
  2. Add or modify the following lines, substituting your own values.

     [Service]
     ExecStart=
     ExecStart=/usr/bin/dockerd --graph="/mnt/docker"
    

Save the file. (It creates: /etc/systemd/system/docker.service.d/override.conf)

  1. Reload the systemctl configuration.

     sudo systemctl daemon-reload
    
  2. Restart Docker.

     sudo systemctl restart docker.service
    

After this if you can nuke /var/lib/docker folder if you do not have any images there you care to backup.

Sileria
  • 15,223
  • 4
  • 49
  • 28
5

As recommneded by @mbarthelemy this can be done via the -g option when starting the docker daemon directly.

However, if docker is being started as a system service, it is not recommended to modify the /etc/default/docker file. There is a guideline to this located here.

The correct approach is to create an /etc/docker/daemon.json file on Linux (or Mac) systems or %programdata%\docker\config\daemon.json on Windows. If this file is not being used for anything else, the following fields should suffice:

{
    "graph": "/docker/daemon_files"
}

This is assuming the new location where you want to have docker persist its data is /docker/daemon_files

ivandov
  • 619
  • 8
  • 14
4

A much simpler solution is to create a soft link point to whatever you want, such as

link -s /var/lib/docker /mnt/whatever

It works for me on my CentOS 6.5 server.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
liuyanghejerry
  • 3,771
  • 5
  • 32
  • 38
  • 6
    Don't create a softlink. Reasons see my comment I post before... You may run in trouble with 'docker rm' commands! – suther Mar 30 '15 at 07:59
  • 2
    @suther: May? Now is there a situation you run into trouble or not? May is a bit imprecise for technical documentation, if I run into a problem then I want to learn when exactly that happens, how that problem is caused and how the outcome would be. – hakre Jul 29 '16 at 05:18
4

This blog post helps me

Here are the steps to change the directory even after you’ve created Docker containers etc.

Note, you don’t need to edit docker.service or init.d files, as it will read the change from the .json file mentioned below.

  1. Edit /etc/docker/daemon.json (if it doesn't exist, create it)

  2. Add the following

{
  "data-root": "/new/path/to/docker-data"
}
  1. Stop docker
sudo systemctl stop docker
  1. Check docker has been stopped
ps aux | grep -i docker | grep -v grep
  1. Copy the files to the new location
sudo rsync -axPS /var/lib/docker/ /new/path/to/docker-data
  1. Start Docker back up
sudo systemctl start docker
  1. Check Docker has started up using the new location
docker info | grep 'Docker Root Dir'
  1. Check everything has started up that should be running
docker ps
  1. Leave both copies on the server for a few days to make sure no issues arise, then feel free to delete it.
sudo rm -r /var/lib/docker
mechnicov
  • 12,025
  • 4
  • 33
  • 56
3

I was having docker version 19.03.14. Below link helped me.

Check this Link

in /etc/docker/daemon.json file I added below section:-

 {
    "data-root": "/hdd2/docker",
    "storage-driver": "overlay2"
 }
Sreekanth
  • 1,787
  • 2
  • 13
  • 22
2

On openSUSE Leap 42.1

$cat /etc/sysconfig/docker 
## Path           : System/Management
## Description    : Extra cli switches for docker daemon
## Type           : string
## Default        : ""
## ServiceRestart : docker
#
DOCKER_OPTS="-g /media/data/installed/docker"

Note that DOCKER_OPTS was initially empty and all I did was add in the argument to make docker use my new directory

2

On Fedora 26 and probably many other versions, you may encounter an error after moving your base folder location as described above. This is particularly true if you are moving it to somewhere under /home. This is because SeLinux kicks in and prevents the docker container from running many of its programs from under this location.

The short solution is to remove the --enable-selinux option when you add the -g parameter.

Euan
  • 559
  • 4
  • 10
1

On an AWS Ubuntu 16.04 Server I put the Docker images on a separate EBS, mounted on /home/ubuntu/kaggle/, under the docker dir

This snippet of my initialization script worked correctly

# where are the images initially stored?
sudo docker info | grep "Root Dir"
# ... not where I want them

# modify the configuration files to change to image location
# NOTE this generates an error
# WARNING: Usage of loopback devices is strongly discouraged for production use.
#          Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
# see https://stackoverflow.com/questions/31620825/
#     warning-of-usage-of-loopback-devices-is-strongly-discouraged-for-production-use

sudo sed -i   ' s@#DOCKER_OPTS=.*@DOCKER_OPTS="-g /home/ubuntu/kaggle/docker"@ '  /etc/default/docker

sudo chmod -R ugo+rw /lib/systemd/system/docker.service
sudo cp  /lib/systemd/system/docker.service /etc/systemd/system/
sudo chmod -R ugo+rw /etc/systemd/system/

sudo sed -i ' s@ExecStart.*@ExecStart=/usr/bin/dockerd $DOCKER_OPTS -H fd://@ '  /etc/systemd/system/docker.service
sudo sed -i '/ExecStart/a EnvironmentFile=-/etc/default/docker' /etc/systemd/system/docker.service
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo docker info | grep "Root Dir"
# now they're where I want them
George Fisher
  • 3,046
  • 2
  • 16
  • 15
0

For Mac users in the 17.06.0-ce-mac19 version you can simply move the Disk Image location from the user interface in the preferences option Just change the location of the disk image and it will work (by clicking Move disk Image) and restarting the docker. Using this approach I was able to use my external hardisk for storing docker images.

Pranjal Sahu
  • 1,469
  • 3
  • 18
  • 27
0

For Those looking in 2020. The following is for Windows 10 Machine:

  1. In the global Actions pane of Hyper-V Manager click Hyper-V Settings…
  2. Under Virtual Hard Disks change the location from the default to your desired location.
  3. Under Virtual Machines change the location from the default to your desired location, and click apply.

enter image description here

  1. Click OK to close the Hyper-V Settings page.
Abdullah
  • 479
  • 5
  • 5