Dinesh Reddy
Dinesh Reddy

Reputation: 1752

How to move Docker containers between different hosts?

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.

Upvotes: 165

Views: 280138

Answers (8)

aholt
aholt

Reputation: 2981

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
    

Upvotes: 137

Aleksandar Pavić
Aleksandar Pavić

Reputation: 3430

What eventually worked for me, after lot's of confusing manuals and confusing tutorials, since Docker is obviously at time of my writing at peak 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.

Update 10/2023
Another very good way to migrate docker containers You can actually follow this upgrade script, but modified for moving instead of upgrading. You will also need docker compose on target machine.

  1. Use autocompose.py from https://github.com/Red5d/docker-autocompose/ you can use it as docker container also
    cd ~
    mkdir ~/dockercontainers
    sudo docker container list -a > ~/dockercontainers/containers.txt
    cat  ~/dockercontainers/containers.txt
    
    CIDS=$(docker container list -a | sed '1d' | awk '{print $1}')
    for c in $CIDS; do mkdir ~/dockercontainers/$c ; docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/red5d/docker-autocompose $c > ~/dockercontainers/$c/docker-compose.yml ; done 
  1. Inspect composer files and move volume folders with data on new machine

  2. Export Images and tags as txt files Images: mkdir dockersave && docker images > dockersave/images.txt tags: cat dockersave/images.txt | sed '1d' | awk '{ print "docker tag "$3" "$1":"$2 }'>> tags.txt

  3. Perform actual Images export (you can commit them before this step if required) IDS=$(docker images | sed '1d' | awk '{print $3}') for c in $IDS; do docker save -o dockersave/$c.tar $c; done

  4. Copy everything from dockersave and dockercontainers to target machine.

  5. Load images onto new docker: cd dockersave/ IDS=$(ls *.tar) for c in $IDS; do docker load -i $c; done

  6. Restore containers CIDS=$(cat ~/dockercontainers/containers.txt | sed '1d' | awk '{print $1}'); for c in $CIDS; do cd ~/dockercontainers/$c ; docker-compose up -d --no-deps --build ; done

If everything is fine, you will get bunch of OK messages and docker ps will display all of your containers.

Upvotes: 71

reza mosavian
reza mosavian

Reputation: 29

if you are using docker desktop you can use this extension

or you can see this link and describe How does it work behind the scenes? and do it by your self.

Upvotes: -1

larsks
larsks

Reputation: 312370

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.

Upvotes: 97

thistleknot
thistleknot

Reputation: 1158

docker export | gzip > .tar.gz

#new host gunzip < /mnt/usb/.tar.gz | docker import -

docker run -i -p 80:80 /bin/bash

Upvotes: -1

Antonio De Marinis
Antonio De Marinis

Reputation: 209

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.

Upvotes: 20

Houssam Ezzoukh
Houssam Ezzoukh

Reputation: 61

I tried many solutions for this, and this is the one that worked for me :

1.commit/save container to new image :

  1. ++ commit the container:
    # docker stop
    # docker commit CONTAINER_NAME
    # docker save --output IMAGE_NAME.tar IMAGE_NAME:TAG


ps:"Our container CONTAINER_NAME has a mounted volume at '/var/home'" ( you have to inspect your container to specify its volume path : # docker inspect CONTAINER_NAME )

  1. ++ save its volume : we will use an ubuntu image to do the thing.
    # mkdir backup
    # docker run --rm --volumes-from CONTAINER_NAME -v ${pwd}/backup:/backup ubuntu bash -c “cd /var/home && tar cvf /backup/volume_backup.tar .”

Now when you look at ${pwd}/backup , you will find our volume under tar format.
Until now, we have our conatainer's image 'IMAGE_NAME.tar' and its volume 'volume_backup.tar'.

Now you can , recreate the same old container on a new host.

Upvotes: 3

Ricardo Branco
Ricardo Branco

Reputation: 6079

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

Upvotes: 34

Related Questions