How to export existing docker images & Containers

How to copy Docker images from one host to another without using a repository




In an ideal scenario, transferring docker images is done through the Docker Registry or though a fully-managed provider such as AWS’s ECR or Google’s GCR. You can easily upload an image through the docker push command, and others can pull the image using the docker pull command.
Although, if you need to move an image from one host to another to test the image before sending it to the production environment, or you want to share the image with someone in the office, then it can be achieved by exporting the image as a .tar file.
Docker supports three different types of methods for saving the container images to a single tarball.
  1. docker machine scp
  2. docker save - Save is used to persist an image (not a container)
  3. docker export - Export is used to persist a container (not an image)

Using docker machine scp Command

docker machine scp

Copy files from your local host to a machine, from machine to machine, or from a machine to your local host using scp.
The notation is machinename:/path/to/files for the arguments; in the host machine’s case, you don’t need to specify the name, just the path.
Since Docker Machine v0.3.0, scp was introduced to copy files from one Docker machine to another. This is very convenient if you want copying a file from your local computer to a remote Docker machine such as AWS EC2 or Digital Ocean because Docker Machine is taking care of SSH credentials for you.
  1. Save you images using docker save like:
    docker save -o docker-images.tar app-web
    
  2. Copy images using docker-machine scp
    docker-machine scp ./docker-images.tar remote-machine:/home/ubuntu
    
Assume your remote Docker machine is remote-machine and the directory you want the tar file to be is /home/ubuntu.
  1. Load the Docker image
    docker-machine ssh remote-machine sudo docker load -i docker-images.tar

Using Docker Save Command:

Saving Docker Image:
First, we will stick to the plan, that is saving the image only. Now, let's walk through the docker save command. Assume that you need a Python image with Alpine, which can be pulled from Docker Hub:
$ docker pull python:2.7.17-alpine3.9
2.7.17-alpine3.9: Pulling from library/python
e7c96db7181b: Already exists
1819f4b92bc2: Already exists
8061b3761cb3: Pull complete
73aebae115de: Pull complete
Digest: sha256:5f6059d78f530c3c59c4842e104ddcfc772a27fb8fac0d900f4d77bcb4621d9b
Status: Downloaded newer image for python:2.7.17-alpine3.9
docker.io/library/python:2.7.17-alpine3.9
After adding a few files or making changes in the container, you decide to create a tarball of the image to provide it to your colleague. You can achieve this by running the below-mentioned command:
$ docker save python:2.7.17-alpine3.9 > /path/to/save/my-python-container.tar
Just make sure that you use the exact image name and the tag during tar creation. In our case, it was python:2.7.17-alpine3.9. You can verify if the above command worked:
$ du -h my-python-container.tar 
75M my-python-container.tar
Now, you can send the .tar file to another person via rsync, scp or a similar file transfer protocol as per your preference.
Loading Docker Image:
Once the target machine has the .tar file, you can load the image into the local registry using command docker load :
$ docker load < my-python-container.tar
image

Now, cross-check if you have that image on the target machine by using docker images or docker image list. The end result will be something like below :
$ docker image list
REPOSITORY   TAG               IMAGE ID       CREATED              SIZE
python       2.7.17-alpine3.9  3f0e580ded94   2 hours ago          74.9MB

Using Docker Export Command:

Exporting Docker Container:
Note: The docker export command will not export the content of the volume, which is attached to the container. In this case, you need to run an additional command to backup, restore or migrate the existing volume. You can read more about this here.
Looking at the docker export method, first we will pull an Alpine image:
$ docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
e6b0cf9c0882: Pull complete
Digest: sha256:2171658620155679240babee0a7714f6509fae66898db422ad803b951257db78
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

Now, you can run the instance in detach mode so that the container doesn’t get destroyed when we exit it.
$ docker run -it --detach --name alpine-t alpine
To get the container ID and name which we created, we can use the docker ps command. Just in case, if in your machine the container has/was stopped for some reason, you can still get the ID and name by using docker ps -a:
$ docker ps
CONTAINER ID  IMAGE  COMMAND   CREATED         STATUS        PORTS    NAMES
35f34fabfa84  alpine "/bin/sh" 14 seconds ago  8 seconds ago           alpine-t
As we can see, our container id is 35f34fabfa84 (it will be different for you), or you can use the container name as well; in our case, it is alpine-t. Now, we can run the docker export command to export the instance’s image:
$ docker export 35f34fabfa84 > alpine-t.tar
Alternatively, you can also use OPTIONS to do the same, and your .tar file will be ready for transfer.
$ docker export --output="alpine-t.tar" 35f34fabfa84
Importing Docker Container:
Now, you can import the .tar file to the target machine by using docker import:
$ sudo tar -c alpine-t.tar | docker import - alpine-t
To verify, you can run the container using --rm (it will destroy the container once you execute it):
$ docker run --rm -it --name alpine-test alpine-t:[TAG]
So if you were having problems with downloading docker image.you can try the above commands to manually transfer the image &
Now you can see that machine will have the required image already.
image

