Save Docker Container As Image
Razvan Ludosanu
Founder, learnbackend.dev
Published: 1/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:
$ docker commit <container> <image>
Run in Warp
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:
$ docker commit 4b2386a65651 node-server:beta
Run in Warp
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:
$ docker commit -c
"ENV NODE_ENV=development" <container> <image>
Run in Warp
Note it is possible to change multiple instructions at once:
$ docker commit -c 'WORKDIR /app' -c
'CMD ["node", "app.js"]' <container> <image>
Run in Warp
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):
$ docker run -v my-volume:/app my-image
Run in Warp
You can read more about volumes and the docker commit command in the official Docker documentation.
Written by
Razvan Ludosanu
Founder, learnbackend.dev
Filed Under
Related Articles
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.
Use An .env File In Docker
Learn how to write and use .env files in Docker to populate the environment of containers on startup.
Run SSH In Docker
Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.
Launch MySQL Using Docker Compose
Learn how to launch a MySQL container in Docker Compose.
Execute in a Docker Container
Learn how to execute one or multiple commands in a Docker container using the docker exec command.
Expose Docker Container Ports
Learn how to publish and expose Docker container ports using the docker run command and Dockerfiles.
Restart Containers In Docker Compose
Learn how to restart and rebuild one or more containers in Docker Compose.
Output Logs in Docker Compose
Learn how to output, monitor, customize and filter the logs of the containers related to one or more services in Docker Compose
Rename A Docker Image
Learn how to rename Docker images locally and remotely using the docker tag command.