Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move docker volume to different partition

I have a server where I run some containers with volumes. All my volumes are in /var/lib/docker/volumes/ because docker is managing it. I use docker-compose to start my containers.

Recently, I tried to stop one of my container but it was impossible :

$ docker-compose down
[17849] INTERNAL ERROR: cannot create temporary directory!

So, I checked how the data is mounted on the server :

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            7,8G     0  7,8G   0% /dev
tmpfs           1,6G  1,9M  1,6G   1% /run
/dev/md3         20G   19G     0 100% /
tmpfs           7,9G     0  7,9G   0% /dev/shm
tmpfs           5,0M     0  5,0M   0% /run/lock
tmpfs           7,9G     0  7,9G   0% /sys/fs/cgroup
/dev/md2        487M  147M  311M  33% /boot
/dev/md4        1,8T  1,7G  1,7T   1% /home
tmpfs           1,6G     0  1,6G   0% /run/user/1000

As you can see, the / is only 20Go, so it is full and I can't stop my containers using docker-compose.

My questions are :

  • There is a simple solution to increase the available space in the /, using /dev/md4 ?

  • Or can I move volumes to another place without losing data ?

like image 637
fmdaboville Avatar asked Sep 11 '25 12:09

fmdaboville


1 Answers

To move an existing docker data folder, do the following:

  1. Stop the docker daemon:
service docker stop
  1. Create/Edit the /etc/docker/daemon.json configuration file with location of the new data directory:
{
    "data-root": "/new/path"
}
  1. Copy docker files to new location:
rsync -aP /var/lib/docker/ /new/path
  1. Remove old directory (rename it to be safe)
mv /var/lib/docker /var/lib/docker.old
  1. Create symlink:
ln -s /new/path /var/lib/docker

Note: If I don't create the symlink, I get this error:

Error response from daemon: error evaluating symlinks from mount source "/var/lib/docker/...": lstat /var/lib/docker: no such file or directory

Here it says that the error can be fixed by changing the path in the /new/path/containers/*/config.v2.json files, but that didn't work for me, the original /var/lib/docker path reappeared in those files.

  1. Start daemon
service docker start
  1. Test
docker run --rm hello-world

Should see something like this:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
  1. Cleanup
rm -rf /var/lib/docker.old
like image 124
Robert Mikes Avatar answered Sep 13 '25 02:09

Robert Mikes