Dockerfile concise tutorial

Dockerfile concise tutorial

docker command can refer to: https://blog.csdn.net/a40850273/article/details/104510863

Dockerfile introduction

dockerfile keywords:

  • FROM: Used to specify the base image used for construction. Subsequent operations will perform a personalized installation on this mirror. The referenced basic mirror can be a local mirror or a remote mirror. The local mirror is referenced by [mirror name: Tag], and the remote mirror is applied by [ip: port number/mirror name: Tag].
  • ENV: Set the environment variable, you can use this environment variable in subsequent instructions.
  • ARG: Build parameters, and have the same effect with ENV. But the scope is different. The environment variable set by ARG is only valid in the Dockerfile, which means that it is only valid during the docker build process. This environment variable does not exist in the built image. Will be overwritten by --build-arg <parameter name>=<value> in docker build.
  • COPY: Copy instruction, copy files or directories from the context directory to the specified path in the container.
  • ADD: The format of the ADD instruction is the same as that of COPY (under the same requirements, the official recommendation is to use COPY). The difference is that ADD will automatically copy and decompress the tar compressed file in the source file.
  • RUN: Specify the instructions that need to be run during the build process. Every time RUN is executed, a new layer is created on docker. So too many meaningless layers will cause the image to expand too much. Therefore, it is recommended to use && \ to concatenate multiple commands.
  • CMD: Specify the instructions that need to be executed during the running of the container, which will be overwritten by the instructions specified to be run during docker run. For multiple CMDs, only the last one is valid.
  • ENTRYPOINT: Similar to the CMD command, but it will not be overwritten by the command specified by the docker run command line parameters, and these command line parameters will be used as parameters and sent to the program specified by the ENTRYPOINT command. However, if the --entrypoint option is used when running docker run, the parameter of this option can be used as the program to be run to override the program specified by the ENTRYPOINT instruction. If there are multiple ENTRYPOINT instructions in the Dockerfile, only the last one will take effect.
  • WORKDIR: Specify the working directory, which will be automatically created during the construction process. The working directory specified by WORKDIR will play a role in each layer of the image.
  • VOLUME: Define anonymous data volume. If you forget to mount the data volume when starting the container, it will be automatically mounted to the anonymous volume. Avoid losing important data due to container restart, which is very fatal.
  • EXPOSE: Just declare the port. Help mirror users understand the guard port of this mirroring service to facilitate the configuration of mapping. When using random port mapping at runtime, that is, when docker run -P, the EXPOSE port will be automatically mapped randomly.
  • USER: Used to specify the user and user group that executes subsequent commands. This is just to switch the user who executes subsequent commands (the user and user group must already exist in advance).

Realize variable parameter operation through ENTRYPOINT and CMD

A simple example

FROM nvidia/cuda:10.2-base

ENV BASE_PATH=/root/object_detect
ENV PYTHON_PATH=python3.8

WORKDIR $BASE_PATH

ADD . $BASE_PATH

RUN rm -rf /etc/apt/sources.list.d/* && \
    apt-get update && \
    mkdir /root/.pip && \
    cp pip.conf /root/.pip/pip.conf && \
    apt-get -y install python3-pip && \
    apt-get -y install $PYTHON_PATH && \
    $PYTHON_PATH -m pip install -r requirements.txt && \
    $PYTHON_PATH -m pip install -r app/requirements.txt && \
    apt-get install -y libsm6 && \
    apt-get install -y libxrender1 && \
    apt-get install -y libxext-dev
    
ENTRYPOINT $PYTHON_PATH app/manager.py

Points to note:

  • Avoid multiple RUN instructions causing the image to be too large, use && \
  • Networking is required during the build process, such as apt-get, you need to add the --network=host option to the docker build command
  • If the command needs to be manually selected, the operation needs to be specified by the option, such as apt-get -y
  • When installing pip through python3-pip, the system will install it according to the python version associated with the system. If you need to perform pip operations on other python versions, you can do it with python -m pip install.

docker build command

docker build -t test:v1.0 --network=host .
  • docker build builds the image through the dockerifle file under the specified path. Last. To build the directory, the dockerfile in the build directory is used by default, or you can use the -f option to specify the dockerfile in a specific path
  • -t test:v1.0 is used to formulate the name and tag of the built image
  • --network=host is used to formulate the network connection of the host used in the construction process, if you need to network during the construction process, you need to set this option

Guess you like

Origin blog.csdn.net/a40850273/article/details/107342196