2207

How do I transfer a Docker image from one machine to another one without using a repository, no matter private or public?

I create my own image in VirtualBox, and when it is finished I try to deploy to other machines to have real usage.

Since it is based on my own based image (like Red Hat Linux), it cannot be recreated from a Dockerfile. My dockerfile isn't easily portable.

Are there simple commands I can use? Or another solution?

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
Larry Cai
  • 55,923
  • 34
  • 110
  • 156
  • 3
    Does this answer your question? [How to save all Docker images and copy to another machine](https://stackoverflow.com/questions/35575674/how-to-save-all-docker-images-and-copy-to-another-machine) – Mišo Sep 02 '20 at 07:59
  • There are ways to copy from one machine to another - see the answer https://stackoverflow.com/a/26226261/4329358 (@kolypto) below. However, gzip compression/decompression on either end may cause more delays than transferring over Ethernet or WiFi. You can set up a [private Docker repository](https://bobcares.com/blog/docker-private-repository/) of your own which may speed up transferring images between machines. – Daisuke Aramaki Feb 01 '23 at 21:06
  • Related discussions: https://stackoverflow.com/q/43274794/320399 , https://stackoverflow.com/q/58028894/320399, and https://stackoverflow.com/q/58768032/320399 – blong Apr 28 '23 at 15:25

21 Answers21

3557

You will need to save the Docker image as a tar file:

docker save -o <path for generated tar file> <image name>

Then copy your image to a new system with regular file transfer tools such as cp, scp, or rsync (preferred for big files). After that you will have to load the image into Docker:

docker load -i <path to image tar file>

You should add filename (not just directory) with -o, for example:

docker save -o c:/myfile.tar centos:16

PS: You may need to sudo all commands.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Daiwei
  • 40,666
  • 3
  • 38
  • 48
  • 62
    This is the better answer for *images*. – Andy May 29 '14 at 20:18
  • 75
    also, it is better to use `repo:tag` as the image reference rather than `image id`. If you use `image id`, the loaded image will not retain the tag (and you will have to do another step to tag the image). – wisbucky Sep 14 '17 at 21:03
  • 2
    I used the image id instead of the name:tag Now I'm sitting here, loaded the image and have a for REPOSITORY and TAG. What is the right way to bring the name and tag back? @wisbucky – Ulfhetnar Jan 10 '18 at 07:37
  • 6
    To tag, first identity the IMAGE ID using `docker images`, then use `docker tag DESIREDIMAGEID mycompany/myreponame`. If your image id is 591de551d6e4, you can abbreviate the image id: `docker tag 59 mycompany/myreponame` – Junior Mayhé Aug 15 '18 at 13:07
  • @AndiJay Take a look at [How to copy docker images from one host to another?](https://stackoverflow.com/questions/41154305/how-to-copy-docker-images-from-one-host-to-another?noredirect=1&lq=1) – duct_tape_coder Feb 14 '19 at 16:12
  • Make sure the filepath is to a new file or it won't work – Ryan Walker Mar 01 '19 at 22:42
  • It looks like loading the path to the `.tar` file that it was saved to on the remote server doesn't work after using `docker-machine scp`. For example, if you use `docker-machine` to copy over into `new_machine:/home/example.tar`, running `docker load -i new_machine:/home/example.tar` or just `docker load -i /home/example.tar` does not work. – rb612 Apr 23 '19 at 01:05
  • @rb612 that sounds pretty normal to me: the image should be copied to a host, not inside a machine. Or am I misunderstanding your comment? – Vic Seedoubleyew Aug 21 '20 at 10:29
  • @jj_ the answer is rather Linux oriented... isn't it? – serge May 03 '21 at 15:37
  • 1
    @serge not at all, it even has an example where a windows path is used... – Redoman May 05 '21 at 11:27
  • I've tried this way and I can save the image in the tar file but I can't take it to the other machine. The command line says `unsupported os linux` when I try to execute the load command in the new machine. – Roberto C. Rodriguez-Hidalgo Jun 02 '21 at 17:39
  • This is a not good solution! please notice that `docker load` will squash all layers!!!! – Doz Parp Dec 16 '21 at 04:09
  • 1
    The result won't be compressed, so running `pigz --keep ` is almost necessary. – asynts Jan 13 '22 at 08:19
  • usually you are doing something terriibly wrong when running `sudo` with docker ... your docker should be configured for non-root users to run – Yordan Georgiev Jan 29 '22 at 08:03
  • Why is `rsync` preferred for big files? – Abhilash Aug 29 '22 at 12:37
  • it gives me `open /home/ahmad/.docker_temp_52129870: permission denied` – Ahmad Jan 27 '23 at 12:40
  • So @Daiwei, will a docker image built from a Windows machine can directly load into a Linux machine? I mean is there nothing we need to change? – Arun Sudhakaran May 24 '23 at 11:36
866

Transferring a Docker image via SSH, bzipping the content on the fly:

docker save <image> | bzip2 | ssh user@host docker load

Note that docker load automatically decompresses images for you. It supports gzip, bzip2 and xz.

It's also a good idea to put pv in the middle of the pipe to see how the transfer is going:

docker save <image> | bzip2 | pv | ssh user@host docker load

(More info about pv: home page, man page).

  • Use gzip/gunzip when your network is fast and you can upload at 10 Mb/s and more -- because bzip won't be able to compress fast enough, gzip will be much faster (Thanks @Thomas Steinbach)

  • Use xz if you're on really slow network (e.g. mobile internet). xz offers a higher compression ratio (Thanks @jgmjgm)

kolypto
  • 31,774
  • 17
  • 105
  • 99
  • 25
    When using docker-machine, you can do `docker $(docker-machine config mach1) save | docker $(docker-machine config mach2) load` to copy images between machines *mach1* and *mach2*. – matlehmann Sep 04 '15 at 12:57
  • 5
    @manojlds `eval $(docker-machine env dev)` is good for general communication with a single docker host but not to copy between two machines, since this involves two different docker hosts / docker machines. – matlehmann Nov 18 '15 at 15:47
  • 27
    to do this in reverse (remote to local): `ssh target_server 'docker save image:latest | bzip2' | pv | bunzip2 | docker load` – ThorSummoner Feb 01 '16 at 22:50
  • @matlehmann Would you post this as a separate answer? It's exactly what I was looking for – herbrandson Oct 13 '16 at 13:39
  • @herbrandson see my new answer - thanks for your encouragement. – matlehmann Oct 14 '16 at 09:54
  • 2
    Is there any way to do this when docker requires sudo on the target machine? I tried (without compression) `docker save my_img:v1 | ssh -t -t my_user@my_machine sudo docker load`. Without the "-t" switch, sudo complains _sudo: sorry, you must have a tty to run sudo_; with one "-t" it's the same message because ssh says _Pseudo-terminal will not be allocated because stdin is not a terminal._ and finally, with two "-t"s, I get the content of the tar file (i.e. the image) on my terminal. Any ideas? – hunger Dec 14 '16 at 10:02
  • 2
    @JosefStark I needed to add "Defaults: !requiretty" when editing the sudoers file to stop the "Sorry" message from sudo. I don't know how much of a difference it makes but I also put everything after the user@host in quotes (so "[...] | ssh user@host 'bunzip2 | sudo docker load'"). – VirtualWolf Jun 15 '17 at 10:12
  • 1
    Cool combo. Here my hint: gzip is much faster on high bandwith than bzip: `docker save myimage | pv | ssh remoteserver 'docker load' 1,44GiB 0:02:31 [9,71MiB/s] docker save myimage | bzip2 | pv | ssh remoteserver 'bunzip2 | docker load' 613MiB 0:02:47 [3,67MiB/s]` `docker save myimage | gzip | pv | ssh remoteserver 'gunzip | docker load' 667MiB 0:01:34 [7,08MiB/s]` – Thomas Steinbach Jun 29 '17 at 12:41
  • If you do this regularly, I created a `docker-send` script based on this answer with support for sudo. I'll probably add support for other compression formats in the future. https://gist.github.com/flungo/6b5f607db87c3c034609c8dbc5b40966 – flungo Aug 04 '17 at 09:53
  • 1
    @hunger a bit late to the party but for anyone out there looking to do this. Edit the sudoers file to allow sudo without password entry. (Be sure to check security policies though). I.e. add: hunger ALL=(ALL) NOPASSWD:ALL – RobbyD Feb 22 '19 at 09:21
  • @RobbyD You can grant access without password in a more restrictive fassion: https://www.ostechnix.com/run-particular-commands-without-sudo-password-linux/ – j2L4e Mar 22 '19 at 10:41
  • 1
    xz is probably a better option than bzip. gzip for portability, xz for best results. xz level 1 performanes roughly the same as gzip level 9 but produces significantly smaller files. – jgmjgm Apr 12 '19 at 13:05
  • In fact, you can almost certainly tell ssh to compress things though it'll probably be a compression library optimised for speed. – jgmjgm Apr 12 '19 at 13:12
  • I have tried this solution (`docker` `commit` `save` `load` `scp` `etc`), however, all file generated inside previous container are **lost** and it like recover to "factor setting". is there any wrong with my setup? – questionasker Mar 21 '22 at 14:06
  • remote machine requires sudo permission. any body know how to solve? ``` Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/images/load?quiet=1": dial unix /var/run/docker.sock: connect: permission denied ``` – Daniel Inbaraj Aug 19 '22 at 04:52
  • @DanielInbaraj Create the docker group on the remote machine and add your user to it. There may be some security implications that I am not completely up to date on. I would think it was worse always to run docker with `sudo`. – grofte Aug 23 '22 at 08:15
  • Even after the docker load was successful at the target location. It required me to do 'docker push image' again at the target location before being able to pull the image and use it. – OkraSala Nov 04 '22 at 02:51
  • @OkraSala wait ... doesn't that push and pull the image to and from DockerHub? – samvv Apr 06 '23 at 13:53
  • @samvv I had a local registry, so DockerHub was not in picture. – OkraSala Apr 06 '23 at 14:15
159

To save an image to any file path or shared NFS place see the following example.

Get the image id by doing:

docker images

Say you have an image with id "matrix-data".

Save the image with id:

docker save -o /home/matrix/matrix-data.tar matrix-data

Copy the image from the path to any host. Now import to your local Docker installation using:

docker load -i <path to copied image file>
Martti Laine
  • 12,655
  • 22
  • 68
  • 102
Sohan
  • 6,252
  • 5
  • 35
  • 56
93

You can use a one-liner with DOCKER_HOST variable:

docker save app:1.0 | gzip | DOCKER_HOST=ssh://user@remotehost docker load
vitalets
  • 4,675
  • 1
  • 35
  • 35
  • 13
    This is definitely the command I was looking for, I think this answer deserves more love – Francois Jul 13 '20 at 15:22
  • 4
    prereqs: ssh credentials setup on the remote (ssh-copy-id) and the local and remote user both need to be in the docker group (sudo usermod -aG docker $USER) – GrendleM Nov 17 '20 at 22:57
  • 2
    Are there any (performance) differences to `ssh user@remotehost docker load` here? – Mouagip Nov 10 '21 at 14:11
  • 2
    @Mouagip I made a comparison between the two and this one (`DOCKER_HOST=ssh://user@remotehost docker load`) seems to be a bit faster than `ssh user@remotehost docker load`. On my setup the difference was about 5-10 seconds on a process that took about 1 minute and 50 seconds. (I timed both of the options multiple times) – Yarin Feb 22 '23 at 14:02
71

First save the Docker image to a compressed archive:

docker save <docker image name> | gzip > <docker image name>.tar.gz

Then load the exported image to Docker using the below command:

zcat <docker image name>.tar.gz | docker load
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
Damodaran
  • 10,882
  • 10
  • 60
  • 81
  • 32
    For loading, `docker load < my-image.tar.gz` is sufficient. The image gets decompressed automatically for gzip, bzip2, and xz. – Flux Apr 04 '18 at 02:41
50

Run

docker images

to see a list of the images on the host. Let's say you have an image called awesomesauce. In your terminal, cd to the directory where you want to export the image to. Now run:

docker save awesomesauce:latest > awesomesauce.tar

Copy the tar file to a thumb drive or whatever, and then copy it to the new host computer.

Now from the new host do:

docker load < awesomesauce.tar

Now go have a coffee and read Hacker News...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Neil Girardi
  • 4,533
  • 1
  • 28
  • 45
34

The fastest way to save and load docker image through gzip command:

docker save <image_id> | gzip > image_file.tgz

To load your zipped image on another server use immediate this command, it will be recognized as zipped image:

docker load -i image_file.tgz

to rename, or re-tag the image use:

docker image tag <image_id> <image_path_name>:<version>

for example:

docker image tag 4444444 your_docker_or_harbor_path/ubuntu:14.0
Tarek Kalaji
  • 2,149
  • 27
  • 30
  • This is a great answer, and it works a treat, but do you have any idea how to get the container back? – Owl Mar 18 '23 at 19:03
28

For a flattened export of a container's filesystem, use;

docker export CONTAINER_ID > my_container.tar

Use cat my_container.tar | docker import - to import said image.

vitaut
  • 49,672
  • 25
  • 199
  • 336
  • 10
    it shall be `cat my_container.tar | docker import - my_container:new` if import locally according to cmd help – Larry Cai May 30 '14 at 07:51
  • 7
    This is more for backing up a running container than for deploying an image. – Kousha Nov 25 '14 at 03:49
  • I tried docker save at ubuntu machines which all docker images up and running good. Then i docker load them at windows machine. There are many errors when i docker run or start them. Any ideas whats wrong? – user2201789 Nov 16 '21 at 12:29
  • this does not work on `windows` command prompt or powershell directly because there is no *.tgz support to load package, you may need to install packages and change the command on windows – Tarek Kalaji Mar 20 '23 at 15:20
26

docker-push-ssh is a command line utility I created just for this scenario.

It sets up a temporary private Docker registry on the server, establishes an SSH tunnel from your localhost, pushes your image, then cleans up after itself.

The benefit of this approach over docker save (at the time of writing most answers are using this method) is that only the new layers are pushed to the server, resulting in a MUCH quicker upload.

Oftentimes using an intermediate registry like dockerhub is undesirable, and cumbersome.

https://github.com/brthor/docker-push-ssh

Install:

pip install docker-push-ssh

Example:

docker-push-ssh -i ~/my_ssh_key username@myserver.com my-docker-image

The biggest caveat is that you have to manually add your localhost to Docker's insecure_registries configuration. Run the tool once and it will give you an informative error:

Error Pushing Image: Ensure localhost:5000 is added to your insecure registries.
More Details (OS X): https://stackoverflow.com/questions/32808215/where-to-set-the-insecure-registry-flag-on-mac-os

Where should I set the '--insecure-registry' flag on Mac OS?

Louis
  • 146,715
  • 28
  • 274
  • 320
brthornbury
  • 3,518
  • 1
  • 22
  • 21
  • This is a promising utility. Do any other answers offer a solution which copies only the updated layers? But, I had to work through a) no py3 support, b) ssh identify file must be specified though mine is in default location, and c) port 5000 is already in use on my servers and there is no option to change to another port. – nmgeek Mar 04 '21 at 00:09
  • @nmgeek Consider making a PR to the utility on GitHub so others can benefit from your changes. – brthornbury Mar 05 '21 at 22:15
  • 1
    There is PR for Python3: https://github.com/brthor/docker-push-ssh/pull/15 @brthornbury can you merge it? – Andrius Jan 21 '22 at 12:32
