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
?
With recent versions of Docker, you would set the value of the graph
parameter to your custom path, in /etc/docker/daemon.json
(according to https://docs.docker.com/v1.11/engine/reference/commandline/daemon/#daemon-configuration-file).
With older versions, you can change Docker's storage base directory (where container and images go) using the -g
option 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
-g
option to /etc/defaults/docker instead of modifying the Upstart file, see my answer - mbarthelemy 2014-06-23 14:37
DOCKER_OPTS="-dns 8.8.8.8 -dns 8.8.4.4 -g /mnt"
mbarthelemy 2014-06-23 17:05
/etc/default/docker
file - nedim 2015-07-03 13:58
DOCKER_OPTS="-g /mnt"
ommit the -dns parameter - HackerBaloo 2015-10-16 05:06
"failed to register layer: Untar re-exec error: exit status 1: output: link /bin/dnsdomainname /bin/domainname: operation not permitted"
Any ideas - chosenbreed37 2016-05-06 18:34
-g
option? I didn't find on the dockerd
man page - Manoel Vilela 2017-10-09 18:10
17.09.0-ce
, but I didn't find yet any docs about that - Manoel Vilela 2017-10-10 16:54
graph
attribute in daemon.json is deprecated as of v17.05.0, use data-root
instead, cf. https://stackoverflow.com/a/50217666/74350 - dschulten 2019-01-25 08:04
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"
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
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 2016-08-04 01:34
Data loop file
and Metadata loop file
. Apparently on Ubuntu you have different version - Piotr Król 2016-08-15 19:14
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 2016-09-26 20:14
/usr/bin/dockerd -H fd:// --data-root="/mnt - lorenzo-bettini 2017-06-30 14:21
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 2017-09-12 00:45
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
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/E5266801/E54669/html/sectionkfyf2zfp.htm - Pablo Marin-Garcia 2015-11-16 00:34
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/"
docker rm
when using a symlink - bjhend 2016-02-08 16:07
Ubuntu 16.04
psychok7 2016-05-06 10:38
Ubuntu 16.04
http://stackoverflow.com/a/30219552/977622 to get it to wor - psychok7 2016-05-06 10:55
For new docker versions we need to use data-root
as from the official deprecated docs (Deprecated In Release: v17.05.0)
{
"data-root": "/new/path/to/docker-data"
}
thanks - nasatome 2018-08-25 23:55
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
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.
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
graph
attribute in daemon.json is deprecated as of v17.05.0, use data-root
instead, cf. https://stackoverflow.com/a/50217666/74350 - dschulten 2019-01-25 08:07
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.
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.
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
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
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
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
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.
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.