How to share a private docker image or container securely
Some times it's required to copy a docker container between machines, or even developers. One option is to use a hosted repository and pay some money for this opportunity. Another option is to use a machine that has a reachable IP address and Docker to run a private and secure Docker Registry.
In this example we have a Linux VM somewhere on the network or Internet. That VM will accept ssh connections from us.
First we set up a ssh forwarding tunnel.
$ ssh -L 5000:localhost:5000 YOUR_VMS_IP_HERE
Run a Docker Registry container in your VM.
From a shell in your VM, run:
1$ docker run -d -p 5000:5000 --restart=always --name registry registry
If we want to share a container with state, stop it and commit it. Lets say we want the image to have the name Dolly.
Back in a shell on your local machine, run:
1$ docker commit DOCKER_IMAGE_ID_OR_NAME localhost:5000/Dolly
2
Alternatively, if we want to share a local image:
1$ docker tag Dolly localhost:5000/Dolly
Then finally, push the image to the VM.
$ docker push localhost:5000/Dolly
Now you can set up a tunnel from your other machine (or the other developer(s) can do that) and just pull it or run it.
1$ docker run localhost:5000/Dolly
And that's it.