Change Docker Root Dir on Red Hat Linux?

12,871

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
12,871

Related videos on Youtube

M. Max
Author by

M. Max

Updated on September 18, 2022

Comments

  • M. Max
    M. Max almost 2 years

    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

    • gaoithe
      gaoithe over 4 years
      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.
  • jsbillings
    jsbillings almost 6 years
    You'll also want to run: semanage fcontext -a -e /var/lib/docker /data/docker
  • CodeMonkey
    CodeMonkey over 3 years
    I needed to use "graph" instead of "data-root", but that may be a difference in version.
  • null
    null over 3 years
    This worked for me on Fedora 33. I didn't have the file, created it, works well.
  • exhuma
    exhuma over 3 years
    @jsbillings could you add an explanation why this is useful?
  • jsbillings
    jsbillings over 3 years
    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.