How to move Docker containers between different hosts?
Solution 1
You cannot move a running docker container from one host to another.
You can commit the changes in your container to an image with docker commit
, move the image onto a new host, and then start a new container with docker run
. This will preserve any data that your application has created inside the container.
Nb: It does not preserve data that is stored inside volumes; you need to move data volumes manually to new host.
Solution 2
Alternatively, if you do not wish to push to a repository:
-
Export the container to a tarball
docker export <CONTAINER ID> > /home/export.tar
Move your tarball to new machine
-
Import it back
cat /home/export.tar | docker import - some-name:latest
Solution 3
What eventually worked for me, after lot's of confusing manuals and confusing tutorials, since Docker is obviously at time of my writing at peek of inflated expectations, is:
- Save the docker image into archive:
docker save image_name > image_name.tar
- copy on another machine
- on that other docker machine, run docker load in a following way:
cat image_name.tar | docker load
Export and import, as proposed in another answers does not export ports and variables, which might be required for your container to run. And you might end up with stuff like "No command specified" etc... When you try to load it on another machine.
So, difference between save and export is that save command saves whole image with history and metadata, while export command exports only files structure (without history or metadata).
Needless to say is that, if you already have those ports taken on the docker hyper-visor you are doing import, by some other docker container, you will end-up in conflict, and you will have to reconfigure exposed ports.
Note: In order to move data with docker, you might be having persistent storage somewhere, which should also be moved alongside with containers.
Solution 4
Use this script: https://github.com/ricardobranco777/docker-volumes.sh
This does preserve data in volumes.
Example usage:
# Stop the container
docker stop $CONTAINER
# Create a new image
docker commit $CONTAINER $CONTAINER
# Save image
docker save -o $CONTAINER.tar $CONTAINER
# Save the volumes (use ".tar.gz" if you want compression)
docker-volumes.sh $CONTAINER save $CONTAINER-volumes.tar
# Copy image and volumes to another host
scp $CONTAINER.tar $CONTAINER-volumes.tar $USER@$HOST:
# On the other host:
docker load -i $CONTAINER.tar
docker create --name $CONTAINER [<PREVIOUS CONTAINER OPTIONS>] $CONTAINER
# Load the volumes
docker-volumes.sh $CONTAINER load $CONTAINER-volumes.tar
# Start container
docker start $CONTAINER
Solution 5
From Docker documentation:
docker export
does not export the contents of volumes associated with the container. If a volume is mounted on top of an existing directory in the container,docker export
will export the contents of the underlying directory, not the contents of the volume. Refer to Backup, restore, or migrate data volumes in the user guide for examples on exporting data in a volume.
Comments
-
Dinesh Reddy over 3 years
I cannot find a way of moving docker running containers from one host to another.
Is there any way I can push my containers to repositories like we do for images ? Currently, I am not using data volumes to store the data associated with applications running inside containers. So some data resides inside containers, which I want to persist before redesigning the setup.
-
stmllr about 8 yearsAlso does not preserve data that is stored inside volumes.
-
valentt almost 8 yearsHow is this supposed to work? After the import I get new image, and then what? Just do a new run command?
-
valentt almost 8 yearsThis is actually a really bad suggestion, especially for containers running database. I tried this suggestion and it didn't work. Could it maybe work with stopping container first?
-
valentt almost 8 years@larsks Wouldn't first step be to stop container, and then do the commit?
-
aholt almost 8 yearsThis suggestion was only really meant for an alternative. It might work for your situation, it might not. For me, I was setting up database replication docker containers at the time, and for the export/import, did not care about preserving the data, as I was running backups of the database data regularly out to a different tarball. For that, this worked perfectly.
-
crollywood over 7 years@valentt Both is possible, to commit running and stopped container
-
asvignesh over 7 yearscluster hq shutdown... and BTW to migrate container the container should run on ZFS / any supported storage lun
-
Rintoul almost 6 yearsHugely helpful. The "No command specified" message was driving me crazy.
-
JasonPlutext over 5 yearsDidn't work for me on AWS Linux (Centos). In the endi I took the low tech approach of using docker inspect to find the volume dir, then manually copying that over.
-
Ricardo Branco over 5 years@JasonPlutext Maybe something related to SELinux? Do you have SELinux enabled?
-
Hua Zhang about 5 yearsThe "No command specified" message was driving me crazy too. I use docker commit <container-id> stackstorm-local:2.9, and docker pull stackstorm-local:2.9 from another host.
-
Paul Kruger almost 5 yearsThis answer doesn't really explain exactly the commands you need to use, which makes it hard for a noob like me
-
dGRAMOP about 4 yearsdocker-checkpoint could let you move a "running" container between hosts, if they both support CRIU.
-
Lau Real about 4 years1. stop the container
docker stop x
; 2. commit ur changesdocker commit -p x x
; 3. save the container to imagedocker save -o x x
; 4. move the x file to the new host and in the new host load the new imagedokcer load -i x
(if u started the container with-v
option, u'll have to move these files to new host too); 5. run this image withdocker run (-v is required to mount these files if needed)
-
hjahan almost 4 yearsGot this: tar: Removing leading `/' from member names
-
Ricardo Branco almost 4 years@hjahan That's a typical tar message. Not an error and not an even a warning.
-
Wenzel almost 3 yearssame here, you just saved my container and my time !
-
Dave over 2 yearsThis worked very well, however I would recommend renaming the $CONTAINER to different names for the example. This is because the $CONTAINER name may not be the same as the actual CONTAINER image name. Also as a side note, You dont need to use
docker start
ordocker create
if using docker-compose. You can just usedocker-compose up
with the same config from the original system, then continue with the instructions. -
Jan over 2 yearsIt should be noted that before doing the
docker save
anddocker load
, one should commit the container to the imagesudo docker commit <container_id> image_name