Docker image creation Dockerfile and commit operations

Docker image creation Dockerfile and commit operations

Build the image

There are two main ways to build an image:

Use the docker commit command to commit the running container to an image;

Use the docker build command to build an image from a Dockerfile.

First, let's introduce how to submit an image from a running container. I still use the busybox image as an example. Use the following command to create a container named busybox and enter the busybox container.

$ docker run --rm --name=busybox -it busybox sh

After executing the above command, the current window will start a busybox container and enter the container. In the container, execute the following command to create a file and write its contents:

/ # touch hello.txt && echo "I love Docker. " > hello.txt

At this point, a hello.txt file has been created in the root directory of the container, and "I love Docker." has been written into it.

Next, we open another command line window and run the following command to submit the image:

$ docker commit busybox busybox:hello

sha256:cbc6406aaef080d1dd3087d4ea1e6c6c9915ee0ee0f5dd9e0a90b03e2215e81c

Then use the docker image ls command mentioned above to view the image:

$ docker image ls busybox
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox hello cbc6406aaef0 2 minutes ago 1.22MB
busybox latest 018c9d7b792b 4 weeks ago 1.22MB

At this point we can see that a new busybox:hello image has been created on the host.

By comparison, it is obvious that Docker build using Dockerfile is better. The disadvantages of docker commit are as follows:

The operation needs to be done inside the container, which is troublesome and inefficient.

This is also the most important point. Others or even yourself after a period of time will not know how this image was made, but using the image built by Dockerfile, we can see that the apt-get install command was executed.

The second method is the most important and most commonly used image building method: Dockerfile. Dockerfile is a text file that contains all the user's build commands. The docker build command can be used to generate an image from a Dockerfile.

Using Dockerfile to build an image has the following features:

Each line of command in Dockerfile will generate an independent image layer with a unique ID

The commands in Dockerfile are completely transparent. By viewing the contents of Dockerfile, you can see how the image is built step by step.

Dockerfile is in plain text, which makes it easy to store it in the code repository along with the code and manage its version.

Seeing that using Dockerfile to build images has so many good features, are you eager to know how to use it? Don't worry, let's first learn the common instructions of Dockerfile.

Dockerfile instructions Instruction Introduction
FROM In addition to the comments, the first line of the Dockerfile must be FROM, followed by the image name, which represents the base image we want to build our container based on.
RUN RUN is followed by a specific command, similar to the Linux command line execution command.
ADD Copy local files or remote files to the image
COPY Copy local files to the image
USER Specify the user to start the container
ENTRYPOINT Container startup command
CMD CMD provides default parameters for the ENTRYPOINT instruction. You can also use CMD alone to specify container startup parameters.
ENV Specifies the environment variables when the container is running, in the format of key=value
ARG Define external variables. When building an image, you can use the build-arg = format to pass parameters for building.
EXPOSE Specify the port that the container listens on, in the format of [port]/tcp or [port]/udp
WORKDIR Sets the working directory for all RUN, CMD, ENTRYPOINT, COPY, and ADD commands that follow it in the Dockerfile.

After reading so many instructions, feel a little confused? Don't worry, I'll walk you through an example to familiarize you with them. Here is a Dockerfile:

FROM centos:7 
COPY nginx.repo /etc/yum.repos.d/nginx.repo 
RUN yum install -y nginx 
EXPOSE 80 
ENV HOST=mynginx 
CMD ["nginx","-g","daemon off;"]

The first line indicates that I want to build a custom image based on the centos:7 image. It should be noted here that the first line of each Dockerfile must start with FROM except for comments.

The second line means copying the local file nginx.repo to the /etc/yum.repos.d directory in the container. The nginx.repo file is copied here to add the installation source of nginx.

The third line indicates that the yum install -y nginx command is run in the container to install the nginx service in the container. After executing the third line of command, nginx in the container has been installed.

The fourth line declares that the service (nginx) in the container uses port 80 to provide external services.

The fifth line defines the environment variable HOST=mynginx when the container is started. After the container is started, the value of the environment variable HOST can be obtained as mynginx.

The sixth line defines the startup command of the container, and the command format is a json array. Here, the container startup command is set to nginx, and the nginx startup parameter -g 'daemon off;' is added to start nginx in the foreground.

Implementation principle of mirroring

In fact, a Docker image is composed of a series of image layers, each of which represents a submission in the image building process. The following uses a Dockerfile built from an image to illustrate how images are layered.

FROM busybox

COPY test /tmp/test

RUN mkdir /tmp/testdir

The Dockerfile above consists of three steps:

The first line creates an image layer based on busybox;

The second line copies the local test file to the image;

The third line creates a directory testdir in the /tmp folder.

