The introduction

In the frequent use of Docker technology at present, building Docker image is the basic skill in daily development, naturally we need to write Dockerfile file, the following will be detailed about the commonly used Dockerfile instruction, is to do a memo.

  • Instructions must be uppercase
  • Each instruction creates a new mirror layer

Dockerfile common directives

FROM

Build the required base image (downloaded) in the following format: # --platform Specifies the platform to be built on, and the target platform to be built on is used by default (optional) # image base image # name Specifies the name of the base image, for later directives to use the tag of the base image, default current version (optional) # digest Specifies the digest of the base image, Default current version (optional) FROM [--platform=<platform>] <image>[AS <name>] FROM [--platform=<platform>] <image>[:<tag>] [AS <name>] FROM [--platform=<platform>] <image>[@<digest>] [AS <name>]Copy the code

RUN

Executable <command> # exec and not executable RUN ["executable", "param1", "param2"]Copy the code

EXPOSE

Notify the Docker container to listen on the specified network port at runtime. You can specify whether the port listens for TCP or UDP. If no protocol is specified, TCP is used by default. EXPOSE <port> [<port>/<protocol>...]Copy the code

ENV

ENV <key>=<value>Copy the code

ADD

ADD [--chown=<user>:<group>] < SRC >... <dest> ADD [--chown=<user>:<group>] ["<src>",... "<dest>"]Copy the code

COPY

# COPY the file to the specified location COPY [- chown = < user >, < group >] < SRC >... <dest> COPY [--chown=<user>:<group>] ["<src>",... "<dest>"]Copy the code

VOLUME

Create a mount point with the specified name and mark it as holding external mount volumes from the local host or other container. The value can be a JSON array, pure string VOLUME ["/data"] with multiple arguments.Copy the code

WORKDIR

WORKDIR /path/to/ WORKDIRCopy the code

ENTRYPOINT

# exec specifies the command to run when the container is started. Preferred ENTRYPOINT ["executable", "param1", "param2"] # shell form ENTRYPOINT command param1 param2Copy the code

CMD

# exec specifies the command to run when the container is started. CMD ["executable","param1","param2"] # as the default parameter of ENTRYPOINT CMD ["param1","param2"] # shell form CMD command param1 param2Copy the code

USER

USER < USER >[:<group>] USER <UID>[:<GID>]Copy the code

ARG

# define a variable, ARG <name>[=<default value>] # ARG version=latest ARG user=root FROM ARG version=latest ARG user=root FROM Node :$version USER $USER # use ARG version ARG USER docker build --build-arg version=1.0.0 USER =root -t qiuqfang/node .Copy the code

SHELL

Allow overriding the default shell form for commands shell ["executable", "parameters"] In Windows, CMD is the default shell, and write-host is the powershell command. Powershell -command RUN powershell -command Write -host hello world "-command"] # Hello world RUN write-host hello world # Switch the default shell to CMD shell [" CMD ", "/S", "/C"]Copy the code

ONBUILD

# add a trigger INSTRUCTION to the image ONBUILD <INSTRUCTION>Copy the code

STOPSIGNAL

Set the system call signal to be sent to the container to exitCopy the code

HEALTHCHECK

# Check container health by running commands inside the container HEALTHCHECK [OPTIONS] CMD command # Disable any health checks inherited from the underlying image HEALTHCHECK NONECopy the code

\