How to move Docker containers between different hosts?

161,989

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:

  1. Export the container to a tarball

    docker export <CONTAINER ID> > /home/export.tar
    
  2. Move your tarball to new machine

  3. 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:

  1. Save the docker image into archive:
    docker save image_name > image_name.tar
  2. copy on another machine
  3. 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.

Share:
161,989
Dinesh Reddy
Author by

Dinesh Reddy

An explorer

Updated on March 22, 2021

Comments

  • Dinesh Reddy
    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
    stmllr about 8 years
    Also does not preserve data that is stored inside volumes.
  • valentt
    valentt almost 8 years
    How is this supposed to work? After the import I get new image, and then what? Just do a new run command?
  • valentt
    valentt almost 8 years
    This 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
    valentt almost 8 years
    @larsks Wouldn't first step be to stop container, and then do the commit?
  • aholt
    aholt almost 8 years
    This 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
    crollywood over 7 years
    @valentt Both is possible, to commit running and stopped container
  • asvignesh
    asvignesh over 7 years
    cluster hq shutdown... and BTW to migrate container the container should run on ZFS / any supported storage lun
  • Rintoul
    Rintoul almost 6 years
    Hugely helpful. The "No command specified" message was driving me crazy.
  • JasonPlutext
    JasonPlutext over 5 years
    Didn'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
    Ricardo Branco over 5 years
    @JasonPlutext Maybe something related to SELinux? Do you have SELinux enabled?
  • Hua Zhang
    Hua Zhang about 5 years
    The "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
    Paul Kruger almost 5 years
    This answer doesn't really explain exactly the commands you need to use, which makes it hard for a noob like me
  • dGRAMOP
    dGRAMOP about 4 years
    docker-checkpoint could let you move a "running" container between hosts, if they both support CRIU.
  • Lau Real
    Lau Real about 4 years
    1. stop the container docker stop x; 2. commit ur changes docker commit -p x x; 3. save the container to image docker save -o x x; 4. move the x file to the new host and in the new host load the new image dokcer 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 with docker run (-v is required to mount these files if needed)
  • hjahan
    hjahan almost 4 years
    Got this: tar: Removing leading `/' from member names
  • Ricardo Branco
    Ricardo Branco almost 4 years
    @hjahan That's a typical tar message. Not an error and not an even a warning.
  • Wenzel
    Wenzel almost 3 years
    same here, you just saved my container and my time !
  • Dave
    Dave over 2 years
    This 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 or docker create if using docker-compose. You can just use docker-compose up with the same config from the original system, then continue with the instructions.
  • Jan
    Jan over 2 years
    It should be noted that before doing the docker save and docker load, one should commit the container to the image sudo docker commit <container_id> image_name