# Day 7: Create a Docker Image From a Container
🎯 Objective
Create a new Docker image from an existing container using the docker commit command.
The basic command is:
docker commit <container_name> <image_name>:<tag>This captures changes made inside a container and saves them as a reusable Docker image.
🧠 Concept
Normally, Docker follows this flow:
Docker Image
│
│ docker run
▼
ContainerAn image is used to create a container.
However, docker commit allows us to do the reverse:
Container
│
│ docker commit
▼
New Docker ImageThis is useful when you:
📦 Step 1: Check Existing Containers
List running containers:
docker psTo include stopped containers:
docker ps -aExample:
CONTAINER ID IMAGE COMMAND STATUS NAMES
a12bc34def56 ubuntu "bash" Up 5 minutes dev-containerIn this example, the container name is:
dev-container🔧 Step 2: Make Changes Inside the Container
If needed, enter the running container:
docker exec -it dev-container bashFor example, install a package:
apt update
apt install -y nginxThen exit:
exitThe container now has changes that were not part of the original image.
ubuntu image
│
│ docker run
▼
dev-container
│
├── Install Nginx
├── Add files
└── Change configuration🖼️ Step 3: Create an Image From the Container
Use:
docker commit dev-container dev-image:v1Syntax
docker commit <container_name> <image_name>:<tag>Command Breakdown
docker commitCreates a new image from a container's changes.
dev-containerThe source container.
dev-imageThe new image repository/name.
v1The image tag.
⚙️ How docker commit Works
Suppose we start with:
ubuntu:latestCreate a container:
ubuntu:latest
│
│ docker run
▼
dev-containerThen make changes:
dev-container
│
├── Install packages
├── Add files
└── Modify configurationCommit the container:
docker commit dev-container dev-image:v1Result:
ubuntu:latest
│
│ docker run
▼
dev-container
│
│ Changes
│
│ docker commit
▼
dev-image:v1The new image can now be used to create other containers.
🔍 Step 4: Verify the New Image
List Docker images:
docker imagesor:
docker image lsExample:
REPOSITORY TAG IMAGE ID CREATED SIZE
dev-image v1 abc123def456 10 seconds ago 180MB
ubuntu latest def456abc123 ... ...The important entry is:
dev-image:v1🚀 Step 5: Test the New Image
Create a new container from the committed image:
docker run -it dev-image:v1 bashYou can then verify that your previous changes exist.
For example:
nginx -vIf Nginx was installed before the commit, it should also exist in containers created from the new image.
🏷️ Image Naming Format
Docker images commonly follow:
<repository>:<tag>For example:
dev-image:v1where:
Repository → dev-image
Tag → v1Other examples:
myapp:v1
myapp:v2
myapp:latest
nginx:custom
ubuntu:configuredIf no tag is specified, Docker uses:
latestFor example:
docker commit dev-container dev-imageis effectively tagged as:
dev-image:latest📝 Optional: Add Commit Information
You can include an author:
docker commit -a "Developer" dev-container dev-image:v1You can also add a commit message:
docker commit \
-a "Developer" \
-m "Install nginx" \
dev-container \
dev-image:v1This provides additional metadata about why the image was created.
🆚 docker commit vs Dockerfile
docker commit is useful for quick snapshots and learning, but for normal development and production environments, a Dockerfile is usually preferred.
Docker Commit
Container
↓
Manual changes
↓
docker commit
↓
ImageAdvantages:
Disadvantage:
Dockerfile
Dockerfile
↓
docker build
↓
ImageExample:
FROM ubuntu:latest
RUN apt update && apt install -y nginxThen:
docker build -t dev-image:v1 .A Dockerfile is better for production because the image creation process is documented and repeatable.
⚠️ Important Notes
⚙️ Complete Workflow
Check containers:
docker ps -aCreate the image:
docker commit container_name image_name:tagVerify:
docker image lsTest the new image:
docker run -it image_name:tag sh🧪 Validation Checklist
📌 Summary
The core command is:
docker commit <container_name> <image_name>:<tag>Example:
docker commit dev-container dev-image:v1The complete flow is:
Existing Image
│
│ docker run
▼
Container
│
├── Install packages
├── Modify files
└── Configure application
│
│ docker commit
▼
New Docker Image
│
│ docker run
▼
New ContainerThe key takeaway is:
> docker commit captures a container's filesystem changes and creates a new reusable Docker image from them. For repeatable builds, prefer a Dockerfile and docker build.