Share Docker Images and Container

If you want to test dockerized applications, you need to export/import Docker images or Docker containers. Therefor Docker offers various options and commands. In this tutorial i show some possibilities without Docker Hub or Docker registry.

Preconditions

Docker save and load

# show Docker images
docker@apimock:~$ docker images

# save Docker image (include parent layers, tags, versions etc.)
docker@apimock:~$ docker save api_image:latest | gzip -vc > api_image.tar.gz

# download archive from apimock
MBP:~ lupin$ scp docker@<192.168.99.100>:/home/docker/api_image.tar.gz ~/Downloads/

# create new Boot2Docker VM
MBP:~ lupin$ docker-machine create -d virtualbox apimock2

# upload archive to apimock2
MBP:~ lupin$ scp ~/Downloads/api_image.tar.gz docker@<192.168.99.101>:/home/docker/

# load archive into image
docker@apimock2:~$ cat api_image.tar.gz | docker load

# show Docker images
docker@apimock2:~$ docker images

# show Docker image history
docker@apimock2:~$ docker history api_image

# create and start Docker container
docker@apimock2:~$ docker run --name api_container -d -p 8888:8888 api_image

Docker export and import

# show Docker containers
docker@apimock:~$ docker ps -a

# export Docker container (could running)
docker@apimock:~$ docker export api_container | gzip -vc > api_container.tar.gz

# download archive from apimock
MBP:~ lupin$ scp docker@<192.168.99.100>:/home/docker/api_container.tar.gz ~/Downloads/

# upload archive to apimock2
MBP:~ lupin$ scp ~/Downloads/api_container.tar.gz docker@<192.168.99.101>:/home/docker/

# stop and delete previous container
docker@apimock2:~$ docker kill api_container && docker rm api_container

# delete previous images
docker@apimock2:~$ docker rmi api_image

# import from archive
docker@apimock2:~$ cat api_container.tar.gz | docker import - api_image/import

# show Docker images
docker@apimock2:~$ docker images

# show Docker image history
docker@apimock2:~$ docker history api_image/import

# create and start Docker container (CMD needed)
docker@apimock2:~$ docker run --name api_container -d -p 8888:8888 api_image/import mock-server --address=0.0.0.0 --dir=api

Attention: Please note each output forĀ “$ docker history…” !!!!