Building an docker image using Dockerfile
- 2021-10-27 09:52:57
- OfStack
1. What is Dockerfile? 2. Execute an Dockerfile to build a mirror image
Today we look at how Dockerfile was created.
There are two ways to build an docker image:
1, 1 is using the docker commit command
2. The other one is based on docker build command and dockerfile file
In general, dockerfile mirroring is more flexible than the docker commit command, so the latter is used more often.
Building an docker image using Dockerfile
1. What is Dockerfile?
dockerfile is a file, which is written using the DSL syntax, and then a new image is built using the docker build command based on the instructions in the dockerfile file.
Suppose our code is:
mkdir test
cd test
touch Dockerfile
It is not difficult to see that, We created an test directory, And enter the directory to create the dockerfile_test file, This directory is called our build environment. Docker calls this environment context (context) or build context. Docker uploads the build context and the files and directories in the context to the Docker daemon when building the image, so that the Docker daemon can directly access any code, files or other data you store in the image.
Let's look at the contents of an Docker file:
# version: 0.0.1
FROM ubuntu:14.04
MAINTAINER Yeyz 'yeyz@example.com'
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Hi, I am your container' > /root/test.html
EXPOSE 80
It is not difficult to see from the contents of the above file that Dockerfile contains a series of commands, each of which needs to be identified with an uppercase keyword before it. Content that begins with # is recognized as a comment.
Docker generally executes the commands in Dockerfile in the following order,
1. Docker runs a container from the basic image. The first command of each DockerFile should be From. From specifies a basic image, and subsequent instructions are executed on this basis.
2. Execute one instruction to modify the container
3. Perform an operation similar to docker commit and submit a new mirror layer
4. The Docker image runs a container based on the image just submitted
5. Execute the next instruction in Dockerfile until all container instructions are executed
In the above process, the failure of any one step means that the creation of the target image fails. However, because the target image is the result of stacking one image after another, we actually get a running image, but we have not yet reached the final image. This feature is very important for debugging the image.
Let's explain the above mirror image:
Version: For comments, for versions
From instruction, From specifies a basic image ubuntu, indicating that our operation is carried out on the basic image ubuntu
Maintainer directive, which represents the author of the mirror and the author's e-mail address
RUN instruction and RUN instruction will run the specified command in the current image, and three instructions are updating apt warehouse, installing nginx package and printing one command to the specified file. Each command creates a mirror layer. If the command succeeds, the mirroring layer commits and continues to specify the following 1 directive in DockerFile. By default, the RUN command uses the/bin/bash-c in shell to execute the following instructions.
Expose directive, which tells the application within the container to use the specified port of the container. Of course, you can use multiple EXPOSE to expose multiple ports to the outside world.
2. Execute an Dockerfile to build a mirror image
We executed the Dockerfile just now and got the following results:
[root test]# docker build -t='yeyz:test0' .
Sending build context to Docker daemon 2.048 kB
Step 1/6 : FROM ubuntu:14.04
Trying to pull repository docker.io/library/ubuntu ...
14.04: Pulling from docker.io/library/ubuntu
2e6e20c8e2e6: Downloading [> ] 539.1 kB/70.69 MB
95201152d9ff: Download complete
5f63a3b65493: Download complete
Where the-t option is used to specify the image and repository name, yeyz is the repository name, and test0 is the image name. The. in the command represents the current directory. Docker defaults to looking for DockerFile from the local directory, or it can be replaced by a specified Git repository source address to specify the location of DockerFile. As follows:
docker build -t='yeyz/test0' xxxx.com:yeyz/test0
If you want to preserve version information, you can specify 1 label, as follows:
docker build -t='yeyz/test0:v1' .
If we only want to upload some files under a certain directory, we can create a file with. dockerignore, and every line is a file filtering matching pattern, so that when generating mirrors, those unnecessary files will be automatically filtered out.
The final return value is a mirror ID. After we get the mirror ID, we can run the mirror using the docker run command.
3. Dockerfile Build Cache
When there is a problem with the Dockerfile we built for the first time, we need to build it again. At this time, the same instruction will be executed. Assuming that we modify the content of line 4 in Dockerfile, Docker will automatically load the first three lines by caching, which will save a lot of time.
If we do not want to use the Dockerfile cache, we can avoid using the Dockerfile cache by adding the--no-cache parameter.
4. View the build history of the mirror
docker history allows you to view the build history of the built image and each layer of the built image.
[root@VM-16-13-centos test]# docker images mysql
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest 4f1413420360 7 weeks ago 545 MB
[root@VM-16-13-centos test]# docker history 4f1413420360
IMAGE CREATED CREATED BY SIZE COMMENT
4f1413420360 7 weeks ago /bin/sh -c #(nop) CMD ["mysqld"] 0 B
<missing> 7 weeks ago /bin/sh -c #(nop) EXPOSE 3306 33060 0 B
<missing> 7 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["docker-ent... 0 B
<missing> 7 weeks ago /bin/sh -c ln -s usr/local/bin/docker-entr... 34 B
<missing> 7 weeks ago /bin/sh -c #(nop) COPY file:f9202f6b715c0e... 13.1 kB
<missing> 7 weeks ago /bin/sh -c #(nop) COPY dir:2e040acc386ebd2... 1.12 kB
<missing> 7 weeks ago /bin/sh -c #(nop) VOLUME [/var/lib/mysql] 0 B
<missing> 7 weeks ago /bin/sh -c { echo mysql-community-server... 410 MB
<missing> 7 weeks ago /bin/sh -c echo "deb http://repo.mysql.com... 55 B
<missing> 7 weeks ago /bin/sh -c #(nop) ENV MYSQL_VERSION=8.0.2... 0 B
<missing> 7 weeks ago /bin/sh -c #(nop) ENV MYSQL_MAJOR=8.0 0 B
<missing> 7 weeks ago /bin/sh -c set -ex; key='A4A9406876FCBD3C... 2.61 kB
<missing> 7 weeks ago /bin/sh -c apt-get update && apt-get insta... 52.2 MB
<missing> 7 weeks ago /bin/sh -c mkdir /docker-entrypoint-initdb.d 0 B
<missing> 7 weeks ago /bin/sh -c set -eux; savedAptMark="$(apt-... 4.17 MB
<missing> 7 weeks ago /bin/sh -c #(nop) ENV GOSU_VERSION=1.12 0 B
<missing> 7 weeks ago /bin/sh -c apt-get update && apt-get insta... 9.34 MB
<missing> 7 weeks ago /bin/sh -c groupadd -r mysql && useradd -r... 329 kB
<missing> 7 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0 B
<missing> 7 weeks ago /bin/sh -c #(nop) ADD file:d2abb0e4e7ac177... 69.2 MB
These are the details of using Dockerfile to build an docker image. For more information about using Dockerfile to build an docker image, please pay attention to other related articles on this site!