Skip to content

Docker Level 2, Task 3: Creating an Image from a Container

Today's task introduced me to a powerful and practical Docker feature: creating a new image directly from a running container. This is like taking a snapshot of a container's current state and saving it as a reusable blueprint. It's a fantastic tool for debugging and for capturing a specific configuration that was set up manually.

I learned about the docker commit command and, more importantly, when to use it versus the more standard Dockerfile approach. It's a great skill to have for quick, ad-hoc image creation.

Table of Contents


The Task

My objective was to create a new Docker image on App Server 1. The requirements were: 1. The new image must be named ecommerce:xfusion. 2. It had to be created from the current state of a running container named ubuntu_latest.


My Step-by-Step Solution

The process was straightforward and performed entirely on App Server 1.

Step 1: Connect to the Server

First, I logged into App Server 1 as the tony user.

ssh tony@stapp01

Step 2: Verify the Source Container

Before starting, I wanted to make sure the source container was actually running. This is a good practice to avoid errors.

sudo docker ps
The output confirmed that the ubuntu_latest container was up and running.

Step 3: Create the Image from the Container

This was the core of the task. I used the docker commit command to create the new image.

sudo docker commit ubuntu_latest ecommerce:xfusion
The command returned the unique SHA256 hash of the new image, which was my first sign of success.

Step 4: Verification

The final and most important step was to confirm that my new image existed. I used the docker images command to list all images on the server.

sudo docker images
The output clearly showed my new image at the top of the list, proving the task was completed successfully.
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
ecommerce           xfusion   b1c2d3e4f5a6   A few seconds ago  ...
ubuntu              latest    ...            ...              ...
...


Why Did I Do This? (The "What & Why")

- docker commit: This is the key command for this task. It takes a container's current state—including any changes made to its filesystem after it was started—and creates a new image from it. It's essentially a "snapshot" of the container's writable layer.

  • Use Cases for docker commit: While building images with a Dockerfile is the standard for production, docker commit is incredibly useful in specific scenarios:
    1. Debugging: If a container is having issues, you can use docker exec to get inside, install debugging tools (like vim, curl, net-tools), and then commit that container to a new myapp:debug image. This image, with all its tools, can then be shared with other developers to analyze the problem in the exact same environment.
    2. Saving Manual Configurations: Sometimes you might configure an application inside a container through a series of manual steps. Committing the result saves that state as a reusable image.
    3. Backing Up Container State: It provides a quick way to create a backup of a container's filesystem at a specific point in time.

Deep Dive: docker commit vs. Dockerfile

This task really highlighted the difference between the two ways of creating images.

Feature Dockerfile docker commit
Method Declarative (You declare the steps to build the image in a text file) Imperative (You take an existing, running container and save its state)
Reproducibility High. Anyone with the Dockerfile can build the exact same image. It's version-controlled and self-documenting. This is the production standard. Low. It's a "black box." No one knows how the container got into its current state unless you document it elsewhere. It's difficult to recreate or patch.
Transparency High. The Dockerfile is a clear, step-by-step recipe. Low. The changes are hidden within the image layers. This is often called a "magic" image.
Best For Production builds, CI/CD pipelines, creating base images. Debugging, quick snapshots, saving a manually configured state.

The key takeaway for me: Use a Dockerfile for anything that needs to be automated, reproducible, and maintained over time. Use docker commit for temporary, one-off situations like debugging or saving a specific state for analysis.


Common Pitfalls

- Committing from the Wrong Container: If multiple containers are running, it's easy to accidentally specify the wrong source container name in the docker commit command. Always double-check with docker ps. - Forgetting to Tag: Running sudo docker commit ubuntu_latest without a new name would create an image with no repository or tag (shown as <none>:<none>), making it hard to find and use. - Including Unwanted Data: The docker commit command captures everything in the container's filesystem. If you have created large temporary files or logs inside the container, they will become part of the new image, making it unnecessarily large.


Exploring the Commands Used

- sudo docker ps: Printer Status. Lists all running Docker containers, which I used to verify my source container existed. - sudo docker commit [container] [new_image:tag]: The main command. It takes a snapshot of a running container and saves it as a new, tagged image. - sudo docker images: Lists all the Docker images stored on the local server, which I used to verify that my new image was created successfully.