18

When using docker-machine, you can copy images between machines mach1 and mach2 with:

docker $(docker-machine config <mach1>) save <image> | docker $(docker-machine config <mach2>) load

And of course you can also stick pv in the middle to get a progess indicator:

docker $(docker-machine config <mach1>) save <image> | pv | docker $(docker-machine config <mach2>) load

You may also omit one of the docker-machine config sub-shells, to use your current default docker-host.

docker save <image> | docker $(docker-machine config <mach>) load

to copy image from current docker-host to mach

or

docker $(docker-machine config <mach>) save <image> | docker load

to copy from mach to current docker-host.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
matlehmann
  • 392
  • 3
  • 8
17

The best way to save all the images is like this :

docker save $(docker images --format '{{.Repository}}:{{.Tag}}') -o allimages.tar

Above code will save all the images in allimages.tar and to load the images go to the directory where you saved the images and run this command :

docker load -i allimages.tar

Just make sure to use this commands in PowerShell and not in Commad Prompt

Kaveh Naseri
  • 1,102
  • 2
  • 15
  • 24
16

I assume you need to save couchdb-cartridge which has an image id of 7ebc8510bc2c:

stratos@Dev-PC:~$ docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
couchdb-cartridge                      latest              7ebc8510bc2c        17 hours ago        1.102 GB
192.168.57.30:5042/couchdb-cartridge   latest              7ebc8510bc2c        17 hours ago        1.102 GB
ubuntu                                 14.04               53bf7a53e890        3 days ago          221.3 MB

