Detailed commands in Dockerfile

Detailed explanation of commands in Dockerfile: https://www.runoob.com/docker/docker-dockerfile.html

FROM: The customized images are all based on FROM, where nginx is the basic image required for customization. The subsequent operations are based on nginx.
RUN: used to execute the command line commands that follow. There are two formats:
         shell format: RUN <command line command> # <command line command> is equivalent to the shell command operated on the terminal.
         exec format: RUN ["executable file", "parameter 1", "parameter 2"] # For example: # RUN ["./test.php", "dev", "offline"] is equivalent to RUN ./test .php dev offline

COPY: Copy instruction, copy files or directories from the context directory to the specified path in the container.
              COPY [--chown=<user>:<group>] <source path 1>... <target path>

ADD: The ADD instruction and COPY use the same format (under the same requirements, the official recommendation is to use COPY). The functions are similar, but the differences are as follows:
            Advantages of ADD: When the execution of <source file> is a tar compressed file, and the compression format is gzip, bzip2 and xz, it
                                   will be automatically copied and decompressed to the <target path>.
            The disadvantage of ADD: It is impossible to copy tar compressed files without decompressing. It will invalidate the image build cache,
                                   which may make the image build slower. Whether it is used or not can be determined according to whether it needs to be automatically decompressed.
CMD: Similar to the RUN instruction, it is used to run the program, but the timing of the two runs is different:
             CMD runs during docker run.
             RUN is in docker build.

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 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.
    Advantages: When executing docker run, you can specify the parameters required for ENTRYPOINT operation.
    Note: If there are multiple ENTRYPOINT instructions in the Dockerfile, only the last one will take effect.
    Format: ENTRYPOINT ["<executeable>","<param1>","<param2>",...]

ENV: Set environment variables, define environment variables, then you can use this environment variable in subsequent instructions.
    Format: ENV <key> <value>
        ENV <key1>=<value1> <key2>=<value2>...

EXPOSE: Just declare the port.
    Role: To
        help mirror users understand the guard port of the mirroring service, so as 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.
    Format:
        EXPOSE <Port 1> [<Port 2>...]

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.
    Role:
        Avoid important data that is lost due to container restart, which is very fatal.
        Avoid containers that keep getting bigger.
    Format:
        VOLUME ["<path 1>", "<path 2>"...]
        VOLUME <path>
    When starting the container docker run, we can modify the mount point through the -v parameter.

WORKDIR: Specify the working directory. The working directory specified by WORKDIR will exist in every layer of the image.
    (The working directory specified by WORKDIR must be created in advance). In the process of docker build building the image,
    each RUN command is a new layer. Only directories created through WORKDIR will always exist.
    Format:
        WORKDIR <working directory path>

Note:
(1) docker build -t nginx:test. The
last instruction. It is the context path, the context path refers to the docker is building the image, sometimes you want to use the local files (such as copy),
docker build command After knowing this path, all content under the path will be packaged.

(2) Use DockerFile to create a mirror image command:

docker build -f ./path -t runoob/centos:6.7 .

Don't miss the last dot symbol

-t: specify the name of the target image to be created, the format is: image name: label
-f: specify the directory where the Dockfile file is located, the default is the current directory:'PATH/Dockerfile

 

Guess you like

Origin blog.csdn.net/Thanours/article/details/108975420