149

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.

Sled
  • 18,541
  • 27
  • 119
  • 168
Dinesh Reddy
  • 1,594
  • 2
  • 11
  • 9

8 Answers8

128

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
    

You can also move a container as follows:

  1. Stop the container on the source host.

    docker stop x
    

    If you started the container with -v, you'll have to copy the mounted files and directories to the new host.

  2. Commit your changes

    docker commit -p x x
    
  3. Save the container to image

    docker save -o x x
    
  4. Copy the x file to the new, target host.

  5. Connect to the target host.

  6. Load the new image

    docker load -i x
    
  7. Run the new image

    docker run 
    

    Note that -v may be required to reference mounted files and directories on the target host, to mirror the source host.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
aholt
  • 2,829
  • 2
  • 10
  • 13
  • 19
    Also does not preserve data that is stored inside volumes. – stmllr Jun 09 '16 at 14:53
  • 2
    How is this supposed to work? After the import I get new image, and then what? Just do a new run command? – valentt Sep 03 '16 at 12:27
  • 4
    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 Sep 06 '16 at 01:28
  • 2
    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. – aholt Sep 06 '16 at 13:19
94

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.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • docker-checkpoint could let you move a "running" container between hosts, if they both support CRIU. – dGRAMOP May 31 '20 at 18:38
  • @DaveJarvis if you want to make that extensive of an edit I would suggest simply posting a new answer. – larsks Apr 06 '23 at 22:36
  • I moved Lau's helpful comment into a more visible location. I thought the idea of a wiki was that anyone could edit an answer to improve it? – Dave Jarvis Apr 06 '23 at 22:42
  • 1
    This isn't a wiki, and when you make major changes to someone else's answer you're putting words into their mouth. Editing a question to make minor correction is just fine, but if you need to add substantial new information it's generally better to just post a new answer (and consider adding a comment on the accepted answer pointing at the additional information). – larsks Apr 07 '23 at 02:38
64

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.

ealfonso
  • 6,622
  • 5
  • 39
  • 67
Aleksandar Pavić
  • 3,143
  • 1
  • 34
  • 36
  • 1
    Hugely helpful. The "No command specified" message was driving me crazy. – Rintoul Jul 25 '18 at 04:54
  • The "No command specified" message was driving me crazy too. I use docker commit stackstorm-local:2.9, and docker pull stackstorm-local:2.9 from another host. – Hua Zhang Apr 24 '19 at 18:02
  • same here, you just saved my container and my time ! – Wenzel Aug 14 '21 at 01:40
  • 2
    It should be noted that before doing the `docker save` and `docker load`, one should commit the container to the image `sudo docker commit image_name` – Jan Jan 07 '22 at 21:30
33

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
Sled
  • 18,541
  • 27
  • 119
  • 168
Ricardo Branco
  • 5,740
  • 1
  • 21
  • 31
  • 2
    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. – JasonPlutext Feb 12 '19 at 23:55
  • @JasonPlutext Maybe something related to SELinux? Do you have SELinux enabled? – Ricardo Branco Mar 22 '19 at 16:21
  • Got this: tar: Removing leading `/' from member names – hjahan Sep 05 '20 at 17:24
  • @hjahan That's a typical tar message. Not an error and not an even a warning. – Ricardo Branco Sep 11 '20 at 12:42
  • 1
    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. – Dave Nov 29 '21 at 15:39
  • Is this works on Raspberrypi os? – mikegrep Feb 27 '23 at 20:06
20

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.

stephenwade
  • 1,057
  • 2
  • 20
  • 37
  • cluster hq shutdown... and BTW to migrate container the container should run on ZFS / any supported storage lun – asvignesh Jan 11 '17 at 16:01
3

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.

-1

docker export | gzip > .tar.gz

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

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

thistleknot
  • 1,098
  • 16
  • 38
-1

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.