preface
The previous chapter has introduced the three cores of Docker in detail, including image, container and warehouse. We use Dockerfile to define images and rely on images to run containers, so Dockerfile is the key to images and containers. Dockerfile is one of the skills you must learn if you are building custom images. This article introduces dockerfile in detail and how to use dockerfile to build an image.
knowledge
Dockerfile concept
Dockerfile is a build file used to build a Docker image. It is a script composed of a series of commands and parameters. A Dockerfile is a text file containing instructions, each instruction builds a layer, so the content of each instruction describes how the layer should be built.
Dockerfile benefits
(1) Easy versioning management, Dockerfile itself is a text file, convenient to store in the code warehouse for version management, but also convenient to find the history of changes between various versions.
(2) no need to consider the environment is easy to build, using Dockerfile to build images do not need to consider the build environment, based on the same Dockerfile, no matter where run, build results are consistent.
(3) The process can be traced. Each line of instructions in Dockerfile represents a mirror layer. According to the content of Dockerfile, the complete construction process of the image can be clearly viewed.
Dockerfile writing principles
(1) Single responsibility. The essence of a container is a process. Applications with different functions should be divided into different containers as far as possible, and each container is only responsible for one business process.
(2) Keep containers to a minimum and should avoid installing useless packages.
(3) Reasonable selection of the base image, the core of the container is the application, so as long as the base image can meet the running environment of the application.
(4) Try to use the build cache, using the cache, can greatly shorten the image build time.
(5) Minimize the number of mirror layers and reduce the number of Dockerfile instruction lines as much as possible when building the image.
Tip: In addition to the above guidelines, you should also provide comment information, set the time zone correctly, use.gitignore files and ignore files that do not require versioning. Learning these guidelines will help you write the best dockerfiles.
operation
DockerFile**** build process
(1) Docker runs a container from the base image
(2) Execute an instruction and make changes to the container
(3) Commit a new image layer by performing operations similar to Docker Commit
(4) Docker then runs a new container based on the image just submitted
(5) Execute the next instruction in dockerfile until all instructions are executed!
Tip: DockerFile, Docker image and Docker container represent three different stages of software respectively. DockerFile is the raw material of the software (code), Docker image is the deliverable of the software (.APk), and Docker container is the running state of the software (customers download, install and execute).
DockerFile * * * * command
Key words:
FROM # base mirror, which mirror is the current new mirror based on
MAINTAINER # Name of the image MAINTAINER mixed email address
RUN # The command to RUN when the container is built
ADD # copies files from the host directory into the image and the ADD command automatically processes the URL and decompresses the tar
COPY # Similar to ADD, COPY files and directories to the image!
VOLUME # Container data VOLUME, used for data preservation and persistence
CMD # specifies the command to run when a container is started. DockerFile can have multiple CMD directives, but only the most
The last one works!
EXPOSE # The port the current container keeps open to the outside world
WORKDIR # Specifies the working directory that the terminal logs in to by default after creating the container, a landing point
ENV # is used to set environment variables during image building
ENTRYPOINT # specifies a command to run when the container is started! Like CMD
ONBUILD # Run the command when building an inherited DockerFile. After the parent image inherits from the quilt image, the parent image’s ONBUILD is triggered
DockerFile of actual combat
Check the official default CentOS:
Purpose: To make our own image have the following: default path after login, support vim editor, ifconfig command
(1) Create a docker-test-volume folder
mkdir docker-test-volume
(2) Write DockerFile file
[root@daxiong home]# cd docker-test-volume/
[root@daxiong docker-test-volume]# vim dockerfile-centosFROM centos MAINTAINER daxiong<123456:@qq.com> ENV MYPATH /usr/local/workdir MYPATH\ RUN yum -y install vim # EXPOSE 80 CMD echo MYPATH CMD echo “———-end——–” CMD /bin/bash
(3) Create an image after build, and obtain a new image.
Docker build -f dockerfile address -t New image name :TAG.
docker build -f dockerfile-centos -t mycentos:test .
Tip: be sure to note that there is a. Dot behind, after executing the command, it will put the docker file configuration installation package, are installed
(4) Start container
Docker runit new image name :TAG
docker run -it mycentos:test
You can see that the default path is changed and the vim/ifconfig commands are now available.
Tip: We can also use the Docker history image name command to see the corresponding history steps.
The Dockerfile directive writes suggestions
(1) When using the RUN command, the content should be sorted alphabetically. If the content is complex, it is recommended to use the backslash (\) end and line break.
(2) Both CMD and ENTRYPOINT commands are command entry for container running, Dockerfifile can have multiple CMD commands, but only the last one takes effect, CMD will be replaced by the parameter after docker run! The parameters after docker run will be passed to ENTRYPOINT as parameters, and then form a new command combination! CMD is recommended if you want your image to be flexible.
(3) The function of ADD and COPY commands is similar. They both ADD files from the outside to the container. The COPY directive only supports basic file and folder copying, while ADD supports more file source types. The COPY directive is recommended because it is more transparent and only allows local files to be copied to the container.
conclusion
Through this article, you will have a deep understanding of the writing principles, common commands, and important commands of Dockerfile.
If this blog has been helpful to you, please leave a comment + like + favorites. Welcome to the docker learning journey!