Save Docker Container As Image

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: January 31, 2024

To help improve the portability, reproducibility, and debugging of their applications, Docker allows developers to build images from running or stopped containers. This way, developers can easily:

  • Capture the required dependencies and configurations to ensure that the application runs the same way every time.
  • Package the application to make it deployable on any operating system.
  • Create a snapshot of their application to fix a specific problem without corrupting a production container caused by unforeseen side effects.

The Short Answer

To create an image from a container, you can use the docker commit command:

Bash
$ docker commit <container> <image>

Where:

  • container is the name or the ID of the container that can be obtained using the docker ps command.
  • image is the name of the image you want to create.

For example:

Bash
$ docker commit 4b2386a65651 node-server:beta

Note that if the container you are creating an image from is running, Docker will automatically pause it while the image is committed, in order to reduce the likelihood of encountering data corruption during this process.

Use AI in your Warp terminal to easily recall the syntax

Warp, our free terminal app, has a handy feature called Artificial Intelligence Command Search (AICS) that helps generate shell commands with natural language. If you can't remember which Docker command is used to create an image from a container, you can type a # followed by a short sentence describing your command:

Overwriting Dockerfile Instructions

A Dockerfileis a text file that contains all the necessary instructions for building a Docker image. These instructions can be used for installing and configuring command line tools, declaring environment variables, exposing ports, copying files from the local environment, and so on.

When creating an image from a container, these instructions can be overwritten using the -c flag (short for change).

For example, to change an environment variable:

Bash
$ docker commit -c 
 "ENV NODE_ENV=development" <container> <image>

Note it is possible to change multiple instructions at once:

Bash
$ docker commit -c 'WORKDIR /app' -c 
 'CMD ["node", "app.js"]' <container> <image>

A word on volumes

Since the commit operation does not include any data contained in volumes mounted inside the container, you will have to manually attach the desired volume when starting the container based on the image you just created using the -v flag (short for volume):

Bash
$ docker run -v my-volume:/app my-image

You can read more about volumes and the docker commit command in the official Docker documentation.

Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Learning Docker (The Easy Way) Using LazyDocker & Warp

A concise guide to learning Docker using Lazydocker. Highlights Docker’s benefits and takes advantage of Warp's AI features for a quick setup.

Run SSH In Docker

Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.

Remove a Docker Image

Learn how to remove a Docker image locally, on a Docker registry, and on Artifactory.

Override the Container Entrypoint With docker run

Learn how to override and customize the entrypoint of a Docker container using the docker run command.

The Dockerfile ARG Instruction

Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.

Start a Docker Container

Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.

Stop All Docker Containers

How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.

Set Docker Container Hostname

Learn how to set, change and match a docker container hostname.

How To Use An .env File In Docker Compose

Learn how define and pass environment variables to Docker containers using an .env file in Docker Compose.

Use An .env File In Docker

Learn how to write and use .env files in Docker to populate the environment of containers on startup.

Restart Docker Containers

Learn how to restart Docker containers automatically with restart policies and manually using the docker restart, docker start, docker stop and docker kill commands.

Run Bash Shell In Docker

Start an interactive shell in Docker container