Docker Certified Associate Exam Course

Docker Image Management

Docker commit method

In this guide, you’ll learn how to create a Docker image from a running container using docker container commit. This technique can be useful for rapid prototyping or debugging changes without writing a Dockerfile. For production-grade images, you should still prefer the Dockerfile approach to ensure repeatability and version control.

Overview

Typically, custom images are built with a Dockerfile:

docker build -t myapp:latest .

Alternatively, you can:

  1. Launch a container from a base image (e.g., httpd).
  2. Modify files or install packages inside the container.
  3. Commit the container’s state as a new image.

Warning

The docker commit workflow is not recommended for production systems. Use a Dockerfile for maintainability, readability, and versioning.

When to Use docker commit

ScenarioRecommended?Alternative
One-off experimentsYesDockerfile (optional)
Capturing state for debuggingYesVolumes, logging
Production-ready, repeatable CINoDockerfile

Step-by-Step Example

  1. Run an httpd container in detached mode

    docker run -d --name httpd httpd
    
  2. Enter the container and update the default web page

    docker exec -it httpd bash
    root@container:/# cat > /usr/local/apache2/htdocs/index.html <<EOF
    Welcome to my custom web application
    EOF
    root@container:/# exit
    
  3. Commit the container state to a new image

    docker container commit -a "Ravi" httpd customhttpd
    
  4. Verify the new image

    docker image ls
    REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
    customhttpd   latest    adac0f56a7df   5 seconds ago   138MB
    httpd         latest    417af7dc28bc   8 days ago      138MB
    

Comparison: Dockerfile vs. docker commit

FeatureDockerfiledocker commit
Version controlYes (plain text)No
AutomationCI/CD pipelinesManual or scripted
ReproducibilityHighLow
Ease of simple tweaksModerateVery fast
Best practice for production

References

Watch Video

Watch video content

Previous
Demo Build Tomcat image