27

We have noticed that our containers are taking up a lot of space, one of the reasons for this is the images.

We would like to move the images.

I know right now they are stored in /var/lib/docker/graph/<id>/layer

Is there a way to move these to another location/persistent disk?

user3666197
  • 1
  • 6
  • 50
  • 92
dvaini
  • 283
  • 1
  • 3
  • 5

4 Answers4

29

To move images to another drive or another server:

docker save image_name > image_name.tar

mv image_name.tar /somewhere/else/

Load it back into docker

docker load < image_name.tar 

Reference.

Pablo Bianchi
  • 1,824
  • 1
  • 26
  • 30
Ken A.
  • 291
  • 2
  • 3
21

Here's any easy way to move docker's data:

sudo service docker stop
sudo mv /var/lib/docker /a/new/location
sudo ln -s /a/new/location /var/lib/docker # Create a symbolic link
sudo service docker start

No need to change DOCKER_OPTS or use -g /path.

jcoffland
  • 5,238
  • 38
  • 43
16

You can always mount /var/lib/docker to a different disk. Otherwise, you can start the daemon with -g /path in order to tell docker to use a different directory for storage.

creack
  • 116,210
  • 12
  • 97
  • 73
5

Using the answer by @creack I did the following on my Ubuntu install to move my entire docker images/containers folder to a new location/disk. The great thing about doing this is that any new images that I install will then use the new disk location.

First stop the docker service:

sudo service docker stop

Then move the docker folder from the default location to your target location:

sudo mv /var/lib/docker /thenewlocation

Then edit the /etc/default/docker file, inserting/amending the following line which provides the new location as an argument for the docker service:

DOCKER_OPTS="-g /thenewlocation/docker"

Restart the docker service:

sudo service docker start

This worked 100% for me - all my images remained in tact.

Community
  • 1
  • 1
ctrlplusb
  • 12,847
  • 6
  • 55
  • 57
  • Doesn't seem to work: I added the location as in answer, but whenever I restart docker, it creates `/var/lib/docker` anew. And when I execute `ps aux | grep docker`, I see the process doesn't have the `-g /foo` arg that was added in config file. – Hi-Angel Aug 31 '21 at 06:50
  • So, the `-g` option I confirm that works. Inserting `-g /foo` into the `docker.service` file works for me. That's not a good option though as update will likely overwrite that file. – Hi-Angel Aug 31 '21 at 07:06
  • Alright, I found: you have to create a file `/etc/docker/daemon.json` with this content `{ "data-root": "/my/path" }`, then restart docker. Although you won't see any changes in `ps aux`, but docker does use the `/my/path` this way, and the older `/var/lib/docker` will not be re-created if you deleted it beforehand. – Hi-Angel Aug 31 '21 at 07:36