2016-10-14 18:30:36 -04:00
---
2020-03-30 11:45:40 -04:00
title: Dockerfile reference
2016-10-14 18:30:36 -04:00
description: "Dockerfiles use a simple DSL which allows you to automate the steps you would normally manually take to create an image."
2016-11-03 18:48:30 -04:00
keywords: "builder, docker, Dockerfile, automation, image creation"
2017-01-31 21:53:42 -05:00
redirect_from:
- /reference/builder/
2016-10-14 18:30:36 -04:00
---
2014-04-15 20:53:12 -04:00
2017-10-04 13:03:55 -04:00
<!-- This file is maintained within the docker/cli GitHub
2017-07-28 13:28:23 -04:00
repository at https://github.com/docker/cli/. Make all
2016-10-19 13:25:45 -04:00
pull requests against that repo. If you see this file in
another repository, consider it read-only there, as it will
periodically be overwritten by the definitive file. Pull
requests which include edits to this file in other repositories
will be rejected.
-->
2014-04-15 20:53:12 -04:00
2015-07-17 14:40:56 -04:00
Docker can build images automatically by reading the instructions from a
`Dockerfile` . 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
2016-10-27 04:24:55 -04:00
Practices](https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/) for a tip-oriented guide.
2014-09-23 17:21:00 -04:00
2014-04-15 20:53:12 -04:00
## Usage
2019-11-21 13:52:50 -05:00
The [docker build ](commandline/build.md ) command builds an image from
2017-06-26 21:46:45 -04:00
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.
2014-04-15 20:53:12 -04:00
2021-04-20 05:03:34 -04:00
The build 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 build context:
2014-04-15 20:53:12 -04:00
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker build .
Sending build context to Docker daemon 6.51 MB
...
```
2015-07-17 14:40:56 -04:00
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.
2020-04-19 09:46:48 -04:00
> **Warning**
>
2021-04-20 05:03:34 -04:00
> Do not use your root directory, `/`, as the `PATH` for your build context, as
> it causes the build to transfer the entire contents of your hard drive to the
> Docker daemon.
2020-04-29 18:17:28 -04:00
{:.warning}
2015-07-17 14:40:56 -04:00
2015-09-06 18:48:05 -04:00
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
2015-07-17 14:40:56 -04:00
performance, exclude files and directories by adding a `.dockerignore` file to
the context directory. For information about how to [create a `.dockerignore`
file](#dockerignore-file) see the documentation on this page.
2014-04-15 20:53:12 -04:00
2015-07-17 14:40:56 -04:00
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.
2014-08-19 16:39:50 -04:00
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker build -f /path/to/a/Dockerfile .
```
2015-09-06 18:48:05 -04:00
2014-04-15 20:53:12 -04:00
You can specify a repository and tag at which to save the new image if
the build succeeds:
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker build -t shykes/myapp .
```
2014-04-15 20:53:12 -04:00
2015-08-30 09:48:03 -04:00
To tag the image into multiple repositories after the build,
add multiple `-t` parameters when you run the `build` command:
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker build -t shykes/myapp:1.0.2 -t shykes/myapp:latest .
```
2015-08-30 09:48:03 -04:00
2016-09-21 20:42:53 -04:00
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:
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker build -t test/myapp .
2021-04-21 05:47:31 -04:00
[+] Building 0.3s (2/2) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 60B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
error: failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition:
dockerfile parse error line 2: unknown instruction: RUNCMD
2020-04-19 09:46:48 -04:00
```
2016-09-21 20:42:53 -04:00
2015-09-06 18:48:05 -04:00
The Docker daemon runs the instructions in the `Dockerfile` one-by-one,
committing the result of each instruction
2014-04-15 20:53:12 -04:00
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
2014-04-23 16:48:28 -04:00
to be created - so `RUN cd /tmp` will not have any effect on the next
instructions.
2014-04-15 20:53:12 -04:00
2021-04-21 05:47:31 -04:00
Whenever possible, Docker uses a build-cache to accelerate the `docker build`
process significantly. This is indicated by the `CACHED` message in the console
output. (For more information, see the [`Dockerfile` best practices guide ](https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/ ):
2014-04-15 20:53:12 -04:00
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker build -t svendowideit/ambassador .
2021-04-21 05:47:31 -04:00
[+] Building 0.7s (6/6) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 286B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/alpine:3.2 0.4s
=> CACHED [1/2] FROM docker.io/library/alpine:3.2@sha256:e9a2035f9d0d7ce 0.0s
=> CACHED [2/2] RUN apk add --no-cache socat 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:1affb80ca37018ac12067fa2af38cc5bcc2a8f09963de 0.0s
2021-04-21 16:36:16 -04:00
=> => naming to docker.io/svendowideit/ambassador 0.0s
2020-04-19 09:46:48 -04:00
```
2014-04-15 20:53:12 -04:00
2021-04-21 16:36:16 -04:00
By default, the build cache is based on results from previous builds on the machine
2021-04-21 05:47:31 -04:00
on which you are building. The `--cache-from` option also allows you to use a
build-cache that's distributed through an image registry refer to the
[specifying external cache sources ](commandline/build.md#specifying-external-cache-sources )
section in the `docker build` command reference.
2016-09-22 17:38:00 -04:00
2021-04-21 05:47:31 -04:00
When you're done with your build, you're ready to look into [scanning your image with `docker scan` ](https://docs.docker.com/engine/scan/ ),
and [pushing your image to Docker Hub ](https://docs.docker.com/docker-hub/repos/ ).
2014-04-15 20:53:12 -04:00
2018-10-31 19:16:14 -04:00
## BuildKit
Starting with version 18.09, Docker supports a new backend for executing your
builds that is provided by the [moby/buildkit ](https://github.com/moby/buildkit )
project. The BuildKit backend provides many benefits compared to the old
implementation. For example, BuildKit can:
2020-04-19 09:46:48 -04:00
- Detect and skip executing unused build stages
- Parallelize building independent build stages
- Incrementally transfer only the changed files in your build context between builds
- Detect and skip transferring unused files in your build context
- Use external Dockerfile implementations with many new features
- Avoid side-effects with rest of the API (intermediate images and containers)
- Prioritize your build cache for automatic pruning
2018-10-31 19:16:14 -04:00
2019-11-21 13:52:50 -05:00
To use the BuildKit backend, you need to set an environment variable
2018-10-31 19:16:14 -04:00
`DOCKER_BUILDKIT=1` on the CLI before invoking `docker build` .
To learn about the experimental Dockerfile syntax available to BuildKit-based
builds [refer to the documentation in the BuildKit repository ](https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md ).
2014-04-15 20:53:12 -04:00
## Format
2014-07-11 22:28:36 -04:00
Here is the format of the `Dockerfile` :
2014-04-15 20:53:12 -04:00
2020-03-17 10:01:52 -04:00
```dockerfile
2016-04-22 18:04:46 -04:00
# Comment
INSTRUCTION arguments
```
2014-04-15 20:53:12 -04:00
2016-04-22 18:04:46 -04:00
The instruction is not case-sensitive. However, convention is for them to
be UPPERCASE to distinguish them from arguments more easily.
2014-04-15 20:53:12 -04:00
2017-04-10 15:45:21 -04:00
Docker runs instructions in a `Dockerfile` in order. A `Dockerfile` **must
2020-07-01 09:26:06 -04:00
begin with a `FROM` instruction**. This may be after [parser
2019-10-10 17:03:48 -04:00
directives](#parser-directives), [comments ](#format ), and globally scoped
[ARGs ](#arg ). The `FROM` instruction specifies the [*Parent
2021-09-13 10:20:09 -04:00
Image*](https://docs.docker.com/glossary/#parent-image) from which you are
2020-04-29 18:17:28 -04:00
building. `FROM` may only be preceded by one or more `ARG` instructions, which
declare arguments that are used in `FROM` lines in the `Dockerfile` .
2014-04-15 20:53:12 -04:00
2016-06-11 15:33:09 -04:00
Docker treats lines that *begin* with `#` as a comment, unless the line is
2016-10-19 13:25:45 -04:00
a valid [parser directive ](#parser-directives ). A `#` marker anywhere
2016-04-22 18:04:46 -04:00
else in a line is treated as an argument. This allows statements like:
2014-04-15 20:53:12 -04:00
2020-03-17 10:01:52 -04:00
```dockerfile
2016-04-22 18:04:46 -04:00
# Comment
RUN echo 'we are running some # of cool things'
```
2014-04-15 20:53:12 -04:00
2020-07-01 09:26:06 -04:00
Comment lines are removed before the Dockerfile instructions are executed, which
means that the comment in the following example is not handled by the shell
executing the `echo` command, and both examples below are equivalent:
```dockerfile
RUN echo hello \
# comment
world
```
```dockerfile
RUN echo hello \
world
```
2016-04-22 18:04:46 -04:00
Line continuation characters are not supported in comments.
2020-07-01 09:26:06 -04:00
> **Note on whitespace**
>
> For backward compatibility, leading whitespace before comments (`#`) and
> instructions (such as `RUN`) are ignored, but discouraged. Leading whitespace
> is not preserved in these cases, and the following examples are therefore
> equivalent:
>
> ```dockerfile
> # this is a comment-line
> RUN echo hello
> RUN echo world
> ```
>
> ```dockerfile
> # this is a comment-line
> RUN echo hello
> RUN echo world
> ```
>
> Note however, that whitespace in instruction _arguments_, such as the commands
> following `RUN`, are preserved, so the following example prints ` hello world`
> with leading whitespace as specified:
>
> ```dockerfile
> RUN echo "\
> hello\
> world"
> ```
2016-04-22 18:04:46 -04:00
## Parser directives
2016-06-11 15:33:09 -04:00
Parser directives are optional, and affect the way in which subsequent lines
2016-04-22 18:04:46 -04:00
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.
2016-06-11 15:33:09 -04:00
Once a comment, empty line or builder instruction has been processed, Docker
2016-04-22 18:04:46 -04:00
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
2016-06-11 15:33:09 -04:00
top of a `Dockerfile` .
2016-04-22 18:04:46 -04:00
Parser directives are not case-sensitive. However, convention is for them to
2016-06-11 15:33:09 -04:00
be lowercase. Convention is also to include a blank line following any
2016-04-22 18:04:46 -04:00
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:
2020-03-17 10:01:52 -04:00
```dockerfile
2016-04-22 18:04:46 -04:00
# direc \
tive=value
```
Invalid due to appearing twice:
2020-03-17 10:01:52 -04:00
```dockerfile
2016-04-22 18:04:46 -04:00
# directive=value1
# directive=value2
FROM ImageName
```
2016-06-11 15:33:09 -04:00
2016-04-22 18:04:46 -04:00
Treated as a comment due to appearing after a builder instruction:
2020-03-17 10:01:52 -04:00
```dockerfile
2016-04-22 18:04:46 -04:00
FROM ImageName
# directive=value
```
Treated as a comment due to appearing after a comment which is not a parser
directive:
2020-03-17 10:01:52 -04:00
```dockerfile
2016-04-22 18:04:46 -04:00
# About my dockerfile
# directive=value
2017-03-09 17:11:30 -05:00
FROM ImageName
2016-04-22 18:04:46 -04:00
```
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.
2020-03-17 10:01:52 -04:00
```dockerfile
2016-04-22 18:04:46 -04:00
# unknowndirective=value
# knowndirective=value
2016-06-11 15:33:09 -04:00
```
2016-04-22 18:04:46 -04:00
Non line-breaking whitespace is permitted in a parser directive. Hence, the
2016-06-11 15:33:09 -04:00
following lines are all treated identically:
2016-04-22 18:04:46 -04:00
2020-03-17 10:01:52 -04:00
```dockerfile
2016-04-22 18:04:46 -04:00
#directive=value
# directive =value
# directive= value
# directive = value
# dIrEcTiVe=value
```
2018-10-31 19:16:14 -04:00
The following parser directives are supported:
2016-04-22 18:04:46 -04:00
2020-04-19 09:46:48 -04:00
- `syntax`
- `escape`
2016-04-22 18:04:46 -04:00
2018-10-31 19:16:14 -04:00
## syntax
2021-04-21 16:36:16 -04:00
< a name = "external-implementation-features" > <!-- included for deep - links to old section --> < / a >
2020-04-19 09:46:48 -04:00
```dockerfile
# syntax=[remote image reference]
```
2018-10-31 19:16:14 -04:00
For example:
2020-04-19 09:46:48 -04:00
```dockerfile
2021-04-21 16:36:16 -04:00
# syntax=docker/dockerfile:1
2020-04-19 09:46:48 -04:00
# syntax=docker.io/docker/dockerfile:1
# syntax=example.com/user/repo:tag@sha256:abcdef...
```
2018-10-31 19:16:14 -04:00
2021-04-21 16:36:16 -04:00
This feature is only available when using the [BuildKit ](#buildkit ) backend, and
is ignored when using the classic builder backend.
2018-10-31 19:16:14 -04:00
2021-04-21 16:36:16 -04:00
The syntax directive defines the location of the Dockerfile syntax that is used
to build the Dockerfile. The BuildKit backend allows to seamlessly use external
implementations that are distributed as Docker images and execute inside a
container sandbox environment.
2018-10-31 19:16:14 -04:00
2021-04-21 16:36:16 -04:00
Custom Dockerfile implementations allows you to:
2020-04-19 09:46:48 -04:00
2021-04-21 16:36:16 -04:00
- Automatically get bugfixes without updating the Docker daemon
2018-10-31 19:16:14 -04:00
- Make sure all users are using the same implementation to build your Dockerfile
2021-04-21 16:36:16 -04:00
- Use the latest features without updating the Docker daemon
- Try out new features or third-party features before they are integrated in the Docker daemon
- Use [alternative build definitions, or create your own ](https://github.com/moby/buildkit#exploring-llb )
2018-10-31 19:16:14 -04:00
### Official releases
2019-11-21 13:52:50 -05:00
Docker distributes official versions of the images that can be used for building
Dockerfiles under `docker/dockerfile` repository on Docker Hub. There are two
2021-04-21 16:36:16 -04:00
channels where new images are released: `stable` and `labs` .
Stable channel follows [semantic versioning ](https://semver.org ). For example:
- `docker/dockerfile:1` - kept updated with the latest `1.x.x` minor _and_ patch release
- `docker/dockerfile:1.2` - kept updated with the latest `1.2.x` patch release,
and stops receiving updates once version `1.3.0` is released.
- `docker/dockerfile:1.2.1` - immutable: never updated
We recommend using `docker/dockerfile:1` , which always points to the latest stable
release of the version 1 syntax, and receives both "minor" and "patch" updates
for the version 1 release cycle. BuildKit automatically checks for updates of the
syntax when performing a build, making sure you are using the most current version.
2018-10-31 19:16:14 -04:00
2021-04-21 16:36:16 -04:00
If a specific version is used, such as `1.2` or `1.2.1` , the Dockerfile needs to
be updated manually to continue receiving bugfixes and new features. Old versions
of the Dockerfile remain compatible with the new versions of the builder.
2018-10-31 19:16:14 -04:00
2021-04-21 16:36:16 -04:00
**labs channel**
2018-10-31 19:16:14 -04:00
2021-04-21 16:36:16 -04:00
The "labs" channel provides early access to Dockerfile features that are not yet
available in the stable channel. Labs channel images are released in conjunction
with the stable releases, and follow the same versioning with the `-labs` suffix,
for example:
2018-10-31 19:16:14 -04:00
2021-04-21 16:36:16 -04:00
- `docker/dockerfile:labs` - latest release on labs channel
- `docker/dockerfile:1-labs` - same as `dockerfile:1` in the stable channel, with labs features enabled
- `docker/dockerfile:1.2-labs` - same as `dockerfile:1.2` in the stable channel, with labs features enabled
- `docker/dockerfile:1.2.1-labs` - immutable: never updated. Same as `dockerfile:1.2.1` in the stable channel, with labs features enabled
2018-10-31 19:16:14 -04:00
2021-04-21 16:36:16 -04:00
Choose a channel that best fits your needs; if you want to benefit from
new features, use the labs channel. Images in the labs channel provide a superset
of the features in the stable channel; note that `stable` features in the labs
channel images follow [semantic versioning ](https://semver.org ), but "labs"
features do not, and newer releases may not be backwards compatible, so it
2018-10-31 19:16:14 -04:00
is recommended to use an immutable full version variant.
2021-04-21 16:36:16 -04:00
For documentation on "labs" features, master builds, and nightly feature releases,
refer to the description in [the BuildKit source repository on GitHub ](https://github.com/moby/buildkit/blob/master/README.md ).
For a full list of available images, visit the [image repository on Docker Hub ](https://hub.docker.com/r/docker/dockerfile ),
and the [docker/dockerfile-upstream image repository ](https://hub.docker.com/r/docker/dockerfile-upstream )
for development builds.
2018-10-31 19:16:14 -04:00
2016-04-22 18:04:46 -04:00
## escape
2020-04-19 09:46:48 -04:00
```dockerfile
# escape=\ (backslash)
```
2016-04-22 18:04:46 -04:00
Or
2020-04-19 09:46:48 -04:00
```dockerfile
# escape=` (backtick)
```
2016-04-22 18:04:46 -04:00
2016-06-11 15:33:09 -04:00
The `escape` directive sets the character used to escape characters in a
`Dockerfile` . If not specified, the default escape character is `\` .
2016-04-22 18:04:46 -04:00
The escape character is used both to escape characters in a line, and to
escape a newline. This allows a `Dockerfile` instruction to
span multiple lines. Note that regardless of whether the `escape` parser
2016-06-11 15:33:09 -04:00
directive is included in a `Dockerfile` , *escaping is not performed in
a `RUN` command, except at the end of a line.*
2016-04-22 18:04:46 -04:00
2016-06-11 15:33:09 -04:00
Setting the escape character to `` ` ` ` is especially useful on
`Windows` , where `\` is the directory path separator. `` ` ` ` is consistent
2016-04-22 18:04:46 -04:00
with [Windows PowerShell ](https://technet.microsoft.com/en-us/library/hh847755.aspx ).
2016-06-11 15:33:09 -04:00
Consider the following example which would fail in a non-obvious way on
2016-04-22 18:04:46 -04:00
`Windows` . The second `\` at the end of the second line would be interpreted as an
2016-06-11 15:33:09 -04:00
escape for the newline, instead of a target of the escape from the first `\` .
2016-04-22 18:04:46 -04:00
Similarly, the `\` at the end of the third line would, assuming it was actually
handled as an instruction, cause it be treated as a line continuation. The result
of this dockerfile is that second and third lines are considered a single
2016-06-11 15:33:09 -04:00
instruction:
2016-04-22 18:04:46 -04:00
2020-03-17 10:01:52 -04:00
```dockerfile
2016-10-28 14:36:11 -04:00
FROM microsoft/nanoserver
2016-04-22 18:04:46 -04:00
COPY testfile.txt c:\\
RUN dir c:\
```
Results in:
2021-04-20 05:03:34 -04:00
```console
PS E:\myproject> docker build -t cmd .
2020-04-19 09:46:48 -04:00
Sending build context to Docker daemon 3.072 kB
Step 1/2 : FROM microsoft/nanoserver
---> 22738ff49c6d
Step 2/2 : COPY testfile.txt c:\RUN dir c:
GetFileAttributesEx c:RUN: The system cannot find the file specified.
2021-04-20 05:03:34 -04:00
PS E:\myproject>
2020-04-19 09:46:48 -04:00
```
2016-04-22 18:04:46 -04:00
One solution to the above would be to use `/` as the target of both the `COPY`
instruction, and `dir` . However, this syntax is, at best, confusing as it is not
natural for paths on `Windows` , and at worst, error prone as not all commands on
`Windows` support `/` as the path separator.
2016-06-11 15:33:09 -04:00
By adding the `escape` parser directive, the following `Dockerfile` succeeds as
2016-04-22 18:04:46 -04:00
expected with the use of natural platform semantics for file paths on `Windows` :
2020-04-19 09:46:48 -04:00
```dockerfile
# escape=`
2016-06-11 15:33:09 -04:00
2020-04-19 09:46:48 -04:00
FROM microsoft/nanoserver
COPY testfile.txt c:\
RUN dir c:\
```
2016-04-22 18:04:46 -04:00
Results in:
2021-04-20 05:03:34 -04:00
```console
PS E:\myproject> docker build -t succeeds --no-cache=true .
2020-04-19 09:46:48 -04:00
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM microsoft/nanoserver
---> 22738ff49c6d
Step 2/3 : COPY testfile.txt c:\
---> 96655de338de
Removing intermediate container 4db9acbb1682
Step 3/3 : RUN dir c:\
---> Running in a2c157f842f5
Volume in drive C has no label.
Volume Serial Number is 7E6D-E0F7
Directory of c:\
10/05/2016 05:04 PM 1,894 License.txt
10/05/2016 02:22 PM < DIR > Program Files
10/05/2016 02:14 PM < DIR > Program Files (x86)
10/28/2016 11:18 AM 62 testfile.txt
10/28/2016 11:20 AM < DIR > Users
10/28/2016 11:20 AM < DIR > Windows
2 File(s) 1,956 bytes
4 Dir(s) 21,259,096,064 bytes free
---> 01c7f3bef04f
Removing intermediate container a2c157f842f5
Successfully built 01c7f3bef04f
2021-04-20 05:03:34 -04:00
PS E:\myproject>
2020-04-19 09:46:48 -04:00
```
2016-04-22 18:04:46 -04:00
## Environment replacement
2014-10-27 16:05:29 -04:00
2015-01-28 21:28:48 -05:00
Environment variables (declared with [the `ENV` statement ](#env )) can also be
used in certain instructions as variables to be interpreted by the
`Dockerfile` . Escapes are also handled for including variable-like syntax
into a statement literally.
2014-10-27 16:05:29 -04:00
Environment variables are notated in the `Dockerfile` either with
`$variable_name` or `${variable_name}` . They are treated equivalently and the
brace syntax is typically used to address issues with variable names with no
whitespace, like `${foo}_bar` .
2015-01-28 21:28:48 -05:00
The `${variable_name}` syntax also supports a few of the standard `bash`
modifiers as specified below:
2020-04-19 09:46:48 -04:00
- `${variable:-word}` indicates that if `variable` is set then the result
2015-01-28 21:28:48 -05:00
will be that value. If `variable` is not set then `word` will be the result.
2020-04-19 09:46:48 -04:00
- `${variable:+word}` indicates that if `variable` is set then `word` will be
2015-01-28 21:28:48 -05:00
the result, otherwise the result is the empty string.
In all cases, `word` can be any string, including additional environment
variables.
2014-10-27 16:05:29 -04:00
Escaping is possible by adding a `\` before the variable: `\$foo` or `\${foo}` ,
for example, will translate to `$foo` and `${foo}` literals respectively.
2015-01-28 21:28:48 -05:00
2014-10-27 16:05:29 -04:00
Example (parsed representation is displayed after the `#` ):
2020-04-19 09:46:48 -04:00
```dockerfile
FROM busybox
2020-09-23 06:42:17 -04:00
ENV FOO=/bar
WORKDIR ${FOO} # WORKDIR /bar
ADD . $FOO # ADD . /bar
COPY \$FOO /quux # COPY $FOO /quux
2020-04-19 09:46:48 -04:00
```
2014-10-27 16:05:29 -04:00
2015-09-30 16:11:36 -04:00
Environment variables are supported by the following list of instructions in
2015-07-19 00:09:48 -04:00
the `Dockerfile` :
2014-10-27 16:05:29 -04:00
2020-04-19 09:46:48 -04:00
- `ADD`
- `COPY`
- `ENV`
- `EXPOSE`
- `FROM`
- `LABEL`
- `STOPSIGNAL`
- `USER`
- `VOLUME`
- `WORKDIR`
- `ONBUILD` (when combined with one of the supported instructions above)
2014-10-27 16:05:29 -04:00
2015-04-25 14:57:01 -04:00
Environment variable substitution will use the same value for each variable
2017-04-10 15:45:21 -04:00
throughout the entire instruction. In other words, in this example:
2015-01-28 21:28:48 -05:00
2020-04-19 09:46:48 -04:00
```dockerfile
ENV abc=hello
ENV abc=bye def=$abc
ENV ghi=$abc
```
2015-01-28 21:28:48 -05:00
2015-09-30 16:11:36 -04:00
will result in `def` having a value of `hello` , not `bye` . However,
2017-08-23 17:33:03 -04:00
`ghi` will have a value of `bye` because it is not part of the same instruction
2015-01-28 21:28:48 -05:00
that set `abc` to `bye` .
2016-04-22 18:04:46 -04:00
## .dockerignore file
2014-07-01 15:03:07 -04:00
2015-10-07 16:51:57 -04:00
Before the docker CLI sends the context to the docker daemon, it looks
for a file named `.dockerignore` in the root directory of the context.
If this file exists, the CLI modifies the context to exclude files and
directories that match patterns in it. This helps to avoid
unnecessarily sending large or sensitive files and directories to the
daemon and potentially adding them to images using `ADD` or `COPY` .
The CLI interprets the `.dockerignore` file as a newline-separated
list of patterns similar to the file globs of Unix shells. For the
purposes of matching, the root of the context is considered to be both
the working and the root directory. For example, the patterns
`/foo/bar` and `foo/bar` both exclude a file or directory named `bar`
in the `foo` subdirectory of `PATH` or in the root of the git
repository located at `URL` . Neither excludes anything else.
2016-06-01 18:20:54 -04:00
If a line in `.dockerignore` file starts with `#` in column 1, then this line is
considered as a comment and is ignored before interpreted by the CLI.
2015-10-07 16:51:57 -04:00
Here is an example `.dockerignore` file:
2015-04-09 15:07:06 -04:00
2020-04-19 09:46:48 -04:00
```gitignore
2016-06-01 18:20:54 -04:00
# comment
2017-01-22 13:46:04 -05:00
*/temp*
*/*/temp*
temp?
2015-04-09 15:07:06 -04:00
```
This file causes the following build behavior:
2017-08-23 17:33:03 -04:00
| Rule | Behavior |
|:------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `# comment` | Ignored. |
| `*/temp*` | Exclude files and directories whose names start with `temp` in any immediate subdirectory of the root. For example, the plain file `/somedir/temporary.txt` is excluded, as is the directory `/somedir/temp` . |
| `*/*/temp*` | Exclude files and directories starting with `temp` from any subdirectory that is two levels below the root. For example, `/somedir/subdir/temporary.txt` is excluded. |
| `temp?` | Exclude files and directories in the root directory whose names are a one-character extension of `temp` . For example, `/tempa` and `/tempb` are excluded. |
2015-10-07 16:51:57 -04:00
Matching is done using Go's
[filepath.Match ](http://golang.org/pkg/path/filepath#Match ) rules. A
preprocessing step removes leading and trailing whitespace and
eliminates `.` and `..` elements using Go's
[filepath.Clean ](http://golang.org/pkg/path/filepath/#Clean ). Lines
that are blank after preprocessing are ignored.
2015-10-14 17:42:21 -04:00
Beyond Go's filepath.Match rules, Docker also supports a special
wildcard string `**` that matches any number of directories (including
zero). For example, `**/*.go` will exclude all files that end with `.go`
that are found in all directories, including the root of the build context.
2015-10-07 16:51:57 -04:00
Lines starting with `!` (exclamation mark) can be used to make exceptions
to exclusions. The following is an example `.dockerignore` file that
uses this mechanism:
2015-04-09 15:07:06 -04:00
2020-04-19 09:46:48 -04:00
```gitignore
*.md
!README.md
2015-04-09 15:07:06 -04:00
```
2015-10-07 16:51:57 -04:00
All markdown files *except* `README.md` are excluded from the context.
The placement of `!` exception rules influences the behavior: the last
line of the `.dockerignore` that matches a particular file determines
whether it is included or excluded. Consider the following example:
2020-04-19 09:46:48 -04:00
```gitignore
*.md
!README*.md
README-secret.md
2015-10-07 16:51:57 -04:00
```
No markdown files are included in the context except README files other than
`README-secret.md` .
Now consider this example:
2020-04-19 09:46:48 -04:00
```gitignore
*.md
README-secret.md
!README*.md
2015-10-07 16:51:57 -04:00
```
All of the README files are included. The middle line has no effect because
`!README*.md` matches `README-secret.md` and comes last.
You can even use the `.dockerignore` file to exclude the `Dockerfile`
and `.dockerignore` files. These files are still sent to the daemon
2017-04-10 15:45:21 -04:00
because it needs them to do its job. But the `ADD` and `COPY` instructions
2016-01-31 14:55:59 -05:00
do not copy them to the image.
2015-04-09 15:07:06 -04:00
2015-10-07 16:51:57 -04:00
Finally, you may want to specify which files to include in the
context, rather than which to exclude. To achieve this, specify `*` as
the first pattern, followed by one or more `!` exception patterns.
2014-07-01 15:03:07 -04:00
2020-04-19 09:46:48 -04:00
> **Note**
>
> For historical reasons, the pattern `.` is ignored.
2014-07-01 15:03:07 -04:00
2014-04-23 16:48:28 -04:00
## FROM
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
FROM [--platform=< platform > ] < image > [AS < name > ]
```
2014-04-15 20:53:12 -04:00
Or
2020-04-19 09:46:48 -04:00
```dockerfile
FROM [--platform=< platform > ] < image > [:< tag > ] [AS < name > ]
```
2014-04-15 20:53:12 -04:00
2015-02-26 21:23:50 -05:00
Or
2020-04-19 09:46:48 -04:00
```dockerfile
FROM [--platform=< platform > ] < image > [@< digest > ] [AS < name > ]
```
2015-02-26 21:23:50 -05:00
2017-08-23 17:33:03 -04:00
The `FROM` instruction initializes a new build stage and sets the
2021-09-13 10:20:09 -04:00
[*Base Image* ](https://docs.docker.com/glossary/#base-image ) for subsequent instructions. As such, a
2017-04-10 15:45:21 -04:00
valid `Dockerfile` must start with a `FROM` instruction. The image can be
2017-08-23 17:33:03 -04:00
any valid image – it is especially easy to start by **pulling an image** from
2020-11-07 08:29:39 -05:00
the [*Public Repositories* ](https://docs.docker.com/docker-hub/repos/ ).
2014-04-15 20:53:12 -04:00
2017-07-07 07:17:15 -04:00
- `ARG` is the only instruction that may precede `FROM` in the `Dockerfile` .
2017-04-10 15:45:21 -04:00
See [Understand how ARG and FROM interact ](#understand-how-arg-and-from-interact ).
2017-08-23 17:33:03 -04:00
- `FROM` can appear multiple times within a single `Dockerfile` to
2017-04-10 15:45:21 -04:00
create multiple images or use one build stage as a dependency for another.
2017-08-23 17:33:03 -04:00
Simply make a note of the last image ID output by the commit before each new
2017-04-10 15:45:21 -04:00
`FROM` instruction. Each `FROM` instruction clears any state created by previous
instructions.
2017-08-23 17:33:03 -04:00
- Optionally a name can be given to a new build stage by adding `AS name` to the
2017-04-10 15:45:21 -04:00
`FROM` instruction. The name can be used in subsequent `FROM` and
2020-07-27 18:56:23 -04:00
`COPY --from=<name>` instructions to refer to the image built in this stage.
2017-08-23 17:33:03 -04:00
- The `tag` or `digest` values are optional. If you omit either of them, the
2017-04-10 15:45:21 -04:00
builder assumes a `latest` tag by default. The builder returns an error if it
cannot find the `tag` value.
2019-07-12 19:00:57 -04:00
The optional `--platform` flag can be used to specify the platform of the image
in case `FROM` references a multi-platform image. For example, `linux/amd64` ,
`linux/arm64` , or `windows/amd64` . By default, the target platform of the build
request is used. Global build arguments can be used in the value of this flag,
for example [automatic platform ARGs ](#automatic-platform-args-in-the-global-scope )
allow you to force a stage to native build platform (`--platform=$BUILDPLATFORM`),
and use it to cross-compile to the target platform inside the stage.
2017-04-10 15:45:21 -04:00
### Understand how ARG and FROM interact
2017-08-23 17:33:03 -04:00
`FROM` instructions support variables that are declared by any `ARG`
2017-04-10 15:45:21 -04:00
instructions that occur before the first `FROM` .
2020-03-17 10:01:52 -04:00
```dockerfile
2017-04-10 15:45:21 -04:00
ARG CODE_VERSION=latest
FROM base:${CODE_VERSION}
CMD /code/run-app
FROM extras:${CODE_VERSION}
CMD /code/run-extras
```
2014-04-15 20:53:12 -04:00
2017-07-13 12:30:38 -04:00
An `ARG` declared before a `FROM` is outside of a build stage, so it
can't be used in any instruction after a `FROM` . To use the default value of
an `ARG` declared before the first `FROM` use an `ARG` instruction without
a value inside of a build stage:
2017-05-12 18:45:08 -04:00
2020-03-17 10:01:52 -04:00
```dockerfile
2017-07-13 14:26:37 -04:00
ARG VERSION=latest
FROM busybox:$VERSION
ARG VERSION
RUN echo $VERSION > image_version
2017-05-12 18:45:08 -04:00
```
2014-04-23 16:48:28 -04:00
## RUN
2014-04-15 20:53:12 -04:00
RUN has 2 forms:
2016-06-11 15:33:09 -04:00
- `RUN <command>` (*shell* form, the command is run in a shell, which by
2016-05-03 16:56:59 -04:00
default is `/bin/sh -c` on Linux or `cmd /S /C` on Windows)
2014-04-23 16:48:28 -04:00
- `RUN ["executable", "param1", "param2"]` (*exec* form)
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
The `RUN` instruction will execute any commands in a new layer on top of the
2016-10-15 07:03:47 -04:00
current image and commit the results. The resulting committed image will be
2014-07-11 22:28:36 -04:00
used for the next step in the `Dockerfile` .
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
Layering `RUN` instructions and generating commits conforms to the core
concepts of Docker where commits are cheap and containers can be created from
any point in an image's history, much like source control.
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
The *exec* form makes it possible to avoid shell string munging, and to `RUN`
2016-05-03 16:56:59 -04:00
commands using a base image that does not contain the specified shell executable.
The default shell for the *shell* form can be changed using the `SHELL`
command.
2014-04-15 20:53:12 -04:00
2015-09-14 07:08:27 -04:00
In the *shell* form you can use a `\` (backslash) to continue a single
RUN instruction onto the next line. For example, consider these two lines:
2016-10-15 07:03:47 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
2016-10-15 07:03:47 -04:00
RUN /bin/bash -c 'source $HOME/.bashrc; \
2015-09-14 07:08:27 -04:00
echo $HOME'
```
2021-08-21 08:54:14 -04:00
2015-09-14 07:08:27 -04:00
Together they are equivalent to this single line:
2016-10-15 07:03:47 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
2016-10-15 07:03:47 -04:00
RUN /bin/bash -c 'source $HOME/.bashrc; echo $HOME'
2015-09-14 07:08:27 -04:00
```
2020-04-19 09:46:48 -04:00
To use a different shell, other than '/bin/sh', use the *exec* form passing in
the desired shell. For example:
```dockerfile
RUN ["/bin/bash", "-c", "echo hello"]
```
2014-08-08 15:26:22 -04:00
2020-04-19 09:46:48 -04:00
> **Note**
>
2014-09-11 13:09:18 -04:00
> The *exec* form is parsed as a JSON array, which means that
> you must use double-quotes (") around words not single-quotes (').
2020-04-19 09:46:48 -04:00
Unlike the *shell* form, the *exec* form does not invoke a command shell.
This means that normal shell processing does not happen. For example,
`RUN [ "echo", "$HOME" ]` will not do variable substitution on `$HOME` .
If you want shell processing then either use the *shell* form or execute
a shell directly, for example: `RUN [ "sh", "-c", "echo $HOME" ]` .
When using the exec form and executing a shell directly, as in the case for
the shell form, it is the shell that is doing the environment variable
expansion, not docker.
> **Note**
2016-05-20 23:56:08 -04:00
>
> In the *JSON* form, it is necessary to escape backslashes. This is
2016-07-03 13:58:11 -04:00
> particularly relevant on Windows where the backslash is the path separator.
2016-05-20 23:56:08 -04:00
> The following line would otherwise be treated as *shell* form due to not
> being valid JSON, and fail in an unexpected way:
2020-04-19 09:46:48 -04:00
>
> ```dockerfile
> RUN ["c:\windows\system32\tasklist.exe"]
> ```
>
2016-05-20 23:56:08 -04:00
> The correct syntax for this example is:
2020-04-19 09:46:48 -04:00
>
> ```dockerfile
> RUN ["c:\\windows\\system32\\tasklist.exe"]
> ```
2014-10-02 14:16:51 -04:00
2014-07-11 22:28:36 -04:00
The cache for `RUN` instructions isn't invalidated automatically during
2015-09-30 16:11:36 -04:00
the next build. The cache for an instruction like
`RUN apt-get dist-upgrade -y` will be reused during the next build. The
cache for `RUN` instructions can be invalidated by using the `--no-cache`
2014-09-29 11:34:58 -04:00
flag, for example `docker build --no-cache` .
See the [`Dockerfile` Best Practices
2019-11-21 13:52:50 -05:00
guide](https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/) for more information.
2014-05-15 04:56:23 -04:00
2019-11-11 14:56:31 -05:00
The cache for `RUN` instructions can be invalidated by [`ADD` ](#add ) and [`COPY` ](#copy ) instructions.
2014-05-15 04:56:23 -04:00
2015-04-21 11:50:09 -04:00
### Known issues (RUN)
2014-04-15 20:53:12 -04:00
2014-07-24 18:19:50 -04:00
- [Issue 783 ](https://github.com/docker/docker/issues/783 ) is about file
2014-04-23 16:48:28 -04:00
permissions problems that can occur when using the AUFS file system. You
2015-03-26 03:46:13 -04:00
might notice it during an attempt to `rm` a file, for example.
For systems that have recent aufs version (i.e., `dirperm1` mount option can
be set), docker will attempt to fix the issue automatically by mounting
the layers with `dirperm1` option. More details on `dirperm1` option can be
2016-07-12 15:51:14 -04:00
found at [`aufs` man page ](https://github.com/sfjro/aufs3-linux/tree/aufs3.18/Documentation/filesystems/aufs )
2015-03-26 03:46:13 -04:00
2015-04-25 14:57:01 -04:00
If your system doesn't have support for `dirperm1` , the issue describes a workaround.
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
## CMD
2014-04-15 20:53:12 -04:00
2014-07-11 22:28:36 -04:00
The `CMD` instruction has three forms:
2014-04-15 20:53:12 -04:00
2014-10-02 14:16:51 -04:00
- `CMD ["executable","param1","param2"]` (*exec* form, this is the preferred form)
2014-04-23 16:48:28 -04:00
- `CMD ["param1","param2"]` (as *default parameters to ENTRYPOINT* )
2014-10-02 14:16:51 -04:00
- `CMD command param1 param2` (*shell* form)
2014-04-15 20:53:12 -04:00
2014-07-11 22:28:36 -04:00
There can only be one `CMD` instruction in a `Dockerfile` . If you list more than one `CMD`
then only the last `CMD` will take effect.
2014-04-15 20:53:12 -04:00
2014-07-11 22:28:36 -04:00
**The main purpose of a `CMD` is to provide defaults for an executing
2014-04-15 20:53:12 -04:00
container.** These defaults can include an executable, or they can omit
2014-07-11 22:28:36 -04:00
the executable, in which case you must specify an `ENTRYPOINT`
instruction as well.
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
If `CMD` is used to provide default arguments for the `ENTRYPOINT` instruction,
both the `CMD` and `ENTRYPOINT` instructions should be specified with the JSON
array format.
2014-08-07 02:39:14 -04:00
2020-04-19 09:46:48 -04:00
> **Note**
>
> The *exec* form is parsed as a JSON array, which means that you must use
> double-quotes (") around words not single-quotes (').
Unlike the *shell* form, the *exec* form does not invoke a command shell.
This means that normal shell processing does not happen. For example,
`CMD [ "echo", "$HOME" ]` will not do variable substitution on `$HOME` .
If you want shell processing then either use the *shell* form or execute
a shell directly, for example: `CMD [ "sh", "-c", "echo $HOME" ]` .
When using the exec form and executing a shell directly, as in the case for
the shell form, it is the shell that is doing the environment variable
expansion, not docker.
2014-10-02 14:16:51 -04:00
2014-04-23 16:48:28 -04:00
When used in the shell or exec formats, the `CMD` instruction sets the command
to be executed when running the image.
2014-04-15 20:53:12 -04:00
2014-07-11 22:28:36 -04:00
If you use the *shell* form of the `CMD` , then the `<command>` will execute in
2014-04-23 16:48:28 -04:00
`/bin/sh -c` :
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
CMD echo "This is a test." | wc -
```
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
If you want to **run your** `<command>` **without a shell** then you must
express the command as a JSON array and give the full path to the executable.
2014-07-11 22:28:36 -04:00
**This array form is the preferred format of `CMD` .** Any additional parameters
2014-04-23 16:48:28 -04:00
must be individually expressed as strings in the array:
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
CMD ["/usr/bin/wc","--help"]
```
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
If you would like your container to run the same executable every time, then
you should consider using `ENTRYPOINT` in combination with `CMD` . See
2014-04-17 11:09:08 -04:00
[*ENTRYPOINT* ](#entrypoint ).
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
If the user specifies arguments to `docker run` then they will override the
2014-07-11 22:28:36 -04:00
default specified in `CMD` .
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
> **Note**
>
> Do not confuse `RUN` with `CMD`. `RUN` actually runs a command and commits
2014-04-18 16:21:55 -04:00
> the result; `CMD` does not execute anything at build time, but specifies
> the intended command for the image.
2014-04-15 20:53:12 -04:00
2015-02-17 10:20:06 -05:00
## LABEL
2020-04-19 09:46:48 -04:00
```dockerfile
LABEL < key > =< value > < key > =< value > < key > =< value > ...
```
2015-02-17 10:20:06 -05:00
2015-03-16 16:28:55 -04:00
The `LABEL` instruction adds metadata to an image. A `LABEL` is a
key-value pair. To include spaces within a `LABEL` value, use quotes and
2015-09-06 18:48:05 -04:00
backslashes as you would in command-line parsing. A few usage examples:
2015-02-17 10:20:06 -05:00
2020-04-19 09:46:48 -04:00
```dockerfile
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."
```
2015-02-17 10:20:06 -05:00
2017-10-16 13:10:49 -04:00
An image can have more than one label. You can specify multiple labels on a
single line. Prior to Docker 1.10, this decreased the size of the final image,
but this is no longer the case. You may still choose to specify multiple labels
in a single instruction, in one of the following two ways:
2015-04-18 14:34:28 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
2017-10-16 13:10:49 -04:00
LABEL multi.label1="value1" multi.label2="value2" other="value3"
```
2015-09-06 18:48:05 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
2017-10-16 13:10:49 -04:00
LABEL multi.label1="value1" \
multi.label2="value2" \
other="value3"
```
2015-09-30 16:11:36 -04:00
2017-10-16 13:10:49 -04:00
Labels included in base or parent images (images in the `FROM` line) are
inherited by your image. If a label already exists but with a different value,
the most-recently-applied value overrides any previously-set value.
2015-03-17 00:49:33 -04:00
2020-04-19 09:46:48 -04:00
To view an image's labels, use the `docker image inspect` command. You can use
the `--format` option to show just the labels;
2021-04-20 05:03:34 -04:00
```console
$ docker image inspect --format='{{json .Config.Labels}}' myimage
2020-04-19 09:46:48 -04:00
```
2021-08-21 08:54:14 -04:00
2020-04-19 09:46:48 -04:00
```json
{
"com.example.vendor": "ACME Incorporated",
"com.example.label-with-value": "foo",
"version": "1.0",
"description": "This text illustrates that label-values can span multiple lines.",
"multi.label1": "value1",
"multi.label2": "value2",
"other": "value3"
}
```
2015-04-18 14:34:28 -04:00
2016-08-06 08:34:49 -04:00
## MAINTAINER (deprecated)
2020-04-19 09:46:48 -04:00
```dockerfile
MAINTAINER < name >
```
2016-08-06 08:34:49 -04:00
2016-10-14 18:30:36 -04:00
The `MAINTAINER` instruction sets the *Author* field of the generated images.
2016-08-06 08:34:49 -04:00
The `LABEL` instruction is a much more flexible version of this and you should use
it instead, as it enables setting any metadata you require, and can be viewed
easily, for example with `docker inspect` . To set a label corresponding to the
`MAINTAINER` field you could use:
2020-04-19 09:46:48 -04:00
```dockerfile
2021-05-04 12:45:10 -04:00
LABEL org.opencontainers.image.authors="SvenDowideit@home.org.au"
2020-04-19 09:46:48 -04:00
```
2016-08-06 08:34:49 -04:00
This will then be visible from `docker inspect` with the other labels.
2014-04-23 16:48:28 -04:00
## EXPOSE
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
EXPOSE < port > [< port > /< protocol > ...]
```
2014-04-15 20:53:12 -04:00
2015-10-12 17:59:25 -04:00
The `EXPOSE` instruction informs Docker that the container listens on the
2017-08-23 17:33:03 -04:00
specified network ports at runtime. You can specify whether the port listens on
TCP or UDP, and the default is TCP if the protocol is not specified.
The `EXPOSE` instruction does not actually publish the port. It functions as a
type of documentation between the person who builds the image and the person who
runs the container, about which ports are intended to be published. To actually
publish the port when running the container, use the `-p` flag on `docker run`
to publish and map one or more ports, or the `-P` flag to publish all exposed
2017-10-31 06:21:09 -04:00
ports and map them to high-order ports.
2015-09-30 16:11:36 -04:00
2017-11-09 14:31:45 -05:00
By default, `EXPOSE` assumes TCP. You can also specify UDP:
2020-03-17 10:01:52 -04:00
```dockerfile
2017-11-09 14:31:45 -05:00
EXPOSE 80/udp
```
To expose on both TCP and UDP, include two lines:
2020-03-17 10:01:52 -04:00
```dockerfile
2017-11-09 14:31:45 -05:00
EXPOSE 80/tcp
EXPOSE 80/udp
```
In this case, if you use `-P` with `docker run` , the port will be exposed once
for TCP and once for UDP. Remember that `-P` uses an ephemeral high-ordered host
port on the host, so the port will not be the same for TCP and UDP.
Regardless of the `EXPOSE` settings, you can override them at runtime by using
the `-p` flag. For example
2021-04-20 05:03:34 -04:00
```console
$ docker run -p 80:80/tcp -p 80:80/udp ...
2017-11-09 14:31:45 -05:00
```
2020-04-30 15:46:11 -04:00
To set up port redirection on the host system, see [using the -P flag ](run.md#expose-incoming-ports ).
The `docker network` command supports creating networks for communication among
containers without the need to expose or publish specific ports, because the
containers connected to the network can communicate with each other over any
port. For detailed information, see the
[overview of this feature ](https://docs.docker.com/engine/userguide/networking/ ).
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
## ENV
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
ENV < key > =< value > ...
```
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
The `ENV` instruction sets the environment variable `<key>` to the value
2018-01-17 16:55:07 -05:00
`<value>` . This value will be in the environment for all subsequent instructions
in the build stage and can be [replaced inline ](#environment-replacement ) in
2020-09-23 06:42:17 -04:00
many as well. The value will be interpreted for other environment variables, so
quote characters will be removed if they are not escaped. Like command line parsing,
2014-09-25 22:28:24 -04:00
quotes and backslashes can be used to include spaces within values.
2020-09-23 06:42:17 -04:00
Example:
2014-09-25 22:28:24 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
2020-09-23 06:42:17 -04:00
ENV MY_NAME="John Doe"
ENV MY_DOG=Rex\ The\ Dog
ENV MY_CAT=fluffy
2020-04-19 09:46:48 -04:00
```
2014-09-25 22:28:24 -04:00
2020-09-23 06:42:17 -04:00
The `ENV` instruction allows for multiple `<key>=<value> ...` variables to be set
at one time, and the example below will yield the same net results in the final
image:
2014-09-25 22:28:24 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
2020-09-23 06:42:17 -04:00
ENV MY_NAME="John Doe" MY_DOG=Rex\ The\ Dog \
MY_CAT=fluffy
2020-04-19 09:46:48 -04:00
```
2014-09-25 22:28:24 -04:00
2014-04-23 16:48:28 -04:00
The environment variables set using `ENV` will persist when a container is run
from the resulting image. You can view the values using `docker inspect` , and
change them using `docker run --env <key>=<value>` .
2014-04-15 20:53:12 -04:00
2020-09-23 07:05:00 -04:00
Environment variable persistence can cause unexpected side effects. For example,
setting `ENV DEBIAN_FRONTEND=noninteractive` changes the behavior of `apt-get` ,
and may confuse users of your image.
If an environment variable is only needed during build, and not in the final
image, consider setting a value for a single command instead:
```dockerfile
RUN DEBIAN_FRONTEND=noninteractive apt-get update & & apt-get install -y ...
```
Or using [`ARG` ](#arg ), which is not persisted in the final image:
```dockerfile
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update & & apt-get install -y ...
```
> **Alternative syntax**
2020-09-23 06:42:17 -04:00
>
2020-09-23 07:05:00 -04:00
> The `ENV` instruction also allows an alternative syntax `ENV <key> <value>`,
> omitting the `=`. For example:
2020-09-23 06:42:17 -04:00
>
> ```dockerfile
2020-09-23 07:05:00 -04:00
> ENV MY_VAR my-value
2020-09-23 06:42:17 -04:00
> ```
>
2020-09-23 07:05:00 -04:00
> This syntax does not allow for multiple environment-variables to be set in a
> single `ENV` instruction, and can be confusing. For example, the following
> sets a single environment variable (`ONE`) with value `"TWO= THREE=world"`:
2020-09-23 06:42:17 -04:00
>
> ```dockerfile
2020-09-23 07:05:00 -04:00
> ENV ONE TWO= THREE=world
2020-09-23 06:42:17 -04:00
> ```
2020-09-23 07:05:00 -04:00
>
> The alternative syntax is supported for backward compatibility, but discouraged
> for the reasons outlined above, and may be removed in a future release.
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
## ADD
2014-04-15 20:53:12 -04:00
2014-12-26 19:30:34 -05:00
ADD has two forms:
2020-04-19 09:46:48 -04:00
```dockerfile
ADD [--chown=< user > :< group > ] < src > ... < dest >
ADD [--chown=< user > :< group > ] ["< src > ",... "< dest > "]
```
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
The latter form is required for paths containing whitespace.
> **Note**
>
2017-08-23 12:22:23 -04:00
> The `--chown` feature is only supported on Dockerfiles used to build Linux containers,
> and will not work on Windows containers. Since user and group ownership concepts do
> not translate between Linux and Windows, the use of `/etc/passwd` and `/etc/group` for
2018-01-08 06:33:06 -05:00
> translating user and group names to IDs restricts this feature to only be viable
2017-08-23 12:22:23 -04:00
> for Linux OS-based containers.
2014-10-29 05:36:38 -04:00
The `ADD` instruction copies new files, directories or remote file URLs from `<src>`
2016-10-15 07:03:47 -04:00
and adds them to the filesystem of the image at the path `<dest>` .
2014-04-15 20:53:12 -04:00
2017-08-23 12:22:23 -04:00
Multiple `<src>` resources may be specified but if they are files or
directories, their paths are interpreted as relative to the source of
the context of the build.
2014-04-15 20:53:12 -04:00
2014-09-22 09:41:02 -04:00
Each `<src>` may contain wildcards and matching will be done using Go's
2015-09-06 18:48:05 -04:00
[filepath.Match ](http://golang.org/pkg/path/filepath#Match ) rules. For example:
2014-09-22 09:41:02 -04:00
2020-04-19 09:46:48 -04:00
To add all files starting with "hom":
```dockerfile
ADD hom* /mydir/
```
In the example below, `?` is replaced with any single character, e.g., "home.txt".
```dockerfile
ADD hom?.txt /mydir/
```
2014-09-22 09:41:02 -04:00
2014-12-12 13:32:11 -05:00
The `<dest>` is an absolute path, or a path relative to `WORKDIR` , into which
the source will be copied inside the destination container.
2020-04-19 09:46:48 -04:00
The example below uses a relative path, and adds "test.txt" to `<WORKDIR>/relativeDir/` :
```dockerfile
ADD test.txt relativeDir/
```
Whereas this example uses an absolute path, and adds "test.txt" to `/absoluteDir/`
```dockerfile
ADD test.txt /absoluteDir/
```
2014-04-15 20:53:12 -04:00
2017-03-21 15:22:29 -04:00
When adding files or directories that contain special characters (such as `[`
and `]` ), you need to escape those paths following the Golang rules to prevent
them from being treated as a matching pattern. For example, to add a file
named `arr[0].txt` , use the following;
2020-04-19 09:46:48 -04:00
```dockerfile
ADD arr[[]0].txt /mydir/
```
2017-03-21 15:22:29 -04:00
2017-08-23 12:22:23 -04:00
All new files and directories are created with a UID and GID of 0, unless the
optional `--chown` flag specifies a given username, groupname, or UID/GID
combination to request specific ownership of the content added. The
format of the `--chown` flag allows for either username and groupname strings
or direct integer UID and GID in any combination. Providing a username without
groupname or a UID without GID will use the same numeric UID as the GID. If a
username or groupname is provided, the container's root filesystem
`/etc/passwd` and `/etc/group` files will be used to perform the translation
from name to integer UID or GID respectively. The following examples show
valid definitions for the `--chown` flag:
2020-04-19 09:46:48 -04:00
```dockerfile
ADD --chown=55:mygroup files* /somedir/
ADD --chown=bin files* /somedir/
ADD --chown=1 files* /somedir/
ADD --chown=10:11 files* /somedir/
```
2017-08-23 12:22:23 -04:00
If the container root filesystem does not contain either `/etc/passwd` or
`/etc/group` files and either user or group names are used in the `--chown`
flag, the build will fail on the `ADD` operation. Using numeric IDs requires
no lookup and will not depend on container root filesystem content.
2014-05-20 16:39:46 -04:00
2014-07-11 22:28:36 -04:00
In the case where `<src>` is a remote file URL, the destination will
2014-10-22 14:16:42 -04:00
have permissions of 600. If the remote file being retrieved has an HTTP
`Last-Modified` header, the timestamp from that header will be used
2015-06-08 14:42:00 -04:00
to set the `mtime` on the destination file. However, like any other file
processed during an `ADD` , `mtime` will not be included in the determination
2014-10-22 14:16:42 -04:00
of whether or not the file has changed and the cache should be updated.
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
> **Note**
>
2014-07-11 22:28:36 -04:00
> If you build by passing a `Dockerfile` through STDIN (`docker
> build - < somefile`), there is no build context, so the `Dockerfile`
> can only contain a URL based `ADD` instruction. You can also pass a
> compressed archive through STDIN: (`docker build - < archive.tar.gz`),
> the `Dockerfile` at the root of the archive and the rest of the
2016-10-15 07:03:47 -04:00
> archive will be used as the context of the build.
2014-07-11 22:28:36 -04:00
2020-04-19 09:46:48 -04:00
If your URL files are protected using authentication, you need to use `RUN wget` ,
`RUN curl` or use another tool from within the container as the `ADD` instruction
does not support authentication.
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
> **Note**
>
2014-07-03 07:55:21 -04:00
> The first encountered `ADD` instruction will invalidate the cache for all
> following instructions from the Dockerfile if the contents of `<src>` have
> changed. This includes invalidating the cache for `RUN` instructions.
2014-09-29 11:34:58 -04:00
> See the [`Dockerfile` Best Practices
2019-11-11 14:56:31 -05:00
guide – Leverage build cache](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#leverage-build-cache)
2020-04-19 09:46:48 -04:00
> for more information.
2014-09-29 11:34:58 -04:00
2014-07-03 07:55:21 -04:00
2015-09-06 18:48:05 -04:00
`ADD` obeys the following rules:
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
- The `<src>` path must be inside the *context* of the build;
you cannot `ADD ../something /something` , because the first step of a
`docker build` is to send the context directory (and subdirectories) to the
docker daemon.
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
- If `<src>` is a URL and `<dest>` does not end with a trailing slash, then a
file is downloaded from the URL and copied to `<dest>` .
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
- If `<src>` is a URL and `<dest>` does end with a trailing slash, then the
filename is inferred from the URL and the file is downloaded to
`<dest>/<filename>` . For instance, `ADD http://example.com/foobar /` would
create the file `/foobar` . The URL must have a nontrivial path so that an
appropriate filename can be discovered in this case (`http://example.com`
will not work).
2014-04-15 20:53:12 -04:00
2015-09-30 16:11:36 -04:00
- If `<src>` is a directory, the entire contents of the directory are copied,
including filesystem metadata.
2015-09-06 18:48:05 -04:00
2020-04-19 09:46:48 -04:00
> **Note**
>
2014-11-16 08:13:14 -05:00
> The directory itself is not copied, just its contents.
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
- If `<src>` is a *local* tar archive in a recognized compression format
(identity, gzip, bzip2 or xz) then it is unpacked as a directory. Resources
from *remote* URLs are **not** decompressed. When a directory is copied or
2016-10-15 07:03:47 -04:00
unpacked, it has the same behavior as `tar -x` , the result is the union of:
2014-04-15 20:53:12 -04:00
2014-07-11 22:28:36 -04:00
1. Whatever existed at the destination path and
2. The contents of the source tree, with conflicts resolved in favor
of "2." on a file-by-file basis.
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
> **Note**
>
2016-01-22 11:52:36 -05:00
> Whether a file is identified as a recognized compression format or not
2016-02-11 18:21:52 -05:00
> is done solely based on the contents of the file, not the name of the file.
2016-01-22 11:52:36 -05:00
> For example, if an empty file happens to end with `.tar.gz` this will not
> be recognized as a compressed file and **will not** generate any kind of
> decompression error message, rather the file will simply be copied to the
> destination.
2014-04-23 16:48:28 -04:00
- If `<src>` is any other kind of file, it is copied individually along with
its metadata. In this case, if `<dest>` ends with a trailing slash `/` , it
will be considered a directory and the contents of `<src>` will be written
at `<dest>/base(<src>)` .
2014-04-15 20:53:12 -04:00
2014-09-22 09:41:02 -04:00
- If multiple `<src>` resources are specified, either directly or due to the
2015-09-30 16:11:36 -04:00
use of a wildcard, then `<dest>` must be a directory, and it must end with
2014-09-22 09:41:02 -04:00
a slash `/` .
2014-09-16 12:58:20 -04:00
2014-04-23 16:48:28 -04:00
- If `<dest>` does not end with a trailing slash, it will be considered a
regular file and the contents of `<src>` will be written at `<dest>` .
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
- If `<dest>` doesn't exist, it is created along with all missing directories
in its path.
2014-04-15 20:53:12 -04:00
2014-05-28 13:53:16 -04:00
## COPY
2014-12-26 19:30:34 -05:00
COPY has two forms:
2020-04-19 09:46:48 -04:00
```dockerfile
COPY [--chown=< user > :< group > ] < src > ... < dest >
COPY [--chown=< user > :< group > ] ["< src > ",... "< dest > "]
```
2014-05-28 13:53:16 -04:00
2020-04-19 09:46:48 -04:00
This latter form is required for paths containing whitespace
> **Note**
>
2017-08-23 12:22:23 -04:00
> The `--chown` feature is only supported on Dockerfiles used to build Linux containers,
> and will not work on Windows containers. Since user and group ownership concepts do
> not translate between Linux and Windows, the use of `/etc/passwd` and `/etc/group` for
> translating user and group names to IDs restricts this feature to only be viable for
2018-01-31 16:05:58 -05:00
> Linux OS-based containers.
2017-08-23 12:22:23 -04:00
2014-10-29 05:36:38 -04:00
The `COPY` instruction copies new files or directories from `<src>`
and adds them to the filesystem of the container at the path `<dest>` .
2014-05-28 13:53:16 -04:00
2017-08-23 12:22:23 -04:00
Multiple `<src>` resources may be specified but the paths of files and
directories will be interpreted as relative to the source of the context
of the build.
2014-05-28 13:53:16 -04:00
2014-09-22 09:41:02 -04:00
Each `<src>` may contain wildcards and matching will be done using Go's
2015-09-06 18:48:05 -04:00
[filepath.Match ](http://golang.org/pkg/path/filepath#Match ) rules. For example:
2014-09-22 09:41:02 -04:00
2020-04-19 09:46:48 -04:00
To add all files starting with "hom":
```dockerfile
COPY hom* /mydir/
```
In the example below, `?` is replaced with any single character, e.g., "home.txt".
```dockerfile
COPY hom?.txt /mydir/
```
2014-09-22 09:41:02 -04:00
2014-12-12 13:32:11 -05:00
The `<dest>` is an absolute path, or a path relative to `WORKDIR` , into which
the source will be copied inside the destination container.
2020-04-19 09:46:48 -04:00
The example below uses a relative path, and adds "test.txt" to `<WORKDIR>/relativeDir/` :
2014-05-28 13:53:16 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
COPY test.txt relativeDir/
```
Whereas this example uses an absolute path, and adds "test.txt" to `/absoluteDir/`
```dockerfile
COPY test.txt /absoluteDir/
```
2017-03-21 15:22:29 -04:00
When copying files or directories that contain special characters (such as `[`
and `]` ), you need to escape those paths following the Golang rules to prevent
2020-07-17 15:18:02 -04:00
them from being treated as a matching pattern. For example, to copy a file
2017-03-21 15:22:29 -04:00
named `arr[0].txt` , use the following;
2020-04-19 09:46:48 -04:00
```dockerfile
2020-07-17 15:18:02 -04:00
COPY arr[[]0].txt /mydir/
2020-04-19 09:46:48 -04:00
```
2017-03-21 15:22:29 -04:00
2017-08-23 12:22:23 -04:00
All new files and directories are created with a UID and GID of 0, unless the
optional `--chown` flag specifies a given username, groupname, or UID/GID
combination to request specific ownership of the copied content. The
format of the `--chown` flag allows for either username and groupname strings
or direct integer UID and GID in any combination. Providing a username without
groupname or a UID without GID will use the same numeric UID as the GID. If a
username or groupname is provided, the container's root filesystem
`/etc/passwd` and `/etc/group` files will be used to perform the translation
from name to integer UID or GID respectively. The following examples show
valid definitions for the `--chown` flag:
2020-04-19 09:46:48 -04:00
```dockerfile
COPY --chown=55:mygroup files* /somedir/
COPY --chown=bin files* /somedir/
COPY --chown=1 files* /somedir/
COPY --chown=10:11 files* /somedir/
```
2017-08-23 12:22:23 -04:00
If the container root filesystem does not contain either `/etc/passwd` or
`/etc/group` files and either user or group names are used in the `--chown`
flag, the build will fail on the `COPY` operation. Using numeric IDs requires
2020-04-19 09:46:48 -04:00
no lookup and does not depend on container root filesystem content.
2014-05-28 13:53:16 -04:00
2020-04-19 09:46:48 -04:00
> **Note**
>
2014-05-28 13:53:16 -04:00
> If you build using STDIN (`docker build - < somefile`), there is no
> build context, so `COPY` can't be used.
2020-07-27 18:56:23 -04:00
Optionally `COPY` accepts a flag `--from=<name>` that can be used to set
2017-04-04 15:31:00 -04:00
the source location to a previous build stage (created with `FROM .. AS <name>` )
2020-07-27 18:56:23 -04:00
that will be used instead of a build context sent by the user. In case a build
stage with a specified name can't be found an image with the same name is
attempted to be used instead.
2017-04-04 15:31:00 -04:00
2015-09-06 18:48:05 -04:00
`COPY` obeys the following rules:
2014-05-28 13:53:16 -04:00
- The `<src>` path must be inside the *context* of the build;
you cannot `COPY ../something /something` , because the first step of a
`docker build` is to send the context directory (and subdirectories) to the
docker daemon.
2015-09-30 16:11:36 -04:00
- If `<src>` is a directory, the entire contents of the directory are copied,
including filesystem metadata.
2015-09-06 18:48:05 -04:00
2020-04-19 09:46:48 -04:00
> **Note**
>
2014-11-16 08:13:14 -05:00
> The directory itself is not copied, just its contents.
2014-05-28 13:53:16 -04:00
- If `<src>` is any other kind of file, it is copied individually along with
its metadata. In this case, if `<dest>` ends with a trailing slash `/` , it
will be considered a directory and the contents of `<src>` will be written
at `<dest>/base(<src>)` .
2014-09-22 09:41:02 -04:00
- If multiple `<src>` resources are specified, either directly or due to the
2015-09-30 16:11:36 -04:00
use of a wildcard, then `<dest>` must be a directory, and it must end with
2014-09-22 09:41:02 -04:00
a slash `/` .
2014-09-16 12:58:20 -04:00
2014-05-28 13:53:16 -04:00
- If `<dest>` does not end with a trailing slash, it will be considered a
regular file and the contents of `<src>` will be written at `<dest>` .
- If `<dest>` doesn't exist, it is created along with all missing directories
in its path.
2019-11-11 14:56:31 -05:00
> **Note**
>
> The first encountered `COPY` instruction will invalidate the cache for all
> following instructions from the Dockerfile if the contents of `<src>` have
> changed. This includes invalidating the cache for `RUN` instructions.
> See the [`Dockerfile` Best Practices
guide – Leverage build cache](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#leverage-build-cache)
> for more information.
2014-05-28 13:53:16 -04:00
2014-04-23 16:48:28 -04:00
## ENTRYPOINT
2014-04-15 20:53:12 -04:00
ENTRYPOINT has two forms:
2020-04-19 09:46:48 -04:00
The *exec* form, which is the preferred form:
```dockerfile
ENTRYPOINT ["executable", "param1", "param2"]
```
The *shell* form:
```dockerfile
ENTRYPOINT command param1 param2
```
2014-04-15 20:53:12 -04:00
2014-10-21 22:30:59 -04:00
An `ENTRYPOINT` allows you to configure a container that will run as an executable.
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
For example, the following starts nginx with its default content, listening
2014-10-21 22:30:59 -04:00
on port 80:
2014-04-15 20:53:12 -04:00
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker run -i -t --rm -p 80:80 nginx
```
2014-04-15 20:53:12 -04:00
2014-10-21 22:30:59 -04:00
Command line arguments to `docker run <image>` will be appended after all
elements in an *exec* form `ENTRYPOINT` , and will override all elements specified
using `CMD` .
This allows arguments to be passed to the entry point, i.e., `docker run <image> -d`
2015-09-30 16:11:36 -04:00
will pass the `-d` argument to the entry point.
2014-10-21 22:30:59 -04:00
You can override the `ENTRYPOINT` instruction using the `docker run --entrypoint`
flag.
2014-04-15 20:53:12 -04:00
2014-10-21 22:30:59 -04:00
The *shell* form prevents any `CMD` or `run` command line arguments from being
used, but has the disadvantage that your `ENTRYPOINT` will be started as a
subcommand of `/bin/sh -c` , which does not pass signals.
This means that the executable will not be the container's `PID 1` - and
will _not_ receive Unix signals - so your executable will not receive a
`SIGTERM` from `docker stop <container>` .
2014-04-15 20:53:12 -04:00
2014-10-21 22:30:59 -04:00
Only the last `ENTRYPOINT` instruction in the `Dockerfile` will have an effect.
### Exec form ENTRYPOINT example
2014-04-15 20:53:12 -04:00
2014-10-21 22:30:59 -04:00
You can use the *exec* form of `ENTRYPOINT` to set fairly stable default commands
and arguments and then use either form of `CMD` to set additional defaults that
are more likely to be changed.
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
ENTRYPOINT ["top", "-b"]
CMD ["-c"]
```
2014-10-21 22:30:59 -04:00
When you run the container, you can see that `top` is the only process:
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker run -it --rm --name test top -H
2015-09-30 16:11:36 -04:00
2020-04-19 09:46:48 -04:00
top - 08:25:00 up 7:27, 0 users, load average: 0.00, 0.01, 0.05
Threads: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.1 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 2056668 total, 1616832 used, 439836 free, 99352 buffers
KiB Swap: 1441840 total, 0 used, 1441840 free. 1324440 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 19744 2336 2080 R 0.0 0.1 0:00.04 top
```
2015-09-30 16:11:36 -04:00
2014-10-21 22:30:59 -04:00
To examine the result further, you can use `docker exec` :
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker exec -it test ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 2.6 0.1 19752 2352 ? Ss+ 08:24 0:00 top -b -H
root 7 0.0 0.1 15572 2164 ? R+ 08:25 0:00 ps aux
```
2014-10-21 22:30:59 -04:00
And you can gracefully request `top` to shut down using `docker stop test` .
2014-11-18 14:42:54 -05:00
The following `Dockerfile` shows using the `ENTRYPOINT` to run Apache in the
foreground (i.e., as `PID 1` ):
2020-04-19 09:46:48 -04:00
```dockerfile
2014-11-18 14:42:54 -05:00
FROM debian:stable
RUN apt-get update & & apt-get install -y --force-yes apache2
EXPOSE 80 443
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
```
2014-10-21 22:30:59 -04:00
If you need to write a starter script for a single executable, you can ensure that
the final executable receives the Unix signals by using `exec` and `gosu`
2015-02-11 22:48:12 -05:00
commands:
2014-10-21 22:30:59 -04:00
```bash
2017-02-13 14:01:54 -05:00
#!/usr/bin/env bash
2014-10-21 22:30:59 -04:00
set -e
if [ "$1" = 'postgres' ]; then
chown -R postgres "$PGDATA"
if [ -z "$(ls -A "$PGDATA")" ]; then
gosu postgres initdb
fi
exec gosu postgres "$@"
fi
exec "$@"
```
Lastly, if you need to do some extra cleanup (or communicate with other containers)
on shutdown, or are co-ordinating more than one executable, you may need to ensure
that the `ENTRYPOINT` script receives the Unix signals, passes them on, and then
does some more work:
2020-04-19 09:46:48 -04:00
```bash
2014-10-21 22:30:59 -04:00
#!/bin/sh
# Note: I've written this using sh so it works in the busybox container too
# USE the trap if you need to also do manual cleanup after the service is stopped,
# or need to start multiple services in the one container
2016-05-11 14:49:24 -04:00
trap "echo TRAPed signal" HUP INT QUIT TERM
2014-10-21 22:30:59 -04:00
# start service in background here
/usr/sbin/apachectl start
echo "[hit enter key to exit] or run 'docker stop < container > '"
read
# stop service and clean up here
echo "stopping apache"
/usr/sbin/apachectl stop
echo "exited $0"
```
If you run this image with `docker run -it --rm -p 80:80 --name test apache` ,
you can then examine the container's processes with `docker exec` , or `docker top` ,
and then ask the script to stop Apache:
2021-04-20 05:03:34 -04:00
```console
2014-10-21 22:30:59 -04:00
$ docker exec -it test ps aux
2020-04-19 09:46:48 -04:00
2014-10-21 22:30:59 -04:00
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.1 0.0 4448 692 ? Ss+ 00:42 0:00 /bin/sh /run.sh 123 cmd cmd2
root 19 0.0 0.2 71304 4440 ? Ss 00:42 0:00 /usr/sbin/apache2 -k start
www-data 20 0.2 0.2 360468 6004 ? Sl 00:42 0:00 /usr/sbin/apache2 -k start
www-data 21 0.2 0.2 360468 6000 ? Sl 00:42 0:00 /usr/sbin/apache2 -k start
root 81 0.0 0.1 15572 2140 ? R+ 00:44 0:00 ps aux
2020-04-19 09:46:48 -04:00
2014-10-21 22:30:59 -04:00
$ docker top test
2020-04-19 09:46:48 -04:00
2014-10-21 22:30:59 -04:00
PID USER COMMAND
10035 root {run.sh} /bin/sh /run.sh 123 cmd cmd2
10054 root /usr/sbin/apache2 -k start
10055 33 /usr/sbin/apache2 -k start
10056 33 /usr/sbin/apache2 -k start
2020-04-19 09:46:48 -04:00
2014-10-21 22:30:59 -04:00
$ /usr/bin/time docker stop test
2020-04-19 09:46:48 -04:00
2014-10-21 22:30:59 -04:00
test
real 0m 0.27s
user 0m 0.03s
sys 0m 0.03s
```
2020-04-19 09:46:48 -04:00
> **Note**
>
> You can override the `ENTRYPOINT` setting using `--entrypoint`,
2014-10-21 22:30:59 -04:00
> but this can only set the binary to *exec* (no `sh -c` will be used).
2014-07-11 19:50:25 -04:00
2020-04-19 09:46:48 -04:00
> **Note**
>
2014-09-11 13:09:18 -04:00
> The *exec* form is parsed as a JSON array, which means that
> you must use double-quotes (") around words not single-quotes (').
2020-04-19 09:46:48 -04:00
Unlike the *shell* form, the *exec* form does not invoke a command shell.
This means that normal shell processing does not happen. For example,
`ENTRYPOINT [ "echo", "$HOME" ]` will not do variable substitution on `$HOME` .
If you want shell processing then either use the *shell* form or execute
a shell directly, for example: `ENTRYPOINT [ "sh", "-c", "echo $HOME" ]` .
When using the exec form and executing a shell directly, as in the case for
the shell form, it is the shell that is doing the environment variable
expansion, not docker.
2014-10-02 14:16:51 -04:00
2014-10-21 22:30:59 -04:00
### Shell form ENTRYPOINT example
You can specify a plain string for the `ENTRYPOINT` and it will execute in `/bin/sh -c` .
This form will use shell processing to substitute shell environment variables,
and will ignore any `CMD` or `docker run` command line arguments.
To ensure that `docker stop` will signal any long running `ENTRYPOINT` executable
correctly, you need to remember to start it with `exec` :
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
ENTRYPOINT exec top -b
```
2014-10-21 22:30:59 -04:00
When you run this image, you'll see the single `PID 1` process:
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker run -it --rm --name test top
2014-10-21 22:30:59 -04:00
2020-04-19 09:46:48 -04:00
Mem: 1704520K used, 352148K free, 0K shrd, 0K buff, 140368121167873K cached
CPU: 5% usr 0% sys 0% nic 94% idle 0% io 0% irq 0% sirq
Load average: 0.08 0.03 0.05 2/98 6
PID PPID USER STAT VSZ %VSZ %CPU COMMAND
1 0 root R 3164 0% 0% top -b
```
2014-10-21 22:30:59 -04:00
2020-04-19 09:46:48 -04:00
Which exits cleanly on `docker stop` :
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ /usr/bin/time docker stop test
test
real 0m 0.20s
user 0m 0.02s
sys 0m 0.04s
```
2014-10-21 22:30:59 -04:00
If you forget to add `exec` to the beginning of your `ENTRYPOINT` :
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
ENTRYPOINT top -b
CMD --ignored-param1
```
2014-10-21 22:30:59 -04:00
You can then run it (giving it a name for the next step):
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker run -it --name test top --ignored-param2
Mem: 1704184K used, 352484K free, 0K shrd, 0K buff, 140621524238337K cached
CPU: 9% usr 2% sys 0% nic 88% idle 0% io 0% irq 0% sirq
Load average: 0.01 0.02 0.05 2/101 7
PID PPID USER STAT VSZ %VSZ %CPU COMMAND
1 0 root S 3168 0% 0% /bin/sh -c top -b cmd cmd2
7 1 root R 3164 0% 0% top -b
```
2014-10-21 22:30:59 -04:00
You can see from the output of `top` that the specified `ENTRYPOINT` is not `PID 1` .
If you then run `docker stop test` , the container will not exit cleanly - the
`stop` command will be forced to send a `SIGKILL` after the timeout:
2021-04-20 05:03:34 -04:00
```console
2020-04-19 09:46:48 -04:00
$ docker exec -it test ps aux
PID USER COMMAND
1 root /bin/sh -c top -b cmd cmd2
7 root top -b
8 root ps aux
$ /usr/bin/time docker stop test
test
real 0m 10.19s
user 0m 0.04s
sys 0m 0.03s
```
2014-04-15 20:53:12 -04:00
2016-02-23 16:03:45 -05:00
### Understand how CMD and ENTRYPOINT interact
Both `CMD` and `ENTRYPOINT` instructions define what command gets executed when running a container.
There are few rules that describe their co-operation.
1. Dockerfile should specify at least one of `CMD` or `ENTRYPOINT` commands.
2. `ENTRYPOINT` should be defined when using the container as an executable.
3. `CMD` should be used as a way of defining default arguments for an `ENTRYPOINT` command
or for executing an ad-hoc command in a container.
4. `CMD` will be overridden when running the container with alternative arguments.
The table below shows what command is executed for different `ENTRYPOINT` / `CMD` combinations:
2016-08-16 16:08:07 -04:00
| | No ENTRYPOINT | ENTRYPOINT exec_entry p1_entry | ENTRYPOINT ["exec_entry", "p1_entry"] |
2017-08-23 17:33:03 -04:00
|:-------------------------------|:---------------------------|:-------------------------------|:-----------------------------------------------|
2016-08-16 16:08:07 -04:00
| **No CMD** | *error, not allowed* | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry |
| **CMD ["exec_cmd", "p1_cmd"]** | exec_cmd p1_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry exec_cmd p1_cmd |
| **CMD ["p1_cmd", "p2_cmd"]** | p1_cmd p2_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry p1_cmd p2_cmd |
| **CMD exec_cmd p1_cmd** | /bin/sh -c exec_cmd p1_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd |
2016-02-23 16:03:45 -05:00
2020-04-19 09:46:48 -04:00
> **Note**
>
> If `CMD` is defined from the base image, setting `ENTRYPOINT` will
2018-11-17 10:10:15 -05:00
> reset `CMD` to an empty value. In this scenario, `CMD` must be defined in the
> current image to have a value.
2014-04-23 16:48:28 -04:00
## VOLUME
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
VOLUME ["/data"]
```
2014-04-15 20:53:12 -04:00
2015-03-06 09:36:23 -05:00
The `VOLUME` instruction creates a mount point with the specified name
and marks it as holding externally mounted volumes from native host or other
2014-05-09 08:42:27 -04:00
containers. The value can be a JSON array, `VOLUME ["/var/log/"]` , or a plain
2014-09-11 09:27:51 -04:00
string with multiple arguments, such as `VOLUME /var/log` or `VOLUME /var/log
2015-07-17 14:40:56 -04:00
/var/db`. For more information/examples and mounting instructions via the
2015-09-30 16:11:36 -04:00
Docker client, refer to
2020-04-19 09:46:48 -04:00
[*Share Directories via Volumes* ](https://docs.docker.com/storage/volumes/ )
2014-09-11 09:27:51 -04:00
documentation.
2014-04-15 20:53:12 -04:00
2015-09-30 16:11:36 -04:00
The `docker run` command initializes the newly created volume with any data
that exists at the specified location within the base image. For example,
2015-03-06 09:36:23 -05:00
consider the following Dockerfile snippet:
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting
VOLUME /myvol
```
2015-03-06 09:36:23 -05:00
2017-11-11 06:36:43 -05:00
This Dockerfile results in an image that causes `docker run` to
2015-09-30 16:11:36 -04:00
create a new mount point at `/myvol` and copy the `greeting` file
2015-03-06 09:36:23 -05:00
into the newly created volume.
2017-04-25 13:18:16 -04:00
### Notes about specifying volumes
2017-03-21 15:30:04 -04:00
2017-04-25 13:18:16 -04:00
Keep the following things in mind about volumes in the `Dockerfile` .
2015-08-05 12:30:36 -04:00
2017-04-25 13:18:16 -04:00
- **Volumes on Windows-based containers**: When using Windows-based containers,
the destination of a volume inside the container must be one of:
2017-08-23 17:33:03 -04:00
2017-04-25 13:18:16 -04:00
- a non-existing or empty directory
- a drive other than `C:`
- **Changing the volume from within the Dockerfile**: If any build steps change the
data within the volume after it has been declared, those changes will be discarded.
- **JSON formatting**: The list is parsed as a JSON array.
2018-09-28 17:40:22 -04:00
You must enclose words with double quotes (`"`) rather than single quotes (`'`).
2017-04-25 13:18:16 -04:00
- **The host directory is declared at container run-time**: The host directory
(the mountpoint) is, by its nature, host-dependent. This is to preserve image
2017-11-11 06:36:43 -05:00
portability, since a given host directory can't be guaranteed to be available
on all hosts. For this reason, you can't mount a host directory from
2017-04-25 13:18:16 -04:00
within the Dockerfile. The `VOLUME` instruction does not support specifying a `host-dir`
parameter. You must specify the mountpoint when you create or run the container.
2014-09-11 13:09:18 -04:00
2014-04-23 16:48:28 -04:00
## USER
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
USER < user > [:< group > ]
```
2017-06-07 12:50:16 -04:00
or
2020-04-19 09:46:48 -04:00
```dockerfile
USER < UID > [:< GID > ]
```
2017-06-07 12:50:16 -04:00
The `USER` instruction sets the user name (or UID) and optionally the user
group (or GID) to use when running the image and for any `RUN` , `CMD` and
`ENTRYPOINT` instructions that follow it in the `Dockerfile` .
2019-09-16 11:35:13 -04:00
> Note that when specifying a group for the user, the user will have _only_ the
> specified group membership. Any other configured group memberships will be ignored.
2020-04-19 09:46:48 -04:00
> **Warning**
>
2017-11-16 06:34:52 -05:00
> When the user doesn't have a primary group then the image (or the next
2017-06-07 12:50:16 -04:00
> instructions) will be run with the `root` group.
2020-04-19 09:46:48 -04:00
>
2017-11-13 14:43:38 -05:00
> On Windows, the user must be created first if it's not a built-in account.
> This can be done with the `net user` command called as part of a Dockerfile.
2020-03-17 10:01:52 -04:00
```dockerfile
2020-04-19 09:46:48 -04:00
FROM microsoft/windowsservercore
# Create Windows user in the container
RUN net user /add patrick
# Set it for subsequent commands
USER patrick
2017-11-13 14:43:38 -05:00
```
2014-04-15 20:53:12 -04:00
2014-04-23 16:48:28 -04:00
## WORKDIR
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
WORKDIR /path/to/workdir
```
2014-04-15 20:53:12 -04:00
2015-01-26 08:29:49 -05:00
The `WORKDIR` instruction sets the working directory for any `RUN` , `CMD` ,
`ENTRYPOINT` , `COPY` and `ADD` instructions that follow it in the `Dockerfile` .
2016-06-11 15:33:09 -04:00
If the `WORKDIR` doesn't exist, it will be created even if it's not used in any
2016-03-19 08:20:40 -04:00
subsequent `Dockerfile` instruction.
2014-04-15 20:53:12 -04:00
2017-06-26 21:46:45 -04:00
The `WORKDIR` instruction can be used multiple times in a `Dockerfile` . If a
relative path is provided, it will be relative to the path of the previous
`WORKDIR` instruction. For example:
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd
```
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
The output of the final `pwd` command in this `Dockerfile` would be `/a/b/c` .
2014-04-15 20:53:12 -04:00
2014-07-15 13:17:20 -04:00
The `WORKDIR` instruction can resolve environment variables previously set using
`ENV` . You can only use environment variables explicitly set in the `Dockerfile` .
For example:
2020-04-19 09:46:48 -04:00
```dockerfile
2020-09-23 06:42:17 -04:00
ENV DIRPATH=/path
2020-04-19 09:46:48 -04:00
WORKDIR $DIRPATH/$DIRNAME
RUN pwd
```
2014-07-15 13:17:20 -04:00
The output of the final `pwd` command in this `Dockerfile` would be
`/path/$DIRNAME`
2014-11-14 13:59:14 -05:00
## ARG
2020-04-19 09:46:48 -04:00
```dockerfile
ARG < name > [=< default value > ]
```
2014-11-14 13:59:14 -05:00
The `ARG` instruction defines a variable that users can pass at build-time to
2017-01-20 17:14:15 -05:00
the builder with the `docker build` command using the `--build-arg <varname>=<value>`
flag. If a user specifies a build argument that was not
2016-10-15 23:37:15 -04:00
defined in the Dockerfile, the build outputs a warning.
2014-11-14 13:59:14 -05:00
2020-04-19 09:46:48 -04:00
```console
2016-10-15 23:37:15 -04:00
[Warning] One or more build-args [foo] were not consumed.
2014-11-14 13:59:14 -05:00
```
2017-07-13 14:26:37 -04:00
A Dockerfile may include one or more `ARG` instructions. For example,
the following is a valid Dockerfile:
2014-11-14 13:59:14 -05:00
2020-04-19 09:46:48 -04:00
```dockerfile
2014-11-14 13:59:14 -05:00
FROM busybox
ARG user1
ARG buildno
2020-04-19 09:46:48 -04:00
# ...
2014-11-14 13:59:14 -05:00
```
2020-04-19 09:46:48 -04:00
> **Warning:**
>
> It is not recommended to use build-time variables for passing secrets like
> github keys, user credentials etc. Build-time variable values are visible to
> any user of the image with the `docker history` command.
>
> Refer to the ["build images with BuildKit"](https://docs.docker.com/develop/develop-images/build_enhancements/#new-docker-build-secret-information)
> section to learn about secure ways to use secrets when building images.
2020-04-29 18:17:28 -04:00
{:.warning}
2017-07-13 14:26:37 -04:00
### Default values
An `ARG` instruction can optionally include a default value:
2014-11-14 13:59:14 -05:00
2020-04-19 09:46:48 -04:00
```dockerfile
2014-11-14 13:59:14 -05:00
FROM busybox
ARG user1=someuser
ARG buildno=1
2020-04-19 09:46:48 -04:00
# ...
2014-11-14 13:59:14 -05:00
```
2017-07-13 14:26:37 -04:00
If an `ARG` instruction has a default value and if there is no value passed
at build-time, the builder uses the default.
### Scope
2014-11-14 13:59:14 -05:00
An `ARG` variable definition comes into effect from the line on which it is
defined in the `Dockerfile` not from the argument's use on the command-line or
elsewhere. For example, consider this Dockerfile:
2020-04-19 09:46:48 -04:00
```dockerfile
FROM busybox
USER ${user:-some_user}
ARG user
USER $user
# ...
2014-11-14 13:59:14 -05:00
```
2021-04-20 05:03:34 -04:00
2014-11-14 13:59:14 -05:00
A user builds this file by calling:
2021-04-20 05:03:34 -04:00
```console
2017-04-10 15:45:21 -04:00
$ docker build --build-arg user=what_user .
2014-11-14 13:59:14 -05:00
```
The `USER` at line 2 evaluates to `some_user` as the `user` variable is defined on the
subsequent line 3. The `USER` at line 4 evaluates to `what_user` as `user` is
defined and the `what_user` value was passed on the command line. Prior to its definition by an
`ARG` instruction, any use of a variable results in an empty string.
2017-07-13 14:26:37 -04:00
An `ARG` instruction goes out of scope at the end of the build
stage where it was defined. To use an arg in multiple stages, each stage must
include the `ARG` instruction.
2020-04-19 09:46:48 -04:00
```dockerfile
2017-07-13 14:26:37 -04:00
FROM busybox
ARG SETTINGS
RUN ./run/setup $SETTINGS
FROM busybox
ARG SETTINGS
RUN ./run/other $SETTINGS
```
### Using ARG variables
2014-11-14 13:59:14 -05:00
You can use an `ARG` or an `ENV` instruction to specify variables that are
available to the `RUN` instruction. Environment variables defined using the
`ENV` instruction always override an `ARG` instruction of the same name. Consider
this Dockerfile with an `ENV` and `ARG` instruction.
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
ARG CONT_IMG_VER
2020-09-23 06:42:17 -04:00
ENV CONT_IMG_VER=v1.0.0
2020-04-19 09:46:48 -04:00
RUN echo $CONT_IMG_VER
2014-11-14 13:59:14 -05:00
```
2020-04-19 09:46:48 -04:00
2014-11-14 13:59:14 -05:00
Then, assume this image is built with this command:
2021-04-20 05:03:34 -04:00
```console
2017-04-10 15:45:21 -04:00
$ docker build --build-arg CONT_IMG_VER=v2.0.1 .
2014-11-14 13:59:14 -05:00
```
In this case, the `RUN` instruction uses `v1.0.0` instead of the `ARG` setting
passed by the user:`v2.0.1` This behavior is similar to a shell
script where a locally scoped variable overrides the variables passed as
arguments or inherited from environment, from its point of definition.
Using the example above but a different `ENV` specification you can create more
useful interactions between `ARG` and `ENV` instructions:
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
ARG CONT_IMG_VER
2020-09-23 06:42:17 -04:00
ENV CONT_IMG_VER=${CONT_IMG_VER:-v1.0.0}
2020-04-19 09:46:48 -04:00
RUN echo $CONT_IMG_VER
2014-11-14 13:59:14 -05:00
```
2015-09-16 04:47:10 -04:00
Unlike an `ARG` instruction, `ENV` values are always persisted in the built
2017-01-20 17:14:15 -05:00
image. Consider a docker build without the `--build-arg` flag:
2014-11-14 13:59:14 -05:00
2021-04-20 05:03:34 -04:00
```console
2017-04-10 15:45:21 -04:00
$ docker build .
2015-09-16 04:47:10 -04:00
```
2014-11-14 13:59:14 -05:00
2015-09-16 04:47:10 -04:00
Using this Dockerfile example, `CONT_IMG_VER` is still persisted in the image but
its value would be `v1.0.0` as it is the default set in line 3 by the `ENV` instruction.
The variable expansion technique in this example allows you to pass arguments
from the command line and persist them in the final image by leveraging the
`ENV` instruction. Variable expansion is only supported for [a limited set of
Dockerfile instructions.](#environment-replacement)
2014-11-14 13:59:14 -05:00
2017-07-13 14:26:37 -04:00
### Predefined ARGs
2014-11-14 13:59:14 -05:00
Docker has a set of predefined `ARG` variables that you can use without a
corresponding `ARG` instruction in the Dockerfile.
2020-04-19 09:46:48 -04:00
- `HTTP_PROXY`
- `http_proxy`
- `HTTPS_PROXY`
- `https_proxy`
- `FTP_PROXY`
- `ftp_proxy`
- `NO_PROXY`
- `no_proxy`
2021-04-30 05:53:53 -04:00
- `ALL_PROXY`
- `all_proxy`
2014-11-14 13:59:14 -05:00
2021-04-30 06:08:28 -04:00
To use these, pass them on the command line using the `--build-arg` flag, for
example:
2016-10-19 13:25:45 -04:00
2021-04-30 06:08:28 -04:00
```console
$ docker build --build-arg HTTPS_PROXY=https://my-proxy.example.com .
2016-10-19 13:25:45 -04:00
```
2014-11-14 13:59:14 -05:00
2017-03-06 09:46:15 -05:00
By default, these pre-defined variables are excluded from the output of
`docker history` . Excluding them reduces the risk of accidentally leaking
sensitive authentication information in an `HTTP_PROXY` variable.
For example, consider building the following Dockerfile using
`--build-arg HTTP_PROXY=http://user:pass@proxy.lon.example.com`
2020-04-19 09:46:48 -04:00
```dockerfile
2017-03-06 09:46:15 -05:00
FROM ubuntu
RUN echo "Hello World"
```
In this case, the value of the `HTTP_PROXY` variable is not available in the
`docker history` and is not cached. If you were to change location, and your
proxy server changed to `http://user:pass@proxy.sfo.example.com` , a subsequent
build does not result in a cache miss.
If you need to override this behaviour then you may do so by adding an `ARG`
statement in the Dockerfile as follows:
2020-04-19 09:46:48 -04:00
```dockerfile
2017-03-06 09:46:15 -05:00
FROM ubuntu
ARG HTTP_PROXY
RUN echo "Hello World"
```
When building this Dockerfile, the `HTTP_PROXY` is preserved in the
`docker history` , and changing its value invalidates the build cache.
2018-10-31 19:16:14 -04:00
### Automatic platform ARGs in the global scope
This feature is only available when using the [BuildKit ](#buildkit ) backend.
Docker predefines a set of `ARG` variables with information on the platform of
the node performing the build (build platform) and on the platform of the
resulting image (target platform). The target platform can be specified with
the `--platform` flag on `docker build` .
The following `ARG` variables are set automatically:
2020-04-19 09:46:48 -04:00
- `TARGETPLATFORM` - platform of the build result. Eg `linux/amd64` , `linux/arm/v7` , `windows/amd64` .
- `TARGETOS` - OS component of TARGETPLATFORM
- `TARGETARCH` - architecture component of TARGETPLATFORM
- `TARGETVARIANT` - variant component of TARGETPLATFORM
- `BUILDPLATFORM` - platform of the node performing the build.
- `BUILDOS` - OS component of BUILDPLATFORM
- `BUILDARCH` - architecture component of BUILDPLATFORM
- `BUILDVARIANT` - variant component of BUILDPLATFORM
2018-10-31 19:16:14 -04:00
These arguments are defined in the global scope so are not automatically
available inside build stages or for your `RUN` commands. To expose one of
these arguments inside the build stage redefine it without value.
For example:
2020-03-17 10:01:52 -04:00
```dockerfile
2018-10-31 19:16:14 -04:00
FROM alpine
ARG TARGETPLATFORM
RUN echo "I'm building for $TARGETPLATFORM"
```
2015-11-23 06:08:21 -05:00
### Impact on build caching
`ARG` variables are not persisted into the built image as `ENV` variables are.
However, `ARG` variables do impact the build cache in similar ways. If a
Dockerfile defines an `ARG` variable whose value is different from a previous
2016-04-08 05:23:48 -04:00
build, then a "cache miss" occurs upon its first usage, not its definition. In
particular, all `RUN` instructions following an `ARG` instruction use the `ARG`
variable implicitly (as an environment variable), thus can cause a cache miss.
2017-03-06 09:46:15 -05:00
All predefined `ARG` variables are exempt from caching unless there is a
matching `ARG` statement in the `Dockerfile` .
2016-03-16 03:26:57 -04:00
For example, consider these two Dockerfile:
2015-11-23 06:08:21 -05:00
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
ARG CONT_IMG_VER
RUN echo $CONT_IMG_VER
2015-11-23 06:08:21 -05:00
```
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
ARG CONT_IMG_VER
RUN echo hello
2016-03-16 03:26:57 -04:00
```
If you specify `--build-arg CONT_IMG_VER=<value>` on the command line, in both
cases, the specification on line 2 does not cause a cache miss; line 3 does
cause a cache miss.`ARG CONT_IMG_VER` causes the RUN line to be identified
2020-11-01 12:08:36 -05:00
as the same as running `CONT_IMG_VER=<value> echo hello` , so if the `<value>`
2016-03-16 03:26:57 -04:00
changes, we get a cache miss.
2015-11-23 06:08:21 -05:00
Consider another example under the same command line:
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
ARG CONT_IMG_VER
2020-09-23 06:42:17 -04:00
ENV CONT_IMG_VER=$CONT_IMG_VER
2020-04-19 09:46:48 -04:00
RUN echo $CONT_IMG_VER
2015-11-23 06:08:21 -05:00
```
2020-04-19 09:46:48 -04:00
2015-11-23 06:08:21 -05:00
In this example, the cache miss occurs on line 3. The miss happens because
the variable's value in the `ENV` references the `ARG` variable and that
variable is changed through the command line. In this example, the `ENV`
command causes the image to include the value.
2016-03-16 03:26:57 -04:00
If an `ENV` instruction overrides an `ARG` instruction of the same name, like
this Dockerfile:
2020-04-19 09:46:48 -04:00
```dockerfile
FROM ubuntu
ARG CONT_IMG_VER
2020-09-23 06:42:17 -04:00
ENV CONT_IMG_VER=hello
2020-04-19 09:46:48 -04:00
RUN echo $CONT_IMG_VER
2016-03-16 03:26:57 -04:00
```
Line 3 does not cause a cache miss because the value of `CONT_IMG_VER` is a
constant (`hello`). As a result, the environment variables and values used on
the `RUN` (line 4) doesn't change between builds.
2014-04-23 16:48:28 -04:00
## ONBUILD
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
ONBUILD < INSTRUCTION >
```
2014-04-15 20:53:12 -04:00
2014-07-11 22:28:36 -04:00
The `ONBUILD` instruction adds to the image a *trigger* instruction to
be executed at a later time, when the image is used as the base for
another build. The trigger will be executed in the context of the
downstream build, as if it had been inserted immediately after the
`FROM` instruction in the downstream `Dockerfile` .
2014-04-15 20:53:12 -04:00
Any build instruction can be registered as a trigger.
This is useful if you are building an image which will be used as a base
to build other images, for example an application build environment or a
daemon which may be customized with user-specific configuration.
2014-07-11 22:28:36 -04:00
For example, if your image is a reusable Python application builder, it
2014-04-15 20:53:12 -04:00
will require application source code to be added in a particular
directory, and it might require a build script to be called *after*
2014-07-11 22:28:36 -04:00
that. You can't just call `ADD` and `RUN` now, because you don't yet
2014-04-15 20:53:12 -04:00
have access to the application source code, and it will be different for
each application build. You could simply provide application developers
2014-07-11 22:28:36 -04:00
with a boilerplate `Dockerfile` to copy-paste into their application, but
2014-04-15 20:53:12 -04:00
that is inefficient, error-prone and difficult to update because it
mixes with application-specific code.
2014-07-11 22:28:36 -04:00
The solution is to use `ONBUILD` to register advance instructions to
2014-04-15 20:53:12 -04:00
run later, during the next build stage.
2014-04-23 16:48:28 -04:00
Here's how it works:
2014-07-11 22:28:36 -04:00
1. When it encounters an `ONBUILD` instruction, the builder adds a
2014-04-23 16:48:28 -04:00
trigger to the metadata of the image being built. The instruction
does not otherwise affect the current build.
2. At the end of the build, a list of all triggers is stored in the
2014-07-11 22:28:36 -04:00
image manifest, under the key `OnBuild` . They can be inspected with
the `docker inspect` command.
2014-04-23 16:48:28 -04:00
3. Later the image may be used as a base for a new build, using the
2014-07-11 22:28:36 -04:00
`FROM` instruction. As part of processing the `FROM` instruction,
the downstream builder looks for `ONBUILD` triggers, and executes
2014-04-23 16:48:28 -04:00
them in the same order they were registered. If any of the triggers
2014-07-11 22:28:36 -04:00
fail, the `FROM` instruction is aborted which in turn causes the
build to fail. If all triggers succeed, the `FROM` instruction
2014-04-23 16:48:28 -04:00
completes and the build continues as usual.
4. Triggers are cleared from the final image after being executed. In
other words they are not inherited by "grand-children" builds.
2014-04-15 20:53:12 -04:00
For example you might add something like this:
2020-04-19 09:46:48 -04:00
```dockerfile
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
```
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
> **Warning**
>
> Chaining `ONBUILD` instructions using `ONBUILD ONBUILD` isn't allowed.
2014-04-15 20:53:12 -04:00
2020-04-19 09:46:48 -04:00
> **Warning**
>
> The `ONBUILD` instruction may not trigger `FROM` or `MAINTAINER` instructions.
2014-04-15 20:53:12 -04:00
2015-08-18 13:30:44 -04:00
## STOPSIGNAL
2020-04-19 09:46:48 -04:00
```dockerfile
STOPSIGNAL signal
```
2015-08-18 13:30:44 -04:00
2021-08-21 07:50:05 -04:00
The `STOPSIGNAL` instruction sets the system call signal that will be sent to the
container to exit. This signal can be a signal name in the format `SIG<NAME>` ,
for instance `SIGKILL` , or an unsigned number that matches a position in the
kernel's syscall table, for instance `9` . The default is `SIGTERM` if not
defined.
The image's default stopsignal can be overridden per container, using the
`--stop-signal` flag on `docker run` and `docker create` .
2015-08-18 13:30:44 -04:00
2016-04-18 05:48:13 -04:00
## HEALTHCHECK
The `HEALTHCHECK` instruction has two forms:
2020-04-19 09:46:48 -04:00
- `HEALTHCHECK [OPTIONS] CMD command` (check container health by running a command inside the container)
- `HEALTHCHECK NONE` (disable any healthcheck inherited from the base image)
2016-04-18 05:48:13 -04:00
The `HEALTHCHECK` instruction tells Docker how to test a container to check that
it is still working. This can detect cases such as a web server that is stuck in
an infinite loop and unable to handle new connections, even though the server
process is still running.
When a container has a healthcheck specified, it has a _health status_ in
addition to its normal status. This status is initially `starting` . Whenever a
health check passes, it becomes `healthy` (whatever state it was previously in).
After a certain number of consecutive failures, it becomes `unhealthy` .
The options that can appear before `CMD` are:
2020-04-19 09:46:48 -04:00
- `--interval=DURATION` (default: `30s` )
- `--timeout=DURATION` (default: `30s` )
- `--start-period=DURATION` (default: `0s` )
- `--retries=N` (default: `3` )
2016-04-18 05:48:13 -04:00
The health check will first run **interval** seconds after the container is
started, and then again **interval** seconds after each previous check completes.
If a single run of the check takes longer than **timeout** seconds then the check
is considered to have failed.
It takes **retries** consecutive failures of the health check for the container
to be considered `unhealthy` .
2016-11-29 04:58:47 -05:00
**start period** provides initialization time for containers that need time to bootstrap.
Probe failure during that period will not be counted towards the maximum number of retries.
However, if a health check succeeds during the start period, the container is considered
started and all consecutive failures will be counted towards the maximum number of retries.
2016-04-18 05:48:13 -04:00
There can only be one `HEALTHCHECK` instruction in a Dockerfile. If you list
more than one then only the last `HEALTHCHECK` will take effect.
The command after the `CMD` keyword can be either a shell command (e.g. `HEALTHCHECK
CMD /bin/check-running`) or an _exec_ array (as with other Dockerfile commands;
see e.g. `ENTRYPOINT` for details).
The command's exit status indicates the health status of the container.
The possible values are:
- 0: success - the container is healthy and ready for use
- 1: unhealthy - the container is not working correctly
2016-07-20 13:50:04 -04:00
- 2: reserved - do not use this exit code
2016-04-18 05:48:13 -04:00
For example, to check every five minutes or so that a web-server is able to
serve the site's main page within three seconds:
2020-04-19 09:46:48 -04:00
```dockerfile
HEALTHCHECK --interval=5m --timeout=3s \
CMD curl -f http://localhost/ || exit 1
```
2016-04-18 05:48:13 -04:00
To help debug failing probes, any output text (UTF-8 encoded) that the command writes
on stdout or stderr will be stored in the health status and can be queried with
`docker inspect` . Such output should be kept short (only the first 4096 bytes
are stored currently).
When the health status of a container changes, a `health_status` event is
generated with the new status.
2016-05-03 16:56:59 -04:00
## SHELL
2020-04-19 09:46:48 -04:00
```dockerfile
SHELL ["executable", "parameters"]
```
2016-06-11 15:33:09 -04:00
2016-05-03 16:56:59 -04:00
The `SHELL` instruction allows the default shell used for the *shell* form of
commands to be overridden. The default shell on Linux is `["/bin/sh", "-c"]` , and on
Windows is `["cmd", "/S", "/C"]` . The `SHELL` instruction *must* be written in JSON
form in a Dockerfile.
The `SHELL` instruction is particularly useful on Windows where there are
two commonly used and quite different native shells: `cmd` and `powershell` , as
well as alternate shells available including `sh` .
2016-06-11 15:33:09 -04:00
The `SHELL` instruction can appear multiple times. Each `SHELL` instruction overrides
2016-05-03 16:56:59 -04:00
all previous `SHELL` instructions, and affects all subsequent instructions. For example:
2020-04-19 09:46:48 -04:00
```dockerfile
FROM microsoft/windowsservercore
2016-06-11 15:33:09 -04:00
2020-04-19 09:46:48 -04:00
# Executed as cmd /S /C echo default
RUN echo default
2016-06-11 15:33:09 -04:00
2020-04-19 09:46:48 -04:00
# Executed as cmd /S /C powershell -command Write-Host default
RUN powershell -command Write-Host default
2016-06-11 15:33:09 -04:00
2020-04-19 09:46:48 -04:00
# Executed as powershell -command Write-Host hello
SHELL ["powershell", "-command"]
RUN Write-Host hello
2016-06-11 15:33:09 -04:00
2020-04-19 09:46:48 -04:00
# Executed as cmd /S /C echo hello
SHELL ["cmd", "/S", "/C"]
RUN echo hello
```
2016-05-03 16:56:59 -04:00
The following instructions can be affected by the `SHELL` instruction when the
*shell* form of them is used in a Dockerfile: `RUN` , `CMD` and `ENTRYPOINT` .
2016-06-11 15:33:09 -04:00
The following example is a common pattern found on Windows which can be
streamlined by using the `SHELL` instruction:
2016-05-03 16:56:59 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
RUN powershell -command Execute-MyCmdlet -param1 "c:\foo.txt"
```
2016-05-03 16:56:59 -04:00
The command invoked by docker will be:
2020-04-19 09:46:48 -04:00
```powershell
cmd /S /C powershell -command Execute-MyCmdlet -param1 "c:\foo.txt"
```
2016-06-11 15:33:09 -04:00
2017-01-20 17:14:15 -05:00
This is inefficient for two reasons. First, there is an un-necessary cmd.exe command
processor (aka shell) being invoked. Second, each `RUN` instruction in the *shell*
form requires an extra `powershell -command` prefixing the command.
2016-06-11 15:33:09 -04:00
2016-05-03 16:56:59 -04:00
To make this more efficient, one of two mechanisms can be employed. One is to
use the JSON form of the RUN command such as:
2020-04-19 09:46:48 -04:00
```dockerfile
RUN ["powershell", "-command", "Execute-MyCmdlet", "-param1 \"c:\\foo.txt\""]
```
2016-05-03 16:56:59 -04:00
2016-06-11 15:33:09 -04:00
While the JSON form is unambiguous and does not use the un-necessary cmd.exe,
2016-05-03 16:56:59 -04:00
it does require more verbosity through double-quoting and escaping. The alternate
mechanism is to use the `SHELL` instruction and the *shell* form,
2016-06-11 15:33:09 -04:00
making a more natural syntax for Windows users, especially when combined with
2016-05-03 16:56:59 -04:00
the `escape` parser directive:
2016-06-11 15:33:09 -04:00
2020-04-19 09:46:48 -04:00
```dockerfile
# escape=`
2016-06-11 15:33:09 -04:00
2020-04-19 09:46:48 -04:00
FROM microsoft/nanoserver
SHELL ["powershell","-command"]
RUN New-Item -ItemType Directory C:\Example
ADD Execute-MyCmdlet.ps1 c:\example\
RUN c:\example\Execute-MyCmdlet -sample 'hello world'
```
2016-05-03 16:56:59 -04:00
Resulting in:
2021-04-20 05:03:34 -04:00
```console
PS E:\myproject> docker build -t shell .
2020-04-19 09:46:48 -04:00
Sending build context to Docker daemon 4.096 kB
Step 1/5 : FROM microsoft/nanoserver
---> 22738ff49c6d
Step 2/5 : SHELL powershell -command
---> Running in 6fcdb6855ae2
---> 6331462d4300
Removing intermediate container 6fcdb6855ae2
Step 3/5 : RUN New-Item -ItemType Directory C:\Example
---> Running in d0eef8386e97
2017-08-23 17:33:03 -04:00
2020-04-19 09:46:48 -04:00
Directory: C:\
2017-08-23 17:33:03 -04:00
2021-04-21 05:47:31 -04:00
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 10/28/2016 11:26 AM Example
2017-08-23 17:33:03 -04:00
2020-04-19 09:46:48 -04:00
---> 3f2fbf1395d9
Removing intermediate container d0eef8386e97
Step 4/5 : ADD Execute-MyCmdlet.ps1 c:\example\
---> a955b2621c31
Removing intermediate container b825593d39fc
Step 5/5 : RUN c:\example\Execute-MyCmdlet 'hello world'
---> Running in be6d8e63fe75
hello world
---> 8e559e9bf424
Removing intermediate container be6d8e63fe75
Successfully built 8e559e9bf424
2021-04-20 05:03:34 -04:00
PS E:\myproject>
2020-04-19 09:46:48 -04:00
```
2016-05-03 16:56:59 -04:00
The `SHELL` instruction could also be used to modify the way in which
a shell operates. For example, using `SHELL cmd /S /C /V:ON|OFF` on Windows, delayed
environment variable expansion semantics could be modified.
2016-06-11 15:33:09 -04:00
2016-05-03 16:56:59 -04:00
The `SHELL` instruction can also be used on Linux should an alternate shell be
2016-10-15 07:03:47 -04:00
required such as `zsh` , `csh` , `tcsh` and others.
2016-05-03 16:56:59 -04:00
2015-04-21 11:50:09 -04:00
## Dockerfile examples
2014-04-15 20:53:12 -04:00
2021-04-21 05:45:35 -04:00
For examples of Dockerfiles, refer to:
2015-10-07 06:45:46 -04:00
2021-04-21 05:45:35 -04:00
- The ["build images" section ](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ )
- The ["get started ](https://docs.docker.com/get-started/ )
- The [language-specific getting started guides ](https://docs.docker.com/language/ )