Copy a Docker container from one machine to another

I ran into a situation this weekend where I needed to copy an ElasticSearch Docker container(already populated with data) from one Mac to another so I did some research and this is what worked for me.

1. Get a list of all my containers
docker ps -a

2. Take the container and make an image of it
docker commit <container_id> <mynewimage>

3. Save the image to a tar file
docker save <mynewimage> > <mynewimage.tar>

4. On the new machine, load the tar file as an image
docker load -i <mynewimage.tar>

5. Run the image as a container(Elastic Search ports for example)
docker run -i -p 9200:9200 -p 9300:9300 <mynewimage>

The size of these images could be large depending on your datastore size so you may want to do a docker rmi <mynewimage id> on the machine you are copying from to free up drive space.

I’ve now found a use for the Terabyte of space I now have on Dropbox.  I use it to store these tar files(gzip’d of course).