8

I have a little problem. I have a brand new Red Hat Linux Server and I installed Docker CE for CentOS/Red Hat with the official repositories for Docker CE. Now I see that docker creates the container under /var/lib/docker, but my problem is that I use an extra partition for my data under /data/docker. How can I change the default root directory for Docker in CentOS/Red Hat?

I tried a few HOWTOs but I get the same problem. I can’t find the configuration. For example, I search for the following files:

  • /etc/default/docker (I think only for Debian/Ubuntu)
  • /etc/systemd/system/docker.service.d/override.conf (I can't find on my system)
  • /etc/docker/daemon.json (I can't find on my system)

If I get the docker info I see:

Docker Root Dir: /var/lib/docker

1
  • you can create /etc/systemd/system/docker.service.d/override.conf and put something like 'ExecStart=/usr/bin/dockerd --graph /docker --default-ulimit nofile=1024:4096 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock' in it in a '[Service]' block. The ExecStart set in /usr/lib/systemd/system/docker.service is over-ridden. This works Centos 7.3,7.7, Docker version 18.09.6, jan 2020.
    – gaoithe
    Jan 20, 2020 at 17:32

3 Answers 3

9

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:.

2
  • I needed to use "graph" instead of "data-root", but that may be a difference in version.
    – CodeMonkey
    Sep 28, 2020 at 9:18
  • This worked for me on Fedora 33. I didn't have the file, created it, works well.
    – Nick Ribal
    Mar 7, 2021 at 5:09
8

Stop all running docker containers and then docker daemon. Move "/var/lib/docker" directory to the place where you want to have this data. For you it would be:

mv /var/lib/docker /data/

and then create symlink for this docker directory in /var/lib path:

ln -s /data/docker /var/lib/docker

Start docker daemon and containers.

3
  • 1
    You'll also want to run: semanage fcontext -a -e /var/lib/docker /data/docker
    – jsbillings
    Jun 28, 2018 at 23:36
  • @jsbillings could you add an explanation why this is useful?
    – exhuma
    Mar 8, 2021 at 16:22
  • semanage fcontext -a -e .... ..... sets up an equivalency class between the original location and the new one, so the files automatically get the right context.
    – jsbillings
    Mar 8, 2021 at 21:40
1

i use fedora 27 you can look docker deamon help

dockerd --help 
--data-root string                        Root directory of persistent Docker state (default "/var/lib/docker")

stop docker service

systemctl stop docker

move docker default folder. i moved to under /data folder.

mv /var/lib/docker /data/.

open service file

vi /usr/lib/systemd/system/docker.service

change ExecStart variable's parameter.

vi /usr/lib/systemd/system/docker.service

this mine. also i added debug parameter.

ExecStart=/usr/bin/dockerd -D --data-root /data/docker

need deamon reload

systemctl daemon-reload

you can start service

systemctl start docker

if you can test on command line before service start

 dockerd -D --data-root /data/docker

it is done.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .