How do I move a docker container's image to a persistent disk?
Asked Answered
C

4

29

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?

Caldera answered 31/1, 2014 at 17:11 Comment(1)
please, if you find a response useful, just flag it green ;-)Averill
D
30

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.

Dna answered 7/2, 2014 at 16:31 Comment(1)
When you load the image back, does it take space on your drive so you have to move it again?Anachronistic
E
24

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.

Echopraxia answered 1/12, 2016 at 20:38 Comment(0)
S
17

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.

Slapbang answered 31/1, 2014 at 19:34 Comment(0)
E
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.

Eucalyptus answered 22/7, 2015 at 8:44 Comment(3)
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.Lilt
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.Lilt
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.Lilt

© 2022 - 2024 — McMap. All rights reserved.