DockerFile (DockerFile, DockerFile, DockerFile

Author: IT Wang Xiaoer blog: itwxe.com

What is a 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.

— Excerpt from rookie tutorial

Speaking in human language, Docker provides a series of instructions through which the Docker engine can build the image.

DockerFile directive

1 FORM

FORM Base ImageCopy the code

Must be placed in the first line of the DOckerfile to indicate which base image to start building from.

2 MAINTAINET

MAINTAINET Author nameCopy the code

Specifies information for the maintainer.

3 COPY

COPY [--chown=<user>:<group>] < source path 1>... < target path > COPY [- chown = < user >, < group >] [" < 1 > the source path, "... "> < target path"] [- chown = < user >, < group >] : optional parameter that users to change container is copied to the file owner and group < target path > : The specified path in the image does not need to be created in advance. If the path does not exist, it will be created automaticallyCopy the code

Copy files to an image, for example:

COPY hom* /mydir/ COPY hom? .txt /mydir/Copy the code

Unlike ADD, the source path cannot access network resources and does not automatically decompress compressed packages.

4 ADD

The format of the ADD command is the same as that of COPY (COPY is officially recommended for the same requirements). Functions are similar, with the following differences:

  • Advantages of ADD: When the < source > is executed as a tar file in gzip, bzip2, or xz format, it is automatically copied and decompressed to the < destination path >, and the source path can use network resources
  • Disadvantages of ADD: You cannot copy tar files without decompressing them. Invalidates the image build cache, which may slow the image build. You can determine whether to use it based on whether automatic decompression is required

5 RUN

Shell format: RUN < command-line command ># < command line command > is equivalent to shell commands that operate on terminals

execFormat: RUN ["Executable file".Parameters of "1"."Parameter 2"]
    # RUN ["./test.sh", "param1", "param2"] is equivalent to RUN./test.sh param1 param2
Copy the code

RUN is used to RUN commands in the image container

The intermediate image created by the RUN directive is cached and used in the next build. If you don’t want to use these cache images, you can specify the –no-cache parameter at build time, such as docker build –no-cache.

At the same time, to minimize the number of file layers of the image, it is best to execute multiple shell commands ata time, using && connection, for example, mkdir data && CD data.

6 CMD

CMD <shell > CMD ["< executable file or command >"."<param1>"."<param2>". ]  CMD ["<param1>"."<param2>". ]The ENTRYPOINT directive provides a default parameter for the program specified by the ENTRYPOINT directive
Copy the code

Similar to the RUN command, but CMD is the command executed when the container is started, and when multiple CMD commands are defined, only the last one takes effect.

The rookie tutorial points out that the second format is recommended because the first format is actually automatically converted to the second format during execution, and the default executable is sh.

7 ENTRYPOINT

ENTRYPOINT ["<executeable>","<param1>","<param2>",...]
Copy the code

Similar to CMD directives, but not overridden by directives specified by the docker Run command line arguments, and these command line arguments are sent as arguments to the program specified by the ENTRYPOINT directive.

However, if you run docker Run with the — entryPoint option, the program specified by the CMD directive will be overwritten.

When docker run is executed, you can specify the parameters required for ENTRYPOINT to run.

Similar to CMD, if multiple ENTRYPOINT directives exist in a Dockerfile, only the last one takes effect.

This command can be used with the CMD command: CMD is used to change parameters. In this case, CMD is used to pass parameters to ENTRYPOINT.

Example:

Assuming that the nginx:test image has been built from Dockerfile:

FROM nginx

ENTRYPOINT ["nginx"."-c"] # set the cords
CMD ["/etc/nginx/nginx.conf"] # varargs
Copy the code

1. Run without passing parameters.

docker run nginx:test
Copy the code

By default, the container runs the following command to start the main process.

nginx -c /etc/nginx/nginx.conf
Copy the code

2. Run the parameter transfer.

docker run nginx:test -c /etc/nginx/new.conf
Copy the code

By default, the container will run the following command to start the main process (/etc/nginx/new.conf: assume this file already exists in the container).

nginx -c /etc/nginx/new.conf
Copy the code

8 EXPOSE

EXPOSE < Port 1> [< Port 2>...]Copy the code

Tells the Docker server to expose the port, which needs to be mapped through -p when the container is started.

9 EVN

ENV <key>=<value>
Copy the code

Set the environment variable and define the environment variable so that it can be used in subsequent instructions.

10 WORKDIR

WORKDIR < working directory path >Copy the code

Specify the working directory. The working directory specified with WORKDIR is present at each layer of the build image.

WORKDIR Specifies a working directory that must be created in advance.

11 USER

USER < username >[:< USER group >]Copy the code

The command is used to specify the user and user group that execute the subsequent command. The user and user group must exist in advance.

12 VOLUME

VOLUME ["< 1 > path"."< 2 > path". ] VOLUME < path >Copy the code

Define anonymous data volumes. If you forget to attach a data volume when starting the container, it is automatically mounted to an anonymous volume, which is usually used to save persistent data such as MySQL. You can also mount the volume when starting the container by using -v.

Three, demonstration examples

1. Create a directory, download the compiled JDK file and upload the project JAR package.

mkdir /itwxe/DockerFile && cd /itwxe/DockerFile
Copy the code

2, create Dockerfile file, name must be Dockerfile, add content.

vim Dockerfile

Add content, in order to use the point command as much as possible, based on the centos image to demonstrate, of course, if you want to run jar of course, the openJDK based image is preferred

The base mirror is centos
FROM centos
# Maintainer Itwxe
MAINTAINER itwxe
# copy JDKCOPY jdk1.8.0 _202 jdk1.8.0 _202Add the project to the image
COPY sunny-admin.jar docker-sunny.jar
Configure the JDK environmentENV JAVA_HOME = / jdk1.8.0 _202 ENV PATH =$JAVA_HOME/bin:$PATH
ENV CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
The container provides services on port 9002
EXPOSE 9002
Start the app.jar application after starting the container
ENTRYPOINT ["java"."-jar"."docker-sunny.jar"]
Copy the code

3. Pay attention to directory structure

4. Compile and build the image

Where. Represents the context path, files in this directory are packed into the image, our JDK and JAR packages are stored in this directory, when need to COPY operations are searched from this directory.

docker build -t itwxe/sunny-admin .
Copy the code

The packaging process is as follows. You can see that the image is created successfully.

5. Create a container and run it to see if the project is normal.

docker run -d --name sunny -p 9002:9002 itwxe/sunny-admin
Copy the code

You can see the normal operation of the project, this is a basic application framework written by myself, all the foundation should have, except for business development, I will write a series of building tutorials in the future, to find a better direction.

So far, I have successfully created the image from Dockerfile and run it. However, this method is too cumbersome and requires uploading jar packages to the Docker environment. As a Java programmer, OF course, I don’t want to use such a cumbersome packaging method. The next article starts with one-click packaging of SpringBoot images and publishing them to the server via the Maven plugin.

Now that you’ve read this, please like, comment, follow and bookmark it!