DEV Community

Cover image for Building Docker Images from a Container
Arif Hossain
Arif Hossain

Posted on

Building Docker Images from a Container

Building Docker Images from a Container
Building Docker images from a container involves creating a container from an existing image, making modifications, and then committing those changes to form a new image.

In this lab, we'll learn how to create a Docker image by making changes to a container and committing those changes. We'll start with a simple example where we create a file in a container and then commit this change to form a new image.

How It Works
When we create a Docker container, it uses a Union File-System (UFS) mount to provide its filesystem. Any changes made to the filesystem within the container are written as new layers, which are owned by the container that created them.

To build a new image, we start with an existing image, make changes to it by modifying the container's filesystem, and then commit these changes to form a new image. This new image can then be used to create further containers, encapsulating the changes made.

How UFS Works
Base Layer: This is the original, unchanged filesystem, like a basic Linux operating system.

Layers: Each time you make changes (like installing software or creating files), these changes are saved as new layers on top of the base layer.

Union Mount: The union filesystem merges these layers into a single, cohesive filesystem that the container uses.

Image description

The above figure demonstrates how UFS works.

Task
Create a container from the ubuntu:latest image and modify its filesystem by creating a file named HelloWorld.
Commit the changes made in the container to a new image named hw_image.
Remove the modified container to clean up.
Verify the changes by running a new container from the hw_image and checking the existence of the HelloWorld file.

Image description

Solution
Follow these steps to complete the task:

Create a container and modify its filesystem:

Create a container from the ubuntu:latest image and enter the container bash:

docker run -it --name hw_container ubuntu:latest /bin/bash
Enter fullscreen mode Exit fullscreen mode

Create a file named HelloWorld.txt in the container:

touch HelloWorld.txt
Enter fullscreen mode Exit fullscreen mode

Exit the container:

exit
Enter fullscreen mode Exit fullscreen mode

Commit the changes to a new image:

Here we are creating a new image from our container:

docker container commit hw_container hw_image
Enter fullscreen mode Exit fullscreen mode

Image description

We can see the new image using the following command:

docker images
Enter fullscreen mode Exit fullscreen mode

Expected output:

root@5616d87af86503cb:~/code# docker container commit hw_container hw_image

sha256:80652e499d3e45a71c9b4818ab235a77259d7c662175a3f2aa12fe75fb102999

root@5616d87af86503cb:~/code# docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
hw_image     latest    80652e499d3e   About a minute ago   78.1MB
ubuntu       latest    59ab366372d5   4 weeks ago          78.1MB
Enter fullscreen mode Exit fullscreen mode

Remove the modified container:

Let's delete the existing container:

docker container rm -vf hw_container
Enter fullscreen mode Exit fullscreen mode

Create new Container from the new Image:

Let's create a new container from the new image that we have created and enter the bash in the new container:

docker run -it --name new_hw_container hw_image /bin/bash
Enter fullscreen mode Exit fullscreen mode

Use the following comand to see the files:

ls
Enter fullscreen mode Exit fullscreen mode

Expected output:

Image description

This output confirms that the file HelloWorld was successfully created in the new image, demonstrating that the modifications made in the original container were correctly committed to the new image.

Cleanup
Remove the container and image:

docker rm  new_hw_container
docker rmi hw_image
Enter fullscreen mode Exit fullscreen mode

Top comments (0)