• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Save Docker Container As Image

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Published: 1/31/2024

About Terminus

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:

Thumbnail for

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

Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

Docker

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.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

Use An .env File In Docker

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

Docker

Run SSH In Docker

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

Docker
Thumbnail for Gabriel ManricksGabriel Manricks

Launch MySQL Using Docker Compose

Learn how to launch a MySQL container in Docker Compose.

DockerSQL

Execute in a Docker Container

Learn how to execute one or multiple commands in a Docker container using the docker exec command.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

Expose Docker Container Ports

Learn how to publish and expose Docker container ports using the docker run command and Dockerfiles.

Docker

Restart Containers In Docker Compose

Learn how to restart and rebuild one or more containers in Docker Compose.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

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

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

Rename A Docker Image

Learn how to rename Docker images locally and remotely using the docker tag command.

Docker

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null