Save the archiveName image to a tar file. I will use the /media/sf_docker_vm/ to save the image.

stratos@Dev-PC:~$ docker save imageID > /media/sf_docker_vm/archiveName.tar

Copy the archiveName.tar file to your new Docker instance using whatever method works in your environment, for example FTP, SCP, etc.

Run the docker load command on your new Docker instance and specify the location of the image tar file.

stratos@Dev-PC:~$ docker load < /media/sf_docker_vm/archiveName.tar

Finally, run the docker images command to check that the image is now available.

stratos@Dev-PC:~$ docker images
REPOSITORY                             TAG        IMAGE ID         CREATED             VIRTUAL SIZE
couchdb-cartridge                      latest     7ebc8510bc2c     17 hours ago        1.102 GB
192.168.57.30:5042/couchdb-cartridge   latest     bc8510bc2c       17 hours ago        1.102 GB
ubuntu                                 14.04      4d2eab1c0b9a     3 days ago          221.3 MB

Please find this detailed post.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tk_
  • 16,415
  • 8
  • 80
  • 90
14

REAL WORLD EXAMPLE

#host1
systemctl stop docker
systemctl start docker
docker commit -p 1d09068ef111 ubuntu001_bkp3
#create backup
docker save -o ubuntu001_bkp3.tar ubuntu001_bkp3

