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:
- 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.
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.
- 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
Inspect composer files and move volume folders with data on new machine
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
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
Copy everything from dockersave and dockercontainers to target machine.
Load images onto new docker:
cd dockersave/ IDS=$(ls *.tar) for c in $IDS; do docker load -i $c; done
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.
docker save
is for saving images, not containers. docs.docker.com/engine/reference/commandline/save – Resumption