Detailed explanation of dockerfile files (common commands)

When writing a Dockerfile, consider the following best practices:

  1. Minimize image size : Try to use lightweight base images and minimize unnecessary layers during the build process.

  2. Use caching wisely : Docker will try to reuse cached layers, and if one step changes, subsequent steps will lose the cache. Therefore, put frequently changing steps last to take full advantage of the cache.

  3. Clean unnecessary files : When building an image, delete unnecessary files and caches to reduce image size.

  4. Security : Ensure that the software packages and configurations in the image are safe and updated in a timely manner.

  5. Documentation : Add comments and documentation to your Dockerfile so others can understand your build process.

When writing a Dockerfile, it's important to understand what each command does. The following are the Dockerfile commands involved and their explanations:

  1. FROM : FROMThe command specifies the base image, that is, which image your container will be built on. This is the first command in the Dockerfile and must appear before any other instructions. For example: FROM ubuntu:20.04means it is built based on Ubuntu 20.04 image.

  2. WORKDIR : WORKDIRThe command is used to set the working directory, which is the default directory when executing commands within the container. For example: WORKDIR /appset the working directory to /app.

  3. RUN : RUNcommand is used to execute commands within the container. It can be used to install software packages, configure the environment and other operations. For example: run and RUN apt-get update && apt-get install -y nginxinside the container to install Nginx.apt-get updateapt-get install

  4. COPY and ADD : COPYand ADDcommands are used to copy files from the host to inside the container. For example: copy files COPY app.py /app/on the host to a directory in the container.app.py/app/

  5. EXPOSE : EXPOSEThe command is used to declare the listening port inside the container. This does not automatically map ports to the host, but it can help others understand the port configuration inside the container. For example: EXPOSE 80declare that the container will listen on port 80.

  6. CMD : CMDCommand is used to define the command to be run when the container starts. Typically used to define the default command for a container. For example: CMD ["python", "app.py"]Define the default startup command to run app.pya script.

  7. ENTRYPOINT : CMDSimilar to ENTRYPOINT, ENTRYPOINTcommand is used to define the command to be run when the container starts. The difference is that CMDthe parameters of can be overridden, while ENTRYPOINTthe parameters of cannot be overridden. Typically used to define the entry point of a container.
    For example:

    ENTRYPOINT ["python", "app.py"]
    
  8. ENV : ENVCommand is used to set environment variables. You can use these environment variables inside the container. For example: ENV MY_ENV_VAR=valueSet an MY_ENV_VARenvironment variable named.

  9. USER : USERThe command is used to specify the username or UID to use when executing commands within the container. It can be used to improve the security of containers to avoid running applications with root privileges.
    For example:

    USER appuser
    
  10. VOLUME : VOLUMEcommand is used to create a volume that can be shared between containers. It is typically used to persist data or share files with the host.
    For example:

    VOLUME /data
    
  11. ARG : ARGThe command is used to define build-time parameters that can be passed to the Dockerfile during the build process. It allows you to dynamically set some values ​​at build time.
    For example:

    ARG APP_VERSION=latest
    

In addition to the common Dockerfile commands mentioned above, there are some other commands and techniques that can be used to further customize and optimize your Docker image building process:

  1. LABEL : LABELThe command is used to add metadata labels to the image. It is usually used to provide image description information, maintainer information, etc. These tags can docker inspectbe viewed via commands.
    For example:

    LABEL maintainer="[email protected]"
    
  2. HEALTHCHECK : HEALTHCHECKCommand is used to define the health check of the container. This command allows Docker to monitor the health of the container and take action if the container is unhealthy.
    For example:

    HEALTHCHECK --interval=5m --timeout=3s \
      CMD curl -f http://localhost/ || exit 1
    
    具体解释如下:
    
    • --interval=5m: This part sets the health check interval. In this example, the container will perform a health check every 5 minutes. If not set --interval, a check will be performed every 30 seconds by default.

    • --timeout=3s: This part sets the timeout for each health check. If the health check command does not return a result within 3 seconds, the health check will be considered failed.

    • CMD curl -f http://localhost/ || exit 1: This part is the actual health check command. It uses curla command to attempt access http://localhost/, and a flag to ensure that the command -freturns success only if the HTTP request returns success (status code 2xx) . curlIf access fails (for example, the application inside the container is unresponsive), the curlcommand will fail, causing the container's health check to also fail. In this case, the container's status will be marked as unhealthy.

  3. Multi-stage builds : Using multi-stage builds can significantly reduce image size. You can define multiple build phases in a Dockerfile and then copy the build results from one phase to another.
    For example:

    # 第一阶段:构建应用程序
    FROM golang:1.16 AS builder
    WORKDIR /app
    COPY . .
    RUN go build -o myapp
    
    # 第二阶段:构建最终镜像
    FROM debian:bullseye-slim
    COPY --from=builder /app/myapp /usr/local/bin/myapp
    CMD ["myapp"]
    

These commands and techniques allow you to build Docker images more flexibly, customizing and optimizing them according to specific needs. In actual applications, you can choose to use appropriate Dockerfile commands and strategies based on the complexity and requirements of the project.

Guess you like

Origin blog.csdn.net/Mrxiao_bo/article/details/132916390