Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Docker Dockerfile detailed explanation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

FROM

The function is to specify the base image and must be the first instruction.

If it is not based on any image, it is written as: FROM scratch.

It also means that the next instruction will start as the first layer of the mirror image.

Syntax:

FROM FROM: FROM: three ways to write, in which sum is optional. If not selected, the default value is latest

RUN

Function is to run the specified command

There are two formats for RUN commands

1. RUN 2. RUN ["executable", "param1", "param2"] the first is followed directly by the shell command on the linux operating system default / bin/sh-c defaults to cmd / S / C on the windows operating system

The second is similar to function calls.

Executable can be understood as an executable file, followed by two parameters.

Compare the two writing methods:

RUN / bin/bash-c 'source $HOME/.bashrc; echo $HOME RUN ["/ bin/bash", "- c", "echo hello"] Note: multiline commands do not write more than one RUN, because each instruction in Dockerfile creates a layer. The number of RUN sets up as many layers of image, which will cause the image to be bloated and multi-tier, which not only increases the time for component deployment, but also is prone to errors. The newline character when writing RUN is\.

CMD

Function is the command to be run when the container starts

There are three ways to write grammar.

1. CMD ["executable", "param1", "param2"] 2. CMD ["param1", "param2"] 3. The third kind of CMD command param1 param2 is easier to understand When shell this way of execution and writing the first and second are actually executable files with parameters in the form of examples to illustrate two ways of writing: CMD ["sh", "- c", "echo $HOME" CMD ["echo", "$HOME"] supplementary details: this includes parameters must be in double quotes, that is, "can not be single quotation marks. Never write it in single quotation marks. The reason is that after the parameter is passed, docker parses a JSON array

RUN & CMD

Don't confuse RUN with CMD.

RUN is the command that runs when the component container and submits the run result CMD is the command that is executed when the container starts, but does not run when the component is, and the component tightly specifies what this command looks like.

LABEL

The function is to specify a label for the image

Syntax:

LABEL =... A Dockerfile species can have multiple LABEL, as follows: LABEL "com.example.vendor" = "ACME Incorporated" LABEL com.example.label-with-value= "foo" LABEL version= "1.0" LABEL description= "This text illustrates\ that label-values can span multiple lines." However, it is not recommended to write in this way. it is best to write on a single line, and use the\ symbol if it is too long to break a line.

As follows:

LABEL multi.label1= "value1"\ multi.label2= "value2"\ other= "value3" description: LABEL inherits the LABEL of the basic image. If the same key is encountered, the value will be overridden.

MAINTAINER

Designated author

Syntax:

The MAINTAINER EXPOSE function is to expose the listening port of the container when it is running, but EXPOSE does not allow the container to access the port of the host. If you want the container to have a mapping relationship with the port of the host, you must add the-P parameter when the container starts.

ENV

The function is to set environment variables

There are two kinds of grammar.

1. ENV 2. ENV = The difference between the two is that the first is to set one at a time, and the second is to set more than one at a time.

ADD

A copy command to copy the file into the scene.

If you think of a virtual machine and a container as two linux servers, this command is similar to scp, except that scp requires permission authentication with a user name and password, but ADD does not.

The syntax is as follows:

1. ADD. 2. The ADD [",..."] path can be an absolute path within the container, a relative path relative to the working directory, a local file or a local zip file, or a url.

If you write it as a url, then ADD is similar to the wget command

It can be written in the following ways:

ADD test relativeDir/ ADD test / relativeDirADD http://example.com/foobar / try not to write as a folder. If it is a folder, copy the contents of the entire directory, including file system metadata.

COPY

If you look at the name, it's another copy command.

The syntax is as follows:

1. COPY. 2. The difference between COPY [",..."] and ADD COPY can only be local files, other uses are the same.

ENTRYPOINT

Function is the default command at startup

The syntax is as follows:

1. ENTRYPOINT ["executable", "param1", "param2"] 2. ENTRYPOINT command param1 param2 if you see this from top to bottom, then you should be familiar with these two grammars. The second is to write shell. The first is to add parameters to the executable file.

Compared with CMD (these two commands are too similar and can be used together):

1. The same point: can only write one, if more than one, then only the last effective container started to run, the same time to run 2. The difference: ENTRYPOINT will not be overwritten by the running command, while CMD will be overwritten if we write both ENTRYPOINT and CMD in Dockerfile, and the CMD instruction is not a complete executable command, then the content specified by CMD will be used as an argument to ENTRYPOINT

As follows:

FROM ubuntuENTRYPOINT ["top", "- b"] CMD ["- c"] if we write both ENTRYPOINT and CMD in Dockerfile, and CMD is a complete instruction, then the two will overwrite each other, and who will take effect at the end

As follows:

FROM ubuntuENTRYPOINT ["top", "- b"] CMD ls-al then ls-al will be executed, top-b will not be executed.

VOLUME

You can mount the function, and you can hang inland folders or other container-grown folders to this container.

The syntax is:

VOLUME ["/ data"] states that ["/ data"] can be a JsonArray or multiple values. Therefore, the following words are all correct: VOLUME ["/ var/log/"] VOLUME / var/logVOLUME / var/log/ var/db. In general, the container uses AUFS when persistent data is needed. This file system cannot persist data. When the container is closed, all changes will be lost. So use this command when the data needs to be persisted.

USER

The user who starts the container can be user name or UID, so only the following two ways of writing are correct

USER daemo USER UID Note: if the container is set to run as the daemon user, then RUN, CMD and ENTRYPOINT will all run under this user

WORKDIR

Syntax:

WORKDIR / path/to/workdir sets the working directory, which is effective for RUN,CMD,ENTRYPOINT,COPY,ADD. If it does not exist, it will be created, or it can be set multiple times.

Such as:

The result of WORKDIR / aWORKDIR bWORKDIR cRUN pwdpwd execution is / a/b/c

WORKDIR can also parse environment variables

Such as:

The execution result of ENV DIRPATH/ pathWORKDIR $DIRPATH/$DIRNAMERUN pwdpwd is / path/$DIRNAME

ARG

Syntax:

ARG [=] set variable command, ARG command defines a variable, when docker build creates a mirror, use-- build-arg = to specify a parameter if the user specifies a parameter in the build image that is not defined in the Dockerfile type, then there will be a Warning prompt as follows: [Warning] One or more build-args [foo] were not consumed.

We can define one or more parameters as follows:

FROM busyboxARG user1ARG buildno

You can also give the parameter a default value:

FROM busyboxARG user1=someuserARG buildno=1

If we give the default value of the parameter defined by ARG, this default value will be used when the parameter value is not specified when build is mirrored.

ONBUILD

Syntax:

The ONBUILD [INSTRUCTION] command only works on child images that are currently mirrored. For example, if the current image is A, the ls-al command ONBUILD RUN ls-al will not be executed when the image An is built or started. If an image B is built based on the An image, the ls-al command will be executed when the image B is built.

STOPSIGNAL

Syntax:

The function of the STOPSIGNAL signalSTOPSIGNAL command is what kind of instructions are sent to the system when the container is launched.

HEALTHCHECK

Container health check command

There are two kinds of grammar:

1. HEALTHCHECK [OPTIONS] CMD command2. The first function of HEALTHCHECK NONE is to run a command inside the container to check the health status of the container, and the second function is to cancel the health check command in the basic image.

The options of [OPTIONS] support the following three options:

-- the default interval between two interval=DURATION checks is 30 seconds.-- the timeout of the timeout=DURATION health check command is 30 seconds by default.-- retries=N when the container fails for a specified number of consecutive times, the container is considered unhealthy. The status is unhealthy, and the default number is 3.

Note:

The HEALTHCHECK command can only occur once, and if it occurs more than once, only the last one takes effect.

The return value of the command after CMD determines the success of this health check. The specific return value is as follows:

0: success-indicates that the container is healthy 1: unhealthy-indicates that the container is no longer working 2: reserved-preserves the value

Example:

HEALTHCHECK-- interval=5m-- timeout=3s\ CMD curl-f http://localhost/ | | exit 1 Health check command is: curl-f http://localhost/ | | exit 1 the interval between two checks is 5 seconds and the command timeout is 3 seconds.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report