#upload ubuntu001_bkp3.tar to my online drive
aws s3 cp ubuntu001_bkp3.tar s3://mybucket001/


#host2
systemctl stop docker
systemctl start docker
cd /dir1

#download ubuntu001_bkp3.tar from my online drive
aws s3 cp s3://mybucket001/ubuntu001_bkp3.tar /dir1

#restore backup
cat ./ubuntu001_bkp3.tar  | docker load
docker run --name ubuntu001 -it ubuntu001_bkp3:latest bash
docker ps -a
docker attach ubuntu001




quine9997
  • 685
  • 7
  • 13
12

To transfer images from your local Docker installation to a minikube VM:

docker save <image> | (eval $(minikube docker-env) && docker load)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bryan Larsen
  • 9,468
  • 8
  • 56
  • 46
9

All other answers are very helpful. I just went through the same problem and figure out an easy way with docker machine scp.

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
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peng
  • 434
  • 5
  • 14
6

If you are working on a Windows machine and uploading to a linux machine commands such as

docker save <image> | ssh user@host docker load

will not work if you are using powershell as it seems that it adds an additional character to the output. If you run the command using cmd (Command Prompt) it will however work. As a side note you can also install gzip using Chocolatey and the following will also work from cmd.

docker save <image> | gzip | ssh user@host docker load
runxc1 Bret Ferrier
  • 8,096
  • 14
  • 61
  • 100