Here my Docker uses the overlay2 file driver. Go to the /var/lib/docker/overlay2 directory and use the tree . command to view the generated image file:

$ tree .
 
# The following is the output of the tree . command |-- 3e89b959f921227acab94f5ab4524252ae0a829ff8a3687178e3aca56d605679
 
| |-- diff # This layer is the base layer, corresponding to the first line of the above Dockerfile, containing all the file contents of the busybox image, such as /etc, /bin, /var and other directories... This time, some of the original image file contents are omitted| `-- link 
 
|-- 6591d4e47eb2488e6297a0a07a2439f550cdb22845b6d2ddb1be2466ae7a9391
 
| |-- diff # This layer corresponds to the second line of the above Dockerfile, copying the test file to the /tmp folder, so there is a /tmp/test file in the diff folder| | `-- tmp
 
| | `-- test
 
| |-- link
 
| |-- lower
 
| `-- work
 
|-- backingFsBlockDev
 
|-- bec6a018080f7b808565728dee8447b9e86b3093b16ad5e6a1ac3976528a8bb1
 
| |-- diff # This layer corresponds to the third line of the above Dockerfile. The testdir folder is created under the /tmp folder, so there is a /tmp/testdir folder under the diff folder. | | `-- tmp
 
| | `--testdir
 
| |-- link
 
| |-- lower
 
| `-- work
 
...

From the above directory structure, we can see that each line of command in Dockerfile generates a mirror layer, and the diff folder of each layer only stores incremental data, as shown in Figure 2.

The layered structure makes the Docker image very lightweight. Each layer has a unique ID value based on the content of the image. When different images have the same image layer, the image layer can be shared between different images.

To summarize, a Docker image is a static, hierarchically managed combination of files, and the underlying implementation of the image relies on the Union File System (UnionFS). Fully understanding the principles of images can help us build the best images in production practice, and can also help us better understand the relationship between containers and images.

Summarize

At this point, I believe you have a deeper understanding of the core concept of Docker images, and are familiar with the common operations of Docker images (pulling, viewing, "renaming", deleting, and building custom images) and the underlying implementation principles.

Mirror operation command:

Pull the image, use the docker pull command to pull the image of the remote warehouse to the local;

Rename the image, use the docker tag command to "rename" the image;

To view the image, use the docker image ls or docker images command to view the existing images locally;

Delete the image and use the docker rmi command to delete useless images;

Build an image. There are two ways to build an image. The first way is to use the docker build command to build an image based on the Dockerfile, which is also the image building method I recommend; the second way is to use the docker commit command to submit an image based on an already running container.

The implementation principle of mirroring:

An image is composed of a series of image layers. Each layer represents a commit in the image building process. When we need to modify a file in the image, we only need to create a new image layer based on the current image layer and only store the modified file content. The layered structure makes it very simple and convenient to share image layers between images.

The above article about Docker image creation Dockerfile and commit operations is all I have to share with you. I hope it can give you a reference, and I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Submit the image through the container DockerCommit and push the image DockerPush
  • Detailed explanation of the working principle and usage of the Docker image submission command commit
  • Docker learning notes: How to commit a container to an image
  • Detailed explanation of Docker modifying existing images (commit)
  • Detailed explanation of Docker learning to create an image using the commit command
  • Docker image commit operation example and function

<<:  Detailed explanation of Javascript Echarts air quality map effect

>>:  Detailed description of the function of new in JS

Recommend

Mybatis mysql delete in operation can only delete the first data method

Bugs As shown in the figure, I started to copy th...

How to detect if the current browser is a headless browser with JavaScript

Table of contents What is a headless browser? Why...

MySQL Query Cache and Buffer Pool

1. Caches - Query Cache The following figure is p...

The table merges cells and the img image to fill the entire td HTML

Source code (some classes deleted): Copy code The ...

Solve the problem of forgetting password in MySQL 5.7 under Linux

1. Problem Forgot password for mysql5.7 under lin...

MySQL 8.0 upgrade experience

Table of contents Preface 1. First completely uni...

MySQL 5.7.18 winx64 installation and configuration method graphic tutorial

The installation of compressed packages has chang...

Implementation of docker-compose deployment of zk+kafka+storm cluster

Cluster Deployment Overview 172.22.12.20 172.22.1...

Native JavaScript to achieve slide effects

When we create a page, especially a homepage, we ...

MySQL establishes efficient index example analysis

This article uses examples to describe how to cre...

JavaScript to achieve balance digital scrolling effect

Table of contents 1. Implementation Background 2....

Let’s talk in detail about how JavaScript affects DOM tree construction

Table of contents Document Object Model (DOM) DOM...