I Definition: it is a text file used to build a docker image. It is a script composed of instructions and parameters required to build an image one by one.
II Dockerfile content Basics
- Each reserved word instruction must be uppercase followed by at least one parameter
- The instructions are executed from top to bottom
- #Indicates a comment
- Each instruction creates a new mirror layer and commits the mirror
III Dockerfile common reserved word instruction
- FORM: the first mirror image must be based on the existing FORM
- MAINTAINER: name and email address of the image MAINTAINER
- RUN: the command that needs to be RUN when the container is built. RUN it when docker is built
Two formats: shell to: RUN <Run command line> #Equivalent to operating shell commands on the terminal, such as RUN yum -y install vim exec Format: RUN ["Executable file","Parameter 1","Parameter 2"] # For example, RUN ["./test,php","dev","offline"] is equivalent to run/ test. php dev offline
- Export: the exposed port of the current container
- WORKDIR: Specifies the default login working directory of the terminal after creating the container. It is a foothold
- USER: Specifies the USER to execute the image. If none is specified, the default is root
- ENV: used to set environment variables during image construction
ENV MY_PATH /usr/mytest This environment variable can be used at any subsequent time RUN These environment variables can also be used directly in other instructions For example: WORKDIR $MY_PATH
- ADD: copy the files in the host directory into the image, and automatically process the URL and decompress the tar compressed package
- COPY: similar to ADD, COPY files and directories into the image, and COPY the files / directories from the < source path > in the construction context directory to the < target path > location in the new layer of image
COPY src dest COPY ["src","dest"] <src from path>:Source file or source directory <dest Target path>:The specified path in the container does not need to be built in advance. If it does not exist, it will be created by itself
- VOLUME: container data VOLUME, which is used for data saving and persistence
As long as the host directory is mounted on the container directory, the change will take effect immediately. We can Dockerfile By using VOLUME Command to achieve the same purpose: FROM debian:wheezy VOLUME /data But there's another thing-v Parameters can be achieved without Dockerfile What you can't do is mount the specified host directory on the container. For example: $ docker run -v /home/adrian/data:/data debian ls /data This command will mount the host/home/adrian/data Directory into container/data On the directory. Any in/home/adrian/data All files in the directory will appear in the container. This is very helpful for sharing files between hosts and containers, such as mounting source code that needs to be compiled. In order to ensure portability (not all the host directories of the system can be used), mounting the host directory does not need to be from Dockerfile appoint. When used-v Parameter, any files in the image directory will not be copied to Volume Yes. (translator's note: Volume Will be copied to the mirror directory, and the mirror will not be copied to the volume)
- CMD: specify what to do after the container starts
CMD Format and of instructions RUN be similar shell Format: CMD <command> exec Format: CMD ["Executable file","Parameter 1","Parameter 2"] Parameter list format: CMD ["Parameter 1","Parameter 2"...],Specified in ENTRYPOINT After the command, use CMD Specify specific parameters
Note: there can be multiple, but only the last one takes effect. CMD will be replaced by the parameter after docker run.
- ENTRYPOINT: it is also used to specify the command to be run when a container is started, similar to CMD, but will not be overwritten by the command after docker run, and these command-line parameters will be sent to the program specified by the ENTRYPOINT instruction as parameters
Command format: ENTRYPOINT ["executeable","param1","param2"...] ENTRYPOINT Can and CMD It is generally used only by changing parameters CMD,there CMD Is giving ENTRYPOINT Pass parameters when specified ENTRYPOINT After, CMD The meaning of is changed. Instead of running its commands directly, it will CMD The contents of are passed as parameters to ENTRYPOINT Command, his two combinations will become <ENTRYPOINT> "<CMD>" Case: FROM nginx ENTRYPOINT ["nginx", "-c"] #Fixed parameter CMD ["/etc/nginx/nginx.conf"] #Variable parameter
IV Simple practice of docker micro service
1.docker build -t image name: tag
Note: Dockerfile and project jar is placed in the same category, and there is one at the end of the name Can't forget;
2.docker run -d -p 8080:8080 image name: tag
V docker network
1.docker network
connect Connect a container to a docker network create Create a custom docker local area network disconnect Exit a container from a local area network inspect Display information of a local area network ls Show all docker local area network prune Delete all unreferenced docker local area network rm delete docker network
Vi docker-compose
1. Docker compose down
2. Docker compose up - d start