[root@localhost ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
openshift/origin-deployer   v1.5.1              0f23b83cad80        3 years ago         617MB
openshift/origin            v1.5.1              a23dc456ea10        3 years ago         617MB
openshift/origin-pod        v1.5.1              aad02d5e14b9        3 years ago         1.14MB
[root@localhost ~]# docker pull docker.io/openshift/origin-deployer:v3.11.0
v3.11.0: Pulling from openshift/origin-deployer
524b0c1e57f8: Pull complete
4f9a298c1818: Pull complete
bca46dece3d7: Pull complete
83bb630d4186: Pull complete
Digest: sha256:e8d2d126f11beba94a43e0fd24d76bc6a9a06eff31e3c8a086686bae0e5c2046
Status: Downloaded newer image for openshift/origin-deployer:v3.11.0
docker.io/openshift/origin-deployer:v3.11.0
[root@localhost ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
openshift/origin-deployer   v3.11.0             6b7e858d3bce        4 days ago          384MB
openshift/origin-deployer   v1.5.1              0f23b83cad80        3 years ago         617MB
openshift/origin            v1.5.1              a23dc456ea10        3 years ago         617MB
openshift/origin-pod        v1.5.1              aad02d5e14b9        3 years ago         1.14MB
[root@localhost ~]# docker save 6b7e858d3bce | gzip > new.tar.gz
[root@localhost ~]# ls -lhtr new.tar.gz
-rw-r--r--. 1 root root 115M Jun 27 11:39 new.tar.gz
[root@localhost ~]# scp new.tar.gz user@RemoteSystem:/tmp/
 docker load -i /tmp/new.tar.gz
edf3aa290fb3: Loading layer [==================================================>] 211.1 MB/211.1 MB
1264301a9bdb: Loading layer [==================================================>] 33.62 MB/33.62 MB
d73f6b3b4b43: Loading layer [==================================================>] 3.072 kB/3.072 kB
89704a0da48d: Loading layer [==================================================>] 147.9 MB/147.9 MB
Loaded image ID: sha256:6b7e858d3bcedfcf6d4f3dfa8ceda9d01c0c8ec41889710958efffbda2157039

docker save

Description

Save one or more images to a tar archive (streamed to STDOUT by default)

Usage

docker save [OPTIONS] IMAGE [IMAGE...]

Extended description

Produces a tarred repository to the standard output stream. Contains all parent layers, and all tags + versions, or specified repo:tag, for each argument provided.
For example uses of this command, refer to the examples section below.

Options

Name, shorthand Default Description
--output , -o Write to a file, instead of STDOUT

Examples

Create a backup that can then be used with docker load.

$ docker save busybox > busybox.tar

$ ls -sh busybox.tar

2.7M busybox.tar

$ docker save --output busybox.tar busybox

$ ls -sh busybox.tar

2.7M busybox.tar

$ docker save -o fedora-all.tar fedora

$ docker save -o fedora-latest.tar fedora:latest

Save an image to a tar.gz file using gzip

You can use gzip to save the image file and make the backup smaller.
docker save myimage:latest | gzip > myimage_latest.tar.gz

Cherry-pick particular tags

You can even cherry-pick particular tags of an image repository.
$ docker save -o ubuntu.tar ubuntu:lucid ubuntu:saucy

Parent command

Command Description
docker The base command for the Docker CLI.

docker image save

Description

Save one or more images to a tar archive (streamed to STDOUT by default)

Usage

docker image save [OPTIONS] IMAGE [IMAGE...]

Options

Name, shorthand Default Description
--output , -o Write to a file, instead of STDOUT

Parent command

Command Description
docker image Manage images
Command Description
docker image build Build an image from a Dockerfile
docker image history Show the history of an image
docker image import Import the contents from a tarball to create a filesystem image
docker image inspect Display detailed information on one or more images
docker image load Load an image from a tar archive or STDIN
docker image ls List images
docker image prune Remove unused images
docker image pull Pull an image or a repository from a registry
docker image push Push an image or a repository to a registry
docker image rm Remove one or more images
docker image save Save one or more images to a tar archive (streamed to STDOUT by default)
docker image tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE


docker load

Description

Load an image from a tar archive or STDIN

Usage

docker load [OPTIONS]

Extended description

Load an image or repository from a tar archive (even if compressed with gzip, bzip2, or xz) from a file or STDIN. It restores both images and tags.
For example uses of this command, refer to the examples section below.

Options

Name, shorthand Default Description
--input , -i Read from tar archive file, instead of STDIN
--quiet , -q Suppress the load output

Examples

$ docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

$ docker load < busybox.tar.gz

Loaded image: busybox:latest
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              769b9341d937        7 weeks ago         2.489 MB

$ docker load --input fedora.tar

Loaded image: fedora:rawhide

Loaded image: fedora:20

$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              769b9341d937        7 weeks ago         2.489 MB
fedora              rawhide             0d20aec6529d        7 weeks ago         387 MB
fedora              20                  58394af37342        7 weeks ago         385.5 MB
fedora              heisenbug           58394af37342        7 weeks ago         385.5 MB
fedora              latest              58394af37342        7 weeks ago         385.5 MB

Parent command

Command Description
docker The base command for the Docker CLI.

docker image load

Description

Load an image from a tar archive or STDIN

Usage

docker image load [OPTIONS]

Options

Name, shorthand
Default
Description
--input , -i
Read from tar archive file, instead of STDIN
--quiet , -q
Suppress the load output

Parent command

docker image
Manage images

 


How to move Docker container to another host

There is no straightforward way to directly move Docker container from one host to another. We workaround this by using one or more of these methods for the migration.

1. Export and import containers

Exporting a container means creating a compressed file from the container’s file system. The exported file is saved as a ‘gzip’ file.
docker export container-name | gzip > container-name.gz

This compressed file is then copied over to the new host via file transfer tools such as scp or rsync. In the new host, this gzip file is then imported into a new container.
zcat container-name.gz | docker import - container-name

The new container created in the new host can be accessed using ‘docker run’ command.
One drawback of export tool is that, it does not copy ports and variables, or the underlying data volume which contains the container data.
This can lead to errors when trying to load the container in another host. In such cases, we opt for Docker image migration to move containers from one host to another.

2. Container image migration

The most commonly used method to move Docker container to another host, is by migrating the image linked to that container.
For the container that has to be moved, first its Docker image is saved into a compressed file using ‘docker commit’ command.
docker commit container-id image-name

The image that is generated is compressed and moved into the new host machine. In the new host, a new container is created with ‘docker run’.
Using this method, the data volumes will not be migrated, but it preserves the data of the application created inside the container.

3. Save and load images

A docker image is a package of code, libraries, configuration files, etc. for an application. Docker containers are created out of these images.
The images can be compressed using ‘docker save’ and moved to a new host.
docker save image-name > image-name.tar

In the new host, this compressed image file can be used to create new image using ‘docker load’.
cat image-name.tar | docker load

4. Migrate data volumes

Data volumes in Docker machines are shared directories that contains the data specific to containers. The data in volumes are persistent and will not be lost during container recreation.
When Docker containers or images are moved from one host to another using export or commit tools, the underlying data volume is not migrated.
In such situations, the directory containing data is manually moved to the new host. Then containers are created there with reference to that directory as its data volume.
Another fool proof method is to backup and restore the data volume by passing ‘–volumes-from’ parameter in the ‘docker run’ command.
docker run --rm --volumes-from datavolume-name -v $(pwd):/backup image-name tar cvf  backup.tar /path-to-datavolume
Here, datavolume-name is the /path/to/volume. This command provides a backup of the data volume. To specify the working directory, we can specify the -w /backup as well. The backup generated in /backup folder can be moved to new host via scp or ftp tools.
Copied backup is then extracted and restored to the data volume in the new container there.
 docker run --rm --volumes-from datavolume-name -v $(pwd):/backup image-name bash -c "cd /path-to-datavolume && tar xvf /backup/backup.tar --strip 1"

5. Move entire Docker containers

The methods we saw here are applicable for individual containers. But in cases where all the containers are to be moved from one host to another, we adopt another method.
This method includes copying the entire “/var/lib/docker” directory to new host. To make this method successful, a few critical points are ensured.
  • The permissions and ownership of the folders are preserved.
  • Docker service is stopped before the move.
  • Docker versions in two hosts are verified to be compatible.
  • Container list and functionality is verified before and after the move.
  • Paths to the entry points and other configuration files are maintained.
In cases when this method does not work due to any hiccups, we configure custom scripts to migrate the containers and images from one host to another.

Tags:
docker transfer images between hosts
how to transfer docker images from one machine to another
transfer docker images between machines
transfer docker image to another machine
how to transfer docker image to another machine
transfer docker image from one host to another
transfer docker image
transfer docker image to another host
how to transfer docker images
how to transfer docker image from one machine to another
transfer docker image offline
transfer docker image over ssh
transfer docker image without registry
transfer docker image between registries
transfer docker image via ssh
transfer docker container from one server to another
docker transfer container to another machine
transfer file docker container
transfer file from docker container to host
transfer file into docker container
transfer files between docker containers
transfer data to docker container
transfer data between docker containers
transfer file out of docker container
transfer docker container to another host
transfer files to a docker container
transfer a file to a docker container
transfer docker image to another machine
transfer docker image from one host to another
docker container data transfer
docker transfer file from container to host
docker container transfer files
transfer files to running docker container
how to transfer docker container
docker transfer file into container
docker transfer file out of container
transfer docker containers
docker transfer file to container
transfer file to docker container

1 Comments

  1. How To Export Existing Docker Images And Containers ~ System Admin Share >>>>> Download Now

    >>>>> Download Full

    How To Export Existing Docker Images And Containers ~ System Admin Share >>>>> Download LINK

    >>>>> Download Now

    How To Export Existing Docker Images And Containers ~ System Admin Share >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete
Previous Post Next Post