How do I change the Docker image installation directory?

Those on 16.04 see comment by @thaJeztah https://github.com/docker/docker/issues/3127

It’s also possible to use a daemon.json configuration file instead of /etc/default/docker. The /etc/default/docker is only used for systems running upstart, and not for systems running systemd, so is more portable. Also it allows reloading some configuration settings without restarting the daemon;

https://docs.docker.com/engine/reference/commandline/daemon/#daemon-configuration-file

E.g

I’m on Ubuntu 16.04.1, but it shouldn’t matter because this is a cross-distro solution.

Just put this json into /etc/docker/daemon.json:

{
“graph”: “/path/to/docker”
}
Worked for me and I didn’t have to mess with upstart or systemd.

7 Likes

I want to do this centos7, could anyone please suggest me how to achieve this? I am using centos 7 and latest docker version 1.13. I have mounted a storage drive /dev/sdb and i want the docker to use this drive instead of /var/lib/docker.

Thanks, that’s worked for me, on Fedora 25 :slight_smile:

Thanks!

Works for docker raspberry pi 3, debian jessie running docker version 17.03.0-ce

Thank you so much! That’s worked perfectly!!!

:relieved:

Hi,

Modifying the docker.service.d might be a problem when updating the packages.
for Centos, you should modify:
/etc/sysconfig/docker
OPTIONS=’–selinux-enabled --log-driver=journald --signature-verification=false -g /new/path/to/docker

Best

That works, thanks!!!

For anyone browsing this page in the future looking for a shell script to move/change the docker root directory on Ubuntu (tested with 16.04), you can download it directly move-docker-root.sh or visit my site https://sysinfo.io/ubuntu-16-04-move-docker-root/.

move-docker-root.sh

#!/bin/bash
#sysinfo.io
#change paths below to suit your needs
dockerdir_new='/docker/var/'
dockerdir_old='/var/lib/docker/'

set -e
dockerdir_new2=$(echo "$dockerdir_new" | sed 's/\//\\\//g')
systemctl stop docker
mkdir -p $dockerdir_new
rsync -a $dockerdir_old* $dockerdir_new
sed -i.bak "/ExecStart=\/usr\/bin\/dockerd \-H fd:\/\//c ExecStart=\/usr\/bin\/dockerd \-H fd:\/\/ -g ${dockerdir_new2}" /lib/systemd/system/docker.service
mv $dockerdir_old /var/lib/docker.bak/
ln -s $dockerdir_new /var/lib/docker
systemctl daemon-reload
systemctl start docker.service
printf '\n* The Docker systemd config has been backed up to /lib/systemd/system/docker.service.bak\n* A backup of the old Docker Root has been copied to /var/lib/docker.bak/\n\n'
docker info | grep 'Root Dir'

I tried. Worked !!! Thanks :slight_smile:

You’re welcome. Took me all day dealing with sed’s delimiter escapes. I’m sure there is a more proficient scripter out there who may want to improve on my script, but hey, it works great.

Worked perfectly thanks !!! :smiley:

For newer versions you would need to have a look here: https://stackoverflow.com/a/34731550/164374

Here is the content of “etc/docker/daemon.json”

{
  "graph": "/mnt/docker"
}
1 Like

Mine is rhel . The solution worked for me.
I restarted my aws instance then I face the below error when I start docker service.

Job for docker.service failed because the control process exited with error code. See “systemctl status docker.service” and “journalctl -xe” for details.

Have created a separate topic. appreciate your help . https://forums.docker.com/t/docker-engine-is-not-starting/40698

I’ve used “data-root” instead, I think because the docker version… tks!

Thanks! This works perfectly on ubuntu 16.04.3.

Has someone find this error after moving the Docker folder?

[root@dgdalecfg201 docker]# docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/: x509: certificate signed by unknown authority.
See 'docker run --help'.

The problem was that the admin from this servers changed the domain or something like that, what I did to fix this was run this command: sudo update-ca-trust This was the fix on CentOS.

Great, but now it looks like it is better to use “data-root”.
The docker daemon tolled me:

WARN[0000] The “graph” config file option is deprecated. Please use “data-root” instead.

Thanks, it’s poorly documented.

Thanks a lot! This was a life saver.

It works like a charm. thanks!

Thanks Amos worked for me, Fedora 27