Docker Import and Export
docker container export
This command is used to Export a container's filesystem as a tar archive that can be shared across. To export the container, move the exported file to the target machine, and import that container.
Syntax
docker container export [OPTIONS] CONTAINER
Options
--output, -o: Write to a file, instead of STDOUT
Example First, you need to create a container. You'll be exporting them by name, so you need to know the names of those containers.
learning-ocean:~ gaurav$ docker container run -it ubuntu:14.04 bash
root@013573bd0adb:/# apt-get update
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
Get:2 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:3 https://esm.ubuntu.com trusty-infra-security InRelease
Get:4 https://esm.ubuntu.com trusty-infra-updates InRelease
Fetched 13.9 MB in 32s (428 kB/s)
Reading package lists... Done
root@013573bd0adb:/#
We will now install tree and git in the container-
root@013573bd0adb:/# apt-get install tree git -y
Come out from container using [CTRL+pq]
Now list the containers-
root@013573bd0adb:/#
learning-ocean:~ gaurav$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
013573bd0adb ubuntu:14.04 "bash" 2 minutes ago Up 2 minutes nifty_hoover
learning-ocean:~ gaurav$
Now, export the container-
learning-ocean:~ gaurav$ docker container export 01 -o myubuntu_free_git.tar
It can also be achieved using the below command-
learning-ocean:~ gaurav$ docker container export 01 > myubuntu_free_git1.tar
You can we have made two .tar files-
learning-ocean:test gaurav$ ls
myubuntu_free_git.tar myubuntu_free_git1.tar
Importing a container
Like export, we can import the container with a single command. You must have the exported file to the target server.
Syntax
docker image file|URL|- [REPOSITORY[:TAG]]
Options
--output , -o: Write to a file, instead of STDOUT
Example Lets list the images first-
learning-ocean:~ gaurav$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 13b66b487594 3 months ago 197MB
learning-ocean:~ gaurav$
Importing the container image-
learning-ocean:~ gaurav$ docker image import myubuntu_free_git.tar myubuntu_free_git
sha256:aee602b47ab0aedc1da86543c14642a191701fa1b1e996a34a24400b9ef784a8
learning-ocean:~ gaurav$
Listing the images again-
learning-ocean:~ gaurav$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu_free_git latest aee602b47ab0 About a minute ago 215MB
ubuntu 14.04 13b66b487594 3 months ago 197MB
learning-ocean:~ gaurav$
As you can see we have the imported images as well. Create a container out of the image and check if it has tree and git-
learning-ocean:~ gaurav$ docker container run -it myubuntu_free_git bash
root@b20fee9f36bf:/# cd /tmp/
root@b20fee9f36bf:/# tree --version
tree v1.6.0 (c) 1996 - 2011 by Steve Baker, Thomas Moore, Francesc Rocher, Kyosuke Tokoro
root@b20fee9f36bf:/tmp# git --version
git version 1.9.1
root@b20fee9f36bf:/tmp#
This way you can import and export containers.