2018-08-07
Dockerfile reference
Docker can build images automatically by reading the instructions from aDockerfile
. A Dockerfile
is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build
users can create an automated build that executes several command-line instructions in succession.
This page describes the commands you can use in a Dockerfile
. When you are done reading this page, refer to the Dockerfile
Best Practices for a tip-oriented guide.
Usage
The docker build
command builds an image from a Dockerfile
and a context. The build’s context is the set of files at a specified location PATH
or URL
. The PATH
is a directory on your local filesystem. The URL
is a Git repository location.
A context is processed recursively. So, a PATH
includes any subdirectories and the URL
includes the repository and its submodules. This example shows a build command that uses the current directory as context:
$ docker build . Sending build context to Docker daemon 6.51 MB ...
The build is run by the Docker daemon, not by the CLI. The first thing a build process does is send the entire context (recursively) to the daemon. In most cases, it’s best to start with an empty directory as context and keep your Dockerfile in that directory. Add only the files needed for building the Dockerfile.
Warning: Do not use your root directory,
/
, as thePATH
as it causes the build to transfer the entire contents of your hard drive to the Docker daemon.
To use a file in the build context, the Dockerfile
refers to the file specified in an instruction, for example, a COPY
instruction. To increase the build’s performance, exclude files and directories by adding a .dockerignore
file to the context directory. For information about how to create a .dockerignore
file see the documentation on this page.
Traditionally, the Dockerfile
is called Dockerfile
and located in the root of the context. You use the -f
flag with docker build
to point to a Dockerfile anywhere in your file system.
$ docker build -f /path/to/a/Dockerfile .
You can specify a repository and tag at which to save the new image if the build succeeds:
$ docker build -t shykes/myapp .
To tag the image into multiple repositories after the build, add multiple -t
parameters when you run the build
command:
$ docker build -t shykes/myapp:1.0.2 -t shykes/myapp:latest .
Before the Docker daemon runs the instructions in the Dockerfile
, it performs a preliminary validation of the Dockerfile
and returns an error if the syntax is incorrect:
$ docker build -t test/myapp . Sending build context to Docker daemon 2.048 kBError response from daemon: Unknown instruction: RUNCMD
The Docker daemon runs the instructions in the Dockerfile
one-by-one, committing the result of each instruction to a new image if necessary, before finally outputting the ID of your new image. The Docker daemon will automatically clean up the context you sent.
Note that each instruction is run independently, and causes a new image to be created - so RUN cd /tmp
will not have any effect on the next instructions.
Whenever possible, Docker will re-use the intermediate images (cache), to accelerate the docker build
process significantly. This is indicated by the Using cache
message in the console output. (For more information, see the Build cache section in the Dockerfile
best practices guide):
$ docker build -t svendowideit/ambassador . Sending build context to Docker daemon 15.36 kB Step 1/4 : FROM alpine:3.2 ---> 31f630c65071 Step 2/4 : MAINTAINER SvenDowideit@home.org.au ---> Using cache ---> 2a1c91448f5f Step 3/4 : RUN apk update && apk add socat && rm -r /var/cache/ ---> Using cache ---> 21ed6e7fbb73 Step 4/4 : CMD env | grep _TCP= | (sed 's/.*_PORT_\([0-9]*\)_TCP=tcp:\/\/\(.*\):\(.*\)/socat -t 100000000 TCP4-LISTEN:\1,fork,reuseaddr TCP4:\2:\3 \&/' && echo wait) | sh ---> Using cache ---> 7ea8aef582cc Successfully built 7ea8aef582cc
Build cache is only used from images that have a local parent chain. This means that these images were created by previous builds or the whole chain of images was loaded with docker load
. If you wish to use build cache of a specific image you can specify it with --cache-from
option. Images specified with--cache-from
do not need to have a parent chain and may be pulled from other registries.
When you’re done with your build, you’re ready to look into Pushing a repository to its registry.
Format
Here is the format of the Dockerfile
:
# CommentINSTRUCTION arguments
The instruction is not case-sensitive. However, convention is for them to be UPPERCASE to distinguish them from arguments more easily.
Docker runs instructions in a Dockerfile
in order. A Dockerfile
must start with a FROM
instruction. The FROM
instruction specifies the Base Image from which you are building. FROM
may only be preceded by one or more ARG
instructions, which declare arguments that are used in FROM
lines in the Dockerfile
.
Docker treats lines that begin with #
as a comment, unless the line is a valid parser directive. A #
marker anywhere else in a line is treated as an argument. This allows statements like:
# CommentRUN echo 'we are running some # of cool things'
Line continuation characters are not supported in comments.
Parser directives
Parser directives are optional, and affect the way in which subsequent lines in a Dockerfile
are handled. Parser directives do not add layers to the build, and will not be shown as a build step. Parser directives are written as a special type of comment in the form # directive=value
. A single directive may only be used once.
Once a comment, empty line or builder instruction has been processed, Docker no longer looks for parser directives. Instead it treats anything formatted as a parser directive as a comment and does not attempt to validate if it might be a parser directive. Therefore, all parser directives must be at the very top of a Dockerfile
.
Parser directives are not case-sensitive. However, convention is for them to be lowercase. Convention is also to include a blank line following any parser directives. Line continuation characters are not supported in parser directives.
Due to these rules, the following examples are all invalid:
Invalid due to line continuation:
# direc \tive=value
Invalid due to appearing twice:
# directive=value1# directive=value2FROM ImageName
Treated as a comment due to appearing after a builder instruction:
FROM ImageName# directive=value
Treated as a comment due to appearing after a comment which is not a parser directive:
# About my dockerfile# directive=valueFROM ImageName
The unknown directive is treated as a comment due to not being recognized. In addition, the known directive is treated as a comment due to appearing after a comment which is not a parser directive.
# unknowndirective=value# knowndirective=value
Non line-breaking whitespace is permitted in a parser directive. Hence, the following lines are all treated identically:
#directive=value# directive =value# directive= value# directive = value# dIrEcTiVe=value
The following parser directive is supported:
escape
作者:心悦飞飞
链接:https://www.jianshu.com/p/46822f255bf8
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章