This article has been added to the Index of.NET Core on K8S Learning Practice Series Articles , you can click to see more articles related to container technology.
1. About Dockerfile
The most common way to create mirrors in a Docker is to use a Dockerfile.Dockerfile is a description file of a Docker image, which we can understand as A, B, C, D of a rocket launch.Steps.Dockerfile contains a single instruction that builds a layer, so the content of each instruction is to describe how the layer should be built.
An example of a Dockerfile is shown below:
#centos-based mirroring FROM centos #Maintainer's Information MAINTAINER The CentOS Project <303323496@qq.com> #Install httpd package RUN yum -y update RUN yum -y install httpd #Open port 80 EXPOSE 80 #Copy the site home page file to the mirror under the web site ADD index.html /var/www/html/index.html #Copy the script into the mirror and modify its permissions ADD run.sh /run.sh RUN chmod 775 /run.sh #Script file executed when the container is started CMD ["/run.sh"]
As you can see from the above, the Dockerfile structure is roughly divided into four parts:
(1) Basic mirror information
(2) Maintainer information
(3) Mirror operation instructions
(4) Execute instructions when the container is started.
Dockerfile supports one instruction per line, with multiple parameters per instruction, and supports the use of comments starting with a #.Some of the common instructions used above are described below.
2. Common directives for Dockerfile
2.1 FROM
Specify from which base image the new image is built, for example:
FROM centos:6
2.2 MAINTAINER
Specify how the mirror is maintained and how it is contacted (typically by a mailbox address), for example:
MAINTAINER Edison Zhou <edisonchou@hotmail.com>
2.3 RUN
Shell commands that run when the image is built, for example:
RUN ["yum", "install", "httpd"] RUN yum install httpd
For example, when using Microsoft's official ASP.NET Core Runtime mirror, we often add the following RUN commands to compensate for the inability to use Drawing-related interfaces in the default mirror:
FROM microsoft/dotnet:2.2.1-aspnetcore-runtime RUN apt-get update RUN apt-get install -y libgdiplus RUN apt-get install -y libc6-dev RUN ln -s /usr/lib/libgdiplus.so /lib/x86_64-linux-gnu/libgdiplus.so
2.4 CMD
Shell commands that are executed when the container is started, for example:
CMD ["-C", "/start.sh"] CMD ["/usr/sbin/sshd", "-D"] CMD /usr/sbin/sshd -D
2.5 EXPOSE
Declare the service ports on which the container is running, for example:
EXPOSE 80 443
2.6 ENV
Set environment variables within the environment, for example:
ENV MYSQL_ROOT_PASSWORD 123456 ENV JAVA_HOME /usr/local/jdk1.8.0_45
2.7 ADD
Copy a file or directory into a mirror, for example:
ADD <src>...<dest> ADD html.tar.gz /var/www/html ADD https://xxx.com/html.tar.gz /var/www/html
_PS:_If it is a URL or a compressed package, it will be automatically downloaded or decompressed.
2.8 COPY
Copying files or directories to a mirror is the same as ADD, but automatic download and decompression are not supported, for example:
COPY ./start.sh /start.sh
2.9 ENTRYPOINT
Shell commands executed when starting containers are similar to CMD except that programs started by ENTRYPOINT are not overwritten by parameters specified by the docker run command line, and these command line parameters are passed as parameters to the programs specified by ENTRYPOINT, for example:
ENTRYPOINT ["/bin/bash", "-C", "/start.sh"] ENTRYPOINT /bin/bash -C '/start.sh'
Multiple ENTRYPOINT directives can also exist in the _PS:_Dockerfile file, but only the last one will take effect.
2.10 VOLUME
Specify a container mount point to a directory or other container automatically generated by the host, for example:
VOLUME ["/var/lib/mysql"]
_PS:_is not typically used in Dockerfile s, but it is more common to specify the -v data volume when docker run.
2.11 USER
Specify a running user to execute Shell commands for RUN, CMD, and ENTRYPOINT, for example:
USER <user>[:<usergroup>] USER <UID>[:<UID>] USER edisonzhou
2.12 WORKDIR
Set up working directories for RUN, CMD, ENTRYPOINT, COPY, and AND, for example:
WORKDIR /data
2.13 HEALTHCHECK
Tell Docker how to test the container to see if it is still working, a health check, for example:
HEALTHCHECK --interval=5m --timeout=3s --retries=3 \ CMD curl -f http:/localhost/ || exit 1
Description of some options:
- --interval=DURATION (default: 30s): how often to probe, default 30 seconds
- --timeout= DURATION (default: 30s): Service response timeout, default 30 seconds
- --start-period= DURATION (default: 0s): how long after the service starts to probe, default 0 seconds
- --retries=N (default: 3): think detection failures are downtime several times, default three times
Some explanations of return values:
- 0: Container success is healthy and ready to use
- 1: Unhealthy containers do not work properly
- 2: Retain not using this exit code
2.14 ARG
When building a mirror, specify some parameters, such as:
FROM centos:6 ARG user # ARG user=root USER $user
At this point, we can take the custom parameter user with us in the docker build, as follows:
docker build --build-arg user=edisonzhou Dockerfile .
3. Comprehensive Dockerfile Case
The following is a mirror Dockerfile for a Java Web application that combines several of the most common commands described above:
FROM centos:7 MAINTANIER www.edisonchou.com ADD jdk-8u45-linux-x64.tar.gz /usr/local ENV JAVA_HOME /usr/local/jdk1.8.0_45 ADD apache-tomcat-8.0.46.tar.gz /usr/local COPY server.xml /usr/local/apache-tomcat-8.0.46/conf RUN rm -f /usr/local/*.tar.gz WORKDIR /usr/local/apache-tomcat-8.0.46 EXPOSE 8080 ENTRYPOINT ["./bin/catalina.sh", "run"]
With Dockerfile, you can create a mirror:
docker build -t tomcat:v1 .
Finally, containers can be created using the following commands:
docker run -itd --name=tomcate -p 8080:8080 \ -v /app/webapps/:/usr/local/apache-tomcat-8.0.46/webapps/ \ tomcat:v1
4. Summary
This paper introduces the background and composition of Dockerfile, as well as some of the most commonly used Dockerfile commands, and finally introduces a case that uses the Dockerfile command in a comprehensive way to illustrate the application of Dockerfile.
Reference material
(1) Li Zhenliang, Dockerfile Common Instructions Detailed>
(2)CloudMan,<Play Docker Container Technology 5 minutes a day>
(3) Aron, Dockerfile Details>
(4)MaAiQiang,<Creating a Docker image from a Dockerfile>