Save Docker Container As Image
Last Updated on 2023-03-23
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 [.inline-code]docker commit[.inline-code] command:
- [.inline-code]container[.inline-code] is the name or the ID of the container that can be obtained using the [.inline-code]docker ps[.inline-code] command.
- [.inline-code]image[.inline-code] is the name of the image you want to create.
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.
[#easily-recall-syntax]Use AI in your Warp terminal to easily recall the syntax[#easily-recall-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 [.inline-code]#[.inline-code] followed by a short sentence describing your command:
[#overwriting-dockerfile]Overwriting Dockerfile Instructions[#overwriting-dockerfile]
A Dockerfile is 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 [.inline-code]-c[.inline-code] flag (short for change).
For example, to change an environment variable:
Note it is possible to change multiple instructions at once:
[#volumes]A word on volumes[#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 [.inline-code]-v[.inline-code] flag (short for volume):
You can read more about volumes and the [.inline-code]docker commit[.inline-code] command in the official Docker documentation.