The command format isINSTRUCTION arguments
, Commands includeFROM
,MAINTAINER
,RUN
.
From
Format:FROM <image>
OrFROM <image>:<tag>
.
The first command must beFROM
Command. In addition, if you create multiple images in the same dockerfile, you can use multipleFROM
Command (once for each image ).
Maintainer
Format:MAINTAINER <name>
, Specify the maintainer information.
Run
Format:RUN <command>
OrRUN ["executable", "param1", "param2"]
.
The former will run commands in the shell terminal, that is/bin/sh -c
The latter usesexec
Run. The second method can be used to specify other terminals, for exampleRUN ["/bin/bash", "-c", "echo hello"]
.
Each entryRUN
The command runs the specified command on the basis of the current image and submits it to the new image. You can use\
Line feed.
CMD
Three formats are supported.
CMD ["executable","param1","param2"]
Useexec
Execution, recommended methods;
CMD command param1 param2
In/bin/sh
;
CMD ["param1","param2"]
ProvidedENTRYPOINT
;
Specify the command to be executed when the container is started. Each dockerfile can have only oneCMD
Command. If multiple commands are specified, only the last one will be executed.
If you specify the running command when starting the container, it will overwriteCMD
The specified command.
Expose
Format:EXPOSE <port> [<port>...]
.
Inform the docker server of the port number exposed by the container for the interconnected system.
Env
Format:ENV <key> <value>
. Specify an environment variable.RUN
Command used and maintained during container running.
For example
ENV PG_MAJOR 9.3ENV PG_VERSION 9.3.4RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress && …ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH
Add
Format:ADD <src> <dest>
.
This command will copy the specified<src>
To the container<dest>
. Where<src>
It can be a relative path of the directory where dockerfile is located, a URL, or a tar file (automatically decompressed as a directory ). .
Copy
Format:COPY <src> <dest>
.
Copy<src>
(The relative path of the directory where dockerfile is located)<dest>
.
We recommend that you use the local directory as the source directory.COPY
.
Entrypoint
Two formats:
ENTRYPOINT ["executable", "param1", "param2"]
ENTRYPOINT command param1 param2
(Executed in shell ).
Configure the commands executed after the container is started and cannot bedocker run
The provided parameters are overwritten.
Each dockerfile can contain only oneENTRYPOINT
When multiple are specified, only the last one takes effect.
Volume
Format:VOLUME ["/data"]
.
Create a mount point that can be mounted from a local host or other containers. It is generally used to store databases and data to be maintained.
User
Format:USER daemon
.
Specify the username or uid when running the container.RUN
The specified user is also used.
When the service does not require administrator permission, you can use this command to specify the user to run. In addition, you can create the required users before, for example:RUN groupadd -r postgres && useradd -r -g postgres postgres
. You can usegosu
But not recommendedsudo
.
Workdir
Format:WORKDIR /path/to/workdir
.
For subsequentRUN
,CMD
,ENTRYPOINT
Command to configure the working directory.
MultipleWORKDIR
Command. If the parameters of subsequent commands are relative paths, they are based on the paths specified by the previous commands. For example
WORKDIR /aWORKDIR bWORKDIR cRUN pwd
The final path is/a/b/c
.
Onbuild
Format:ONBUILD [INSTRUCTION]
.
Configure the operation commands executed when the created image is used as the basic image of another newly created image.
For example, dockerfile creates an image using the following content:image-A
.
[...]ONBUILD ADD . /app/srcONBUILD RUN /usr/local/bin/python-build --dir /app/src[...]
If a new image is created based onFROM image-A
When the basic image is specifiedONBUILD
Command content, equivalent to adding two commands later.
FROM image-A#Automatically run the followingADD . /app/srcRUN /usr/local/bin/python-build --dir /app/src
UseONBUILD
The image of the command, which should be specified in the label, for exampleruby:1.9-onbuild
.
Dockerfile command Summary