Exporting Multiple Docker Images Alongside Docker-Compose

It is possible to export a Docker container for use on another machine.

However, what if you have two containers locally that are connected with docker-compose, and you want to export them together?

Unfortunately, you can’t quite do that. However, exporting them both together with your compose file is not that difficult.

Exporting

Let’s look at an example. Say I have a project/ directory, with two subdirectories: project/frontend/ and project/backend/, and each has its own Dockerfile. A project/docker-compose.yml that connects them together might look like this:

Here, docker-compose has defined several important aspects about the project, including where the code is located. If I run docker-compose up, it will start both environments. It will refer to the two Dockerfiles in their subdirectories, and then will create containers accordingly. If you open Docker Desktop, you should see the two containers running together under a section named after the folder your .yml file is in.

Now you can export the running containers to .tar files. To do so, first look at the running processes with docker ps. For example, this is what my terminal prints out:

Then commit them with the following commands. Make sure to change it to your own container_id, and your own name for the image.

Next, save them to a tarball (this will take a while). The tarball can have any name you like.

Finally, you need a new docker-compose.yml. This only needs to be changed to reflect that you are no longer using directories, but Docker images. We will change the build argument, and remove the volumes.

So now you have three files to export:

Importing

Once you get your files to a new machine, spinning up Docker is trivial! Make sure that you are in the right directory, then load the tarballs into Docker (this will take a loooong time):

Finally, run them.

Originally published at https://elliott-king.github.io on October 7, 2020.

Written by

NYC, fullstack

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store