0% found this document useful (0 votes)
7 views

Lesson 4 Understanding and Creating Docker Image in Depth

This document provides an in-depth understanding of Docker images, explaining their structure as lightweight, executable software packages that include all necessary components for running applications. It covers the concept of UnionFS, which allows for layered file systems, and the advantages of image layering for resource sharing. Additionally, it details methods for creating and modifying Docker images using commands like 'docker commit' and 'Dockerfile'.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lesson 4 Understanding and Creating Docker Image in Depth

This document provides an in-depth understanding of Docker images, explaining their structure as lightweight, executable software packages that include all necessary components for running applications. It covers the concept of UnionFS, which allows for layered file systems, and the advantages of image layering for resource sharing. Additionally, it details methods for creating and modifying Docker images using commands like 'docker commit' and 'Dockerfile'.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lesson 4 Understanding and Creating

Docker Image in Depth

1. Image Understanding
1. An image is a lightweight, executable, and self-sufficient software
package, including all the necessary components for running software. The
application and configurations are packaged into a complete, deliverable, and
deployable running environment that contains code, runtime, required libraries,
environment variables, configuration files, etc, which is the image file.
2. The docker containers can only be instantiated from image files.

2. UnionFS (Union File System)


1. UnionFS is a layered, lightweight, and high-performance file system
that serves as the basis of the docker image. It allows for modifications to the
file system to be added layer by layer and also enables different directories to
be mounted onto the same virtual file system.
2. Images can be built through layering based on a base image to create
various specific application images.
The characteristic of UnionFS is that it can simultaneously load multiple
file systems, while appearing to the outside as a single file system. Union
loading will overlay each file system to ensure that the final file system includes
all layered files and directories.

3. Image Layering
When an image is downloaded, please observe the downloaded log
output. You can see it is downloaded layer by layer.

1
Enter the command “docker image inspect mysql” in the terminal to check
the image layering.

3.1 Layering Understanding


All docker images start at a base image layer. When a modification or new
component is added, a new image layer is created on top of the current image
layer.
Illustrate by a simple example. If a new image is created based on
“debian”, it will be the first layer of the new image. If a Python package is
added to the image, the second image layer will be created on the top of the
base image layer. If a security patch is added, the third image layer will be
2
created on the top of the second layer. The image will be built like this layer by
layer.
All docker images are read-only. When a container is started, a new
writable layer is mounted on the top of the image layers, which is commonly
referred to as the container layer. The layers below it are called image layers.

3.2 Advantage of Layering in Docker Image


The advantage of layering is resource sharing. For example, if there are
multiple images created from the same base image, the host only needs to
keep one copy of the base image on its disk and in its memory. This allows the
base image to be shared among all containers, and each layer of the image
can be shared.

4. Image Creating
When the downloaded images from the docker image repository cannot
meet your requirements, there are two ways to modify them.

① Use “docker commit” to update an image from the created container

and commit the image.

② Use the “Dockerfile” command to create a new image.

4.1 Commit an Image from Container


Command explanation: docker commit -m= “commit message” -a= “author”

container ID target_image_name:[Tag] 【 “-m” and “-a” parameters can be

omitted】

1) Press “Ctrl+Alt+T” to open the command line terminal and enter


“docker ps -a”. It displays all previously run containers, and you need to locate
the container ID to be committed.

3
2) Enter “docker commit 9aeb4cc795e6 debain:1.0” in the terminal to
commit the image of container “9aeb4cc795e6” and name it “debian”. The tag
name is 1.0.

3) Enter “docker images” in the terminal to view the images on the master.
You can see a “debain” has been added to the list of images.

4.2 “dockerfile” Image Creating


Command explanation: docker build “-f dockerfile” means file path and “-t”
is the name of the new image: TAG . # docker build. The final “.” in the
command indicates the current directory.
Use the command “docker build” to create a new image from scratch. As a
result, a “Dockerfile ” file needs to be created, including a set of commands for
Docker on how to build the image.
About the compiling of “dockerfile”, please refer to
“https://docs.docker.com/develop/develop images/dockerfile_best-practices/”.

4
1) Press “Ctrl+Alt+T” to open the command line terminal. Enter “vim
Dockerfile” in the terminal to create and open the file.

2) Type the content shown below in the terminal and press “ESC”. Enter “:
wq” to exit and save the file.

3) Enter “docker build -f dockerfile -t debain:1.1 .” in the terminal to build a


new image.

4) Enter “docker images” in the terminal, then you can see the newly built
5
image.

You might also like