Creating a new Docker Image from a Running Container

You can consider this tutorial, but essentially these are my notes so that I won’t forget the steps involved in:

  • committing changes to an already-running container to an image
  • pushing the image to a registry
  • running both modified and original containers.

Purpose

When I was writing up the final results for a paper with more than 80+ co-authors, I wanted to containerize the main results from the statistical model. The image containing the R script and packages required was pushed to my private registry here.

Step 1: Check Running Containers

To see the list of currently running containers and their details, use the following command:

docker container ls

Example output:

CONTAINER ID   IMAGE                                      COMMAND   CREATED          STAUS          PORTS                    NAMES
461d6df5d57d   nghuixin/enigma-brain-age-model1c:latest   "/init"   34 minutes ago   Up 34 minutes   0.0.0.0:8787->8787/tcp   distracted_panini

Take note of the NAMES column to identify the running container name. In this case, the name is distracted_panini.

Step 2: Commit the Running Container as an Image

To save the state of the running container as a new image, use the docker commit command:

docker commit distracted_panini nghuixin/enigma-brain-age-model1c:2.0

This command commits the changes of the running container distracted_panini to a new image tagged as nghuixin/enigma-brain-age-model1c:2.0.

Step 3: Push the Image to the Registry

Next, push the newly created image to your Docker registry:

docker push nghuixin/enigma-brain-age-model1c:2.0

This command uploads the image nghuixin/enigma-brain-age-model1c:2.0 to your registry, making it available for use on other systems.

Step 4: Run the Modified Container

To run the container with the changes made to the original active container, use the following command:

docker run --rm -p 8787:8787 -e DISABLE_AUTH=true -v ${PWD}/../General/data/DescriptiveData_080720_2.xlsx:/home/rstudio/../General/data/DescriptiveData_080720_2.xlsx -v ${PWD}/Master_BD_080720_withPredictions_withNewHajek_fixed.sav:/home/rstudio/Master_BD_080720_withPredictions_withNewHajek_fixed.sav nghuixin/enigma-brain-age-model1c:2.0

The volumes mounted to the container should correspond to the path specified in your script and the location of the folders and files in the local computer. Here’s the tree for illustration purposes:

/
|-- home/
|   |-- rstudio/
|   |   |-- ../General/data/
|   |   |   |-- DescriptiveData_080720_2.xlsx
|   |   |-- Master_BD_080720_withPredictions_withNewHajek_fixed.sav

Step 5: Run the Unmodified Container

If you need to run the original, unmodified container, use this command:

docker run --rm -p 8787:8787 -e DISABLE_AUTH=true -v ${PWD}/../General/data/DescriptiveData_080720_2.xlsx:/home/rstudio/../General/data/DescriptiveData_080720_2.xlsx -v ${PWD}/Master_BD_080720_withPredictions_withNewHajek_fixed.sav:/home/rstudio/Master_BD_080720_withPredictions_withNewHajek_fixed.sav nghuixin/enigma-brain-age-model1c:latest

Summary

By following these steps, you can save changes as new images, push them to a registry, and run both modified and unmodified versions of your containers.