fmdaboville
fmdaboville

Reputation: 1831

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 :

Upvotes: 22

Views: 39797

Answers (5)

ZaxLofful
ZaxLofful

Reputation: 1147

This part of the Docker Daemon is configurable. Best practices would have you change the data folder; this can be done with OS-level Linux commands like a symlink... I would say it's better to actually configure the Docker Daemon to store the data elsewhere!

You can do that by editing the Docker command line (e.g. the systemd script that starts the Docker daemon), or change /etc/docker/daemon.json.

The file should have this content:

{
  "data-root": "/path/to/your/docker"
}

If you add a new hard drive, partition, or mount point you can add it here and docker will store its data there.

Upvotes: 23

Robert Mikes
Robert Mikes

Reputation: 1431

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

Upvotes: 22

sg qy
sg qy

Reputation: 366

Do a bind mount.

For example, moving /docker/volumes to /mnt/large.

Append line into /etc/fstab.

/mnt/large /docker/volumes none bind 0 0

And then.

mv /docker/volumes/* /mnt/large/
mount /docker/volumes

Do not forget chown and chmod of /mnt/large first, if you are using non-root docker.

Upvotes: 4

Qohelet
Qohelet

Reputation: 1616

I landed here as I had the very same issue. Even though some sources suggest you could do it with a symbolic link this will cause all kinds of issues.

Depending on the OS and Docker version I had malformed images, weird errors or the docker-daemon refused to start.

Here is a solution, but it seems it varies a little from version to version. For me the solution was:

Open

/lib/systemd/system/docker.service

And change this line

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

to:

ExecStart=/usr/bin/dockerd -g /mnt/WHATEVERYOUR/PARTITIONIS/docker --containerd=/run/containerd/containerd.sock

Upvotes: 6

ravenskater
ravenskater

Reputation: 794

I solved it creating a symbolic link to a partition with bigger size:

ln -s /scratch/docker_meta /var/lib/docker

/scratch/docker_meta is the folder that I have in a bigger partition.

Upvotes: 3

Related Questions