The most common way to build an image in Docker is to use Dockerfile. A Dockerfile is a text file used to build an image. The text content contains the instructions and instructions required to build the image. The official document: docs.docker.com/engine/refe…

  

Dockerfile common directives

  

FROM

  

Syntax: FROM :

Specifies from which base image the new image to build is from, and if tag is not selected, the default is Latest.

FROM centos:7
Copy the code

If it is not based on any image, it is written: FROM scratch. Official note: Scratch image is an empty image that can be used to build busyBox and other ultra-small images. You can really build your own image from scratch.

  

MAINTAINER(deprecated)

  

Syntax: MAINTAINER

Specifies the image maintainer and his contact information (usually a email address). The official instructions are out of date and LABEL is recommended.

MAINTAINER mrhelloworld <[email protected]>
Copy the code

  

LABEL

  

Grammar: LABEL < key > = < value > < key > = < value > < key > = < value >…

The function is to specify a label for the image. You can also use LABEL to specify the mirror author.

LABEL maintainer="mrhelloworld.com"
Copy the code

  

RUN

  

Syntax: RUN

Shell commands run when building an image, such as a new image we want to create a Java directory under /usr/local.

RUN mkdir -p /usr/local/java
Copy the code

  

ADD

  

Syntax: ADD < SRC >…

Copy files or directories to the image. SRC can be a local file or a local compressed file, which is automatically decompressed. It can also be a URL, if SRC is written as a URL, then ADD is like the wget command, which automatically downloads and unzips.

The ADD JDK - 11.0.6 _linux - x64_bin. Tar. Gz/usr/local/JavaCopy the code

  

COPY

  

Syntax: COPY < SRC >…

Copy files or directories to the image. Use the same as ADD, except that it does not support automatic download and decompression.

COPY the JDK - 11.0.6 _linux - x64_bin. Tar. Gz/usr/local/JavaCopy the code

  

EXPOSE

  

EXPOSE [ / …]

Expose the container’s runtime listening port to the outside world. You can specify whether the port listens for TCP or UDP. If no protocol is specified, the default is TCP.

EXPOSE 80 443 8080/tcp
Copy the code

If you want the container to map to the host port, you must add the -p parameter when the container is started.

  

ENV

  

Syntax: ENV


Add a single, ENV

=

… Add multiple.



Set the container environment variables.

ENV JAVA_HOME/usr/local/Java/JDK - 11.0.6 /Copy the code

  

CMD

  

Grammar:

  • CMD ["executable","param1","param2"], such as:CMD ["/usr/local/tomcat/bin/catalina.sh", "start"]
  • CMD ["param1","param2"] , such as:CMD [ "echo", "$JAVA_HOME" ]
  • CMD command param1 param2, such as:CMD echo $JAVA_HOME

The Shell command executed when the container is started. There can be only one CMD directive in a Dockerfile. If you set multiple CMD entries, only the last CMD entry takes effect.

CMD ehco $JAVA_HOME
Copy the code

If a command is specified when creating the container, the CMD command is replaced. If the image is called centos:7, create the container with the following command: Docker run-it –name centos7 centos:7 echo “helloWorld” docker run-it –name centos7 centos:7 /bin/bash, The $JAVA_HOME environment variable is not printed because CMD is overwritten by echo “helloworld”, /bin/bash.

  

ENTRYPOINT

  

Grammar:

  • ENTRYPOINT ["executable", "param1", "param2"], such as:ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh", "start"]
  • ENTRYPOINT command param1 param2, such as:ENTRYPOINT ehco $JAVA_HOME

Shell commands executed when starting containers, like CMD, are not overridden by arguments specified on the Docker run command line. There can only be one ENTRYPOINT directive in a Dockerfile. If multiple EntryPoints are configured, only the last ENTRYPOINT takes effect.

ENTRYPOINT ehco $JAVA_HOME
Copy the code
  • If both ENTRYPOINT and CMD are written to a Dockerfile, and CMD is not a complete executable command, the contents specified by CMD will be taken as arguments to ENTRYPOINT.
  • If ENTRYPOINT and CMD are both written in a Dockerfile, and CMD is a complete directive, then the two overwrite each other and whichever takes effect last

  

WORKDIR

  

Syntax: WORKDIR /path/to/ WORKDIR

Set the working directories for RUN, CMD, ENTRYPOINT, AND COPY AND AND.

WORKDIR /usr/local
Copy the code

  

VOLUME

  

Specify a container mount point to a directory or other container automatically generated by the host. Generally, data needs to be stored persistently.

#The /var/lib/mysql directory of the container is automatically mounted as an anonymous volume during runtime. The anonymous volume resides in the /var/lib/docker-volumes directory of the host
VOLUME ["/var/lib/mysql"]
Copy the code

This is not normally used in dockerfiles, but is more commonly used to specify data volumes during Docker run with -v.

  

Build the mirror

  

After the Dockerfile file is written, you need to use the docker build command to actually build the image.

The docker build command is used to create an image using Dockerfile.

#Create the image using the Dockerfile of the current directory
docker build -t mycentos:7 .
#Create the image from the location of the -f Dockerfile file
docker build -f /usr/local/dockerfile/Dockerfile -t mycentos:7 .
Copy the code
  • -f: specifies the Dockerfile path to use.
  • --tag, -t: The name and label of the image. Multiple labels can be set for an image in a single build.

  

About understanding

  

When using the docker build command to build an image, we often see the command end with a. Number. What does it mean?

