Dockerfile details

     Dockerfile is the standard file used to build images. Write the corresponding commands in the Dockerfile according to the Dockerfile specification officially given by docker, and then run the docker build command to build a docker image by yourself. Below are some explanations of some of the main commands. (See the complete command suggestion and go directly to the official website: https://docs.docker.com/engine/reference/builder/ )

FROM :  Initializes a new build phase and sets up a base image for subsequent instructions. So a valid Dockerfile must start with the FROM instruction. The specified image can be any valid image -- and it's very container-friendly by pulling an image from the public repository (if the image isn't available locally, go back to the remote repository and pull it).

RUN: This command has two forms

           1.RUN command param1,param2 .... (command can be any command that can be executed by the shell in the image specified by FROM)

           2. RUN ["executable","param1","param2"]

           RUN is mainly used to perform some necessary operations for making mirrors, such as installing related software, creating directories, moving files, and so on.


CMD : This command is mainly used to provide some default values ​​for a running container, or to pass parameters to the ENTRYPOINT command. There can only be one CMD instruction in the Dockerfile. If you specify multiple, the last one will be executed and the others will be ignored. The default value provided by CMD can be an executable script, or it will also ignore the executable script if you also specify the ENTRYPOINT build directive in the Dockerfile.


EXPOSE : This command informs docker that the container should listen to a specific network port when running. This directive cannot make the specified listening port accessible by the host host. To do this, you can add the -p attribute when starting the container to map some ports inside the container, or -P to map all the ports inside the container. Of course, the port mapped to the host can be completely different from the port inside the container. Docker networking features support creating networks without exposing ports in a network. What does it mean, that is, docker containers in the same network can access each other's internal ports without exposing them. (This can be achieved with the --link parameter, in docker compose 3, it doesn't even need anything special)


ENV : Environment variable settings. Used to set some environment variables, so that after using this image to start the container, it can be used directly. The form is ENV <key> <value>, or ENV <key>=<value>

If you want to build an image without the java environment, you can set the java environment variable through this command.


ADD : The format is ADD <src> .. <dest> where src is the file (folder) of the local machine, and dest is the file (folder) inside the container. This command is mainly used to copy some files of this machine to the in the container generated by the image. (The file or folder specified by src must be in the same directory as the Dockerfile, because when building an image, docker will package all the context where the Dockerfile is located and send it to the docker daemon)


COPY : The format is COPY <src> .. <dest> is the same as the ADD command, but does not support copying files from the url to the container.


ENTRYPOINT : Specifies a command that will be executed when the container starts.


In fact, some of the above instructions have very similar concepts, so it is necessary to make some distinctions here:

1. ADD and COPY

ADD can actually be understood as an upgraded version of COPY. ADD not only supports copying local files to the file system of the container, but also downloads the corresponding files from the specified url path and copies them to the container. Other than that, the two are almost identical. Therefore, the following focuses on the COPY instruction.

The function of the COPY instruction has been introduced before, and will not be described here. But here are some notable things:

NOTE:

      1. The path of src must be in the context of the current build image.

      2. If src is a folder, only the sub-folders (folders) of the folder and the metadata information of the current file system will be copied to the specified directory of the container, but the folder itself will not be included.

      3. If the directory specified by dest does not exist in the container, the corresponding directory will be created automatically.

      4. If src is a file and dest ends with /, then src will be copied to dest/src, otherwise the content of src file will be written to dest file.

      5. If src specifies multiple resource files (such as using wildcards), then dest must be a directory and end with /

2. CMD and ENTRYPOINT

Both CMD and ENTRYPOINT can specify a command to be executed when the container starts. However, the command or executable script specified by CMD will be overwritten by the command passed to the container by the docker run command, while ENTRYPOINT will not.

There are three forms of CMD:

      1.CMD ["executable","param1","param2"] (exec formpreferred)

      2.CMD ["param1","param2"]

      3.CMD command param1 param2 ... (shell form)

The first form specifies an executable script and passes param1, param2 as parameters to the script. One thing to note, this form doesn't invoke the command line interpreter, which means that normal shell processing doesn't happen. For example, CMD ["echo","$HOME"] will directly output $HOME without replacing the environment variable. If you want to achieve the purpose of variable substitution, you can do this: CMD ["sh","-c","echo $HOME"]. In this case, the sh interpreter (instead of docker) will do the work of environment variable substitution , replace $HOME with the current user's home directory. Of course, there is an easier way, which is to use the third form of CMD command directly: CMD echo $HOME

The second form is mainly to pass parameters to the ENTRYPOINT instruction

The third form is called the shell form, which runs the command directly in the shell. And the command it specifies will be executed in /bin/sh -c.

ENTRYPOINT has the following two forms:

       1.ENTRYPOINT ["executable","param1","param2"]   (exec form preferred)

       2.ENTRYPOINT command param1,param2,..   (shell form)

The ENTRYPOINT instruction will run when the container starts, and can accept the parameters (or commands) passed to the container by docker run without being overwritten. Of course, if you specify --entrypoint, you can also override the ENTRYPOINT instruction specified in the Dockerfile.

CMD and ENTRYPOINT can interact. If there are both CMD and ENTRYPOINT in the Dockerfile, the commands in CMD will not be executed, and there are some differences according to the different forms of the two, as follows:



There are some other instructions, but the above instructions are the most frequently used. Through these instructions, we can build some custom images that are commonly used by ourselves. Such as java image, tomcat image, canal image, jekens image and so on.

Finally, put a Dokerfile to make the tomcat-maven-jdk integrated environment mirror by yourself:

FROM daocloud.io/java:8

#install maven 3.3.9
ENV MAVEN_VERSION 3.3.9
RUN mkdir -p /usr/share/maven \
  && curl -fsSL http://apache.osuosl.org/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz \
    | tar -xzC /usr/share/maven --strip-components=1 \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
ENV MAVEN_HOME /usr/share/maven
VOLUME /root/.m2

#install tomcat 8
RUN mkdir -p /usr/share/tomcat && \
    curl -fsSL http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.16/bin/apache-tomcat-8.5.16.tar.gz | tar -xzC /usr/share/tomcat
ENV TOMCAT_HOME /usr/share/tomcat/apache-tomcat-8.5.16
EXPOSE 8080

ENTRYPOINT ${TOMCAT_HOME}/bin/startup.sh && tail -F ${TOMCAT_HOME}/logs/catalina.out

(The above technical details are all from the docker official website. If you want to understand other instructions in more detail, please visit https://docs.docker.com/engine/reference/builder/ directly )


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325941994&siteId=291194637