5

Based on the @kolypto 's answer, this worked great for me but only with sudo for docker load:

docker save <image> | bzip2 | pv | ssh user@host sudo docker load

or if you don't have / don't want to install the pv:

docker save <image> | bzip2 | ssh user@host sudo docker load

No need to manually zip or similar.

Yoshi
  • 304
  • 4
  • 6
  • This is because your user is not a member of the "docker" group :) Don't do sudo; just do this once: `sudo adduser $USER docker` – kolypto Jun 01 '23 at 22:00
4

I want to move all images with tags.

```
OUT=$(docker images --format '{{.Repository}}:{{.Tag}}')
OUTPUT=($OUT)
docker save $(echo "${OUTPUT[*]}") -o /dir/images.tar
``` 

Explanation:

First OUT gets all tags but separated with new lines. Second OUTPUT gets all tags in an array. Third $(echo "${OUTPUT[*]}") puts all tags for a single docker save command so that all images are in a single tar.

Additionally, this can be zipped using gzip. On target, run:

tar xvf images.tar.gz -O | docker load

-O option to tar puts contents on stdin which can be grabbed by docker load.

Vikram
  • 565
  • 1
  • 5
  • 10
3

You may use sshfs:

$ sshfs user@ip:/<remote-path> <local-mount-path>
$ docker save <image-id> > <local-mount-path>/myImage.tar
Chu
  • 468
  • 1
  • 5
  • 21
3

1. Pull an image or a repository from a registry.

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

2. Save it as a .tar file.

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

For example:

docker pull hello-world
docker save -o hello-world.tar hello-world
ofir_aghai
  • 3,017
  • 1
  • 37
  • 43
2

Script to perform Docker save and load function (tried and tested):

Docker Save:

#!/bin/bash

#files will be saved in the dir 'Docker_images'
mkdir Docker_images
cd Docker_images
directory=`pwd`
c=0
#save the image names in 'list.txt'
doc= docker images | awk '{print $1}' > list.txt
printf "START \n"
input="$directory/list.txt"
#Check and create the image tar for the docker images
while IFS= read -r line
do
     one=`echo $line | awk '{print $1}'`
     two=`echo $line | awk '{print $1}' | cut -c 1-3`
     if [ "$one" != "<none>" ]; then
             c=$((c+1))
             printf "\n $one \n $two \n"
             docker save -o $two$c'.tar' $one
             printf "Docker image number $c successfully converted:   $two$c \n \n"
     fi
done < "$input"

Docker Load:

#!/bin/bash

cd Docker_images/
directory=`pwd`
ls | grep tar > files.txt
c=0
printf "START \n"
input="$directory/files.txt"
while IFS= read -r line
do
     c=$((c+1))
     printf "$c) $line \n"
     docker load -i $line
     printf "$c) Successfully created the Docker image $line  \n \n"
done < "$input"
Syed Faraz Umar
  • 389
  • 1
  • 4
  • 10