-f specifies the path to the Dockerfile file. -f specifies the path to the Dockerfile file. What is it used for?

Docker is divided into Docker engine (server daemon) and client tools at runtime. We use a variety of Docker commands daily, which is actually using client tools to interact with Docker engine.

When we use the Docker build command to build an image, the build process is actually done in the Docker engine, not in the native environment. If you use ADD and other commands to manipulate files in Dockerfile, how do you make the Docker engine get these files?

Here is the concept of an image construction context. When building, the user specifies the context path for image construction, and Docker build will package all files in this path and upload them to the Docker engine. After the engine expands these contents, the files in the context can be obtained.

  

/usr/local/dockerfile = /usr/local/ Dockerfile = /usr/local/ Dockerfile = /usr/local/ Dockerfile

The ADD JDK - 11.0.6 _linux - x64_bin. Tar. Gz/usr/local/JavaCopy the code

The command to build the image would look like this:

docker build -f /usr/local/dockerfile/Dockerfile -t mycentos:7 /root
Copy the code

  

/usr/local/ Dockerfile /usr/local/ Dockerfile /usr/local/ Dockerfile /usr/local/ Dockerfile

The ADD JDK - 11.0.6 _linux - x64_bin. Tar. Gz/usr/local/JavaCopy the code

The command to build the image looks like this:

docker build -f /usr/local/dockerfile/Dockerfile -t mycentos:7 .
Copy the code

  

Dockerfile practice

  

Mycentos :7 = mycentos:7 = mycentos:7 = mycentos:7

Create a directory.

mkdir -p /usr/local/dockerfile
Copy the code

Write a Dockerfile file.

vi Dockerfile
Copy the code

Dockerfile contains the following contents:

#Specifies that the new image to be built is from the centos:7 base image
FROM centos:7
#The author information is declared by the mirror tag
LABEL maintainer="mrhelloworld.com"
#Setting the Working Directory
WORKDIR /usr/local
#After the new image is successfully built, the specified directory is created
RUN mkdir -p /usr/local/java && mkdir -p /usr/local/tomcat
#Copy the file to the image and unzip itADD JDK -11.0.6_linux-x64_bin.tar.gz /usr/local/java ADD apache-tomcat-9.0.37.tar#Exposes the container runtime 8080 listening port to the outside world
EXPOSE 8080
#Set the JAVA_HOME environment variable in the containerENV JAVA_HOME /usr/local/java/jdK-11.0.6/env PATH $PATH:$JAVA_HOME/bin#Start Tomcat when the container is startedCMD ["/usr/local/tomcat/apache tomcat - 9.0.37 / bin/catalina. Sh ", "run"]Copy the code

Build the image.

[root@localhost ~]# docker build -f /usr/local/dockerfile/Dockerfile -t mycentos:7 /root/ Sending build context to Docker Daemon 191.4MB Step 1/10: FROM centos:7 ---> 7e6257c9f8d8
Step 2/10 : LABEL maintainer="mrhelloworld.com"
 ---> Running in 3f18aa4f3fb2
Removing intermediate container 3f18aa4f3fb2
 ---> 7364f68ca4ab
Step 3/10 : WORKDIR /usr/local
 ---> Running in d9889152cfc4
Removing intermediate container d9889152cfc4
 ---> d05bd2e09fa4
Step 4/10 : RUN mkdir -p /usr/local/java && mkdir -p /usr/local/tomcat
 ---> Running in 3bcd6ef78350
Removing intermediate container 3bcd6ef78350
 ---> 4832abf9d769Step 5: ADD JDK -11.0.6_linux-x64_bin.tar.gz /usr/local/java ---> e61474bf7a76ADD apache-tomcat-9.0.37.tar.gz /usr/local/tomcat ---> 7110cdff7438
Step 7/10 : EXPOSE 8080
 ---> Running in a4731c1cf77d
Removing intermediate container a4731c1cf77d
 ---> f893cefee00cENV JAVA_HOME /usr/local/java/jdK-11.0.6 / ---> Running in f0cb08f390db
Removing intermediate container f0cb08f390db
 ---> ff9f6acf6844
Step 9/10 : ENV PATH $PATH:$JAVA_HOME/bin
 ---> Running in eae88cf841d0
Removing intermediate container eae88cf841d0
 ---> 4b9226a23b10CMD ["/usr/local/tomcat/apache-tomcat-9.0.37/bin/catalina.sh", "run"] ---> Running in ccf481045906
Removing intermediate container ccf481045906
 ---> 9ef76a16441b
Successfully built 9ef76a16441b
Successfully tagged mycentos:7
Copy the code

  

Mirror Build History

  

Docker history image name: tags | ID docker history mycentos: 7Copy the code

  

Create containers using the image you build

  

#Create a container
docker run -di --name mycentos7 -p 8080:8080 mycentos:7
#Into the container
docker exec -it mycentos7 /bin/bash
#Test Java environment variables
[root@dcae87df010b /]# java -version
java version "11.0.6" 2020-01-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.6+8-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.6+8-LTS, mixed mode)
#Visit http://192.168.10.10:8080/ to see the page that environment is OK!
Copy the code

Dockerfile to build the image of the way you also learned, continue to learn about the Docker image backup recovery migration, go ~

This article is licensed under a Creative Commons attribution – Noncommercial – No Deductive 4.0 International license.

You can check out more Docker articles in the category below.

  

🤗 your likes and retweets are the biggest support for me.

📢 Scan code pay attention to Mr. Hallward “document + video” each article is equipped with a special video explanation, learning more easily oh ~