|
|
54481b |
% "CONTAINERFILE" "5" "Aug 2021" "" "Container User Manuals"
|
|
|
54481b |
|
|
|
54481b |
# NAME
|
|
|
54481b |
|
|
|
54481b |
Containerfile(Dockerfile) - automate the steps of creating a container image
|
|
|
54481b |
|
|
|
54481b |
# INTRODUCTION
|
|
|
54481b |
|
|
|
54481b |
The **Containerfile** is a configuration file that automates the steps of creating a container image. It is similar to a Makefile. Container engines (Podman, Buildah, Docker) read instructions from the **Containerfile** to automate the steps otherwise performed manually to create an image. To build an image, create a file called **Containerfile**.
|
|
|
54481b |
|
|
|
54481b |
The **Containerfile** describes the steps taken to assemble the image. When the
|
|
|
54481b |
**Containerfile** has been created, call the `buildah bud`, `podman build`, `docker build` command,
|
|
|
54481b |
using the path of context directory that contains **Containerfile** as the argument. Podman and Buildah default to **Containerfile** and will fall back to **Dockerfile**. Docker only will search for **Dockerfile** in the context directory.
|
|
|
54481b |
|
|
|
54481b |
|
|
|
54481b |
**Dockerfile** is an alternate name for the same object. **Containerfile** and **Dockerfile** support the same syntax.
|
|
|
54481b |
|
|
|
54481b |
# SYNOPSIS
|
|
|
54481b |
|
|
|
54481b |
INSTRUCTION arguments
|
|
|
54481b |
|
|
|
54481b |
For example:
|
|
|
54481b |
|
|
|
54481b |
FROM image
|
|
|
54481b |
|
|
|
54481b |
# DESCRIPTION
|
|
|
54481b |
|
|
|
54481b |
A Containerfile is a file that automates the steps of creating a container image.
|
|
|
54481b |
A Containerfile is similar to a Makefile.
|
|
|
54481b |
|
|
|
54481b |
# USAGE
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
buildah bud .
|
|
|
54481b |
podman build .
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
-- Runs the steps and commits them, building a final image.
|
|
|
54481b |
The path to the source repository defines where to find the context of the
|
|
|
54481b |
build.
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
buildah bud -t repository/tag .
|
|
|
54481b |
podman build -t repository/tag .
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
-- specifies a repository and tag at which to save the new image if the build
|
|
|
54481b |
succeeds. The container engine runs the steps one-by-one, committing the result
|
|
|
54481b |
to a new image if necessary, before finally outputting the ID of the new
|
|
|
54481b |
image.
|
|
|
54481b |
|
|
|
54481b |
Container engines re-use intermediate images whenever possible. This significantly
|
|
|
54481b |
accelerates the *build* process.
|
|
|
54481b |
|
|
|
54481b |
# FORMAT
|
|
|
54481b |
|
|
|
54481b |
`FROM image`
|
|
|
54481b |
|
|
|
54481b |
`FROM image:tag`
|
|
|
54481b |
|
|
|
54481b |
`FROM image@digest`
|
|
|
54481b |
|
|
|
54481b |
-- The **FROM** instruction sets the base image for subsequent instructions. A
|
|
|
54481b |
valid Containerfile must have either **ARG** or *FROM** as its first instruction.
|
|
|
54481b |
If **FROM** is not the first instruction in the file, it may only be preceded by
|
|
|
54481b |
one or more ARG instructions, which declare arguments that are used in the next FROM line in the Containerfile.
|
|
|
54481b |
The image can be any valid image. It is easy to start by pulling an image from the public
|
|
|
54481b |
repositories.
|
|
|
54481b |
|
|
|
54481b |
-- **FROM** must appear at least once in the Containerfile.
|
|
|
54481b |
|
|
|
54481b |
-- **FROM** The first **FROM** command must come before all other instructions in
|
|
|
54481b |
the Containerfile except **ARG**
|
|
|
54481b |
|
|
|
54481b |
-- **FROM** may appear multiple times within a single Containerfile in order to create
|
|
|
54481b |
multiple images. Make a note of the last image ID output by the commit before
|
|
|
54481b |
each new **FROM** command.
|
|
|
54481b |
|
|
|
54481b |
-- If no tag is given to the **FROM** instruction, container engines apply the
|
|
|
54481b |
`latest` tag. If the used tag does not exist, an error is returned.
|
|
|
54481b |
|
|
|
54481b |
-- If no digest is given to the **FROM** instruction, container engines apply the
|
|
|
54481b |
`latest` tag. If the used tag does not exist, an error is returned.
|
|
|
54481b |
|
|
|
54481b |
**MAINTAINER**
|
|
|
54481b |
-- **MAINTAINER** sets the Author field for the generated images.
|
|
|
54481b |
Useful for providing users with an email or url for support.
|
|
|
54481b |
|
|
|
54481b |
**RUN**
|
|
|
54481b |
-- **RUN** has two forms:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
# the command is run in a shell - /bin/sh -c
|
|
|
54481b |
RUN <command>
|
|
|
54481b |
|
|
|
54481b |
# Executable form
|
|
|
54481b |
RUN ["executable", "param1", "param2"]
|
|
|
54481b |
```
|
|
|
54481b |
**RUN mounts**
|
|
|
54481b |
|
|
|
54481b |
**--mount**=*type=TYPE,TYPE-SPECIFIC-OPTION[,...]*
|
|
|
54481b |
|
|
|
54481b |
Attach a filesystem mount to the container
|
|
|
54481b |
|
|
|
54481b |
Current supported mount TYPES are bind, cache, secret and tmpfs.
|
|
|
54481b |
|
|
|
54481b |
e.g.
|
|
|
54481b |
|
|
|
54481b |
mount=type=bind,source=/path/on/host,destination=/path/in/container
|
|
|
54481b |
|
|
|
54481b |
mount=type=tmpfs,tmpfs-size=512M,destination=/path/in/container
|
|
|
54481b |
|
|
|
54481b |
mount=type=secret,id=mysecret cat /run/secrets/mysecret
|
|
|
54481b |
|
|
|
54481b |
Common Options:
|
|
|
54481b |
|
|
|
54481b |
· src, source: mount source spec for bind and volume. Mandatory for bind. If `from` is specified, `src` is the subpath in the `from` field.
|
|
|
54481b |
|
|
|
54481b |
· dst, destination, target: mount destination spec.
|
|
|
54481b |
|
|
|
54481b |
· ro, read-only: true or false (default).
|
|
|
54481b |
|
|
|
54481b |
Options specific to bind:
|
|
|
54481b |
|
|
|
54481b |
· bind-propagation: shared, slave, private, rshared, rslave, or rprivate(default). See also mount(2).
|
|
|
54481b |
|
|
|
54481b |
. bind-nonrecursive: do not setup a recursive bind mount. By default it is recursive.
|
|
|
54481b |
|
|
|
54481b |
· from: stage or image name for the root of the source. Defaults to the build context.
|
|
|
54481b |
|
|
|
54481b |
Options specific to tmpfs:
|
|
|
54481b |
|
|
|
54481b |
· tmpfs-size: Size of the tmpfs mount in bytes. Unlimited by default in Linux.
|
|
|
54481b |
|
|
|
54481b |
· tmpfs-mode: File mode of the tmpfs in octal. (e.g. 700 or 0700.) Defaults to 1777 in Linux.
|
|
|
54481b |
|
|
|
54481b |
· tmpcopyup: Path that is shadowed by the tmpfs mount is recursively copied up to the tmpfs itself.
|
|
|
54481b |
|
|
|
54481b |
Options specific to cache:
|
|
|
54481b |
|
|
|
54481b |
· id: Create a separate cache directory for a particular id.
|
|
|
54481b |
|
|
|
54481b |
· mode: File mode for new cache directory in octal. Default 0755.
|
|
|
54481b |
|
|
|
54481b |
· ro, readonly: read only cache if set.
|
|
|
54481b |
|
|
|
54481b |
· uid: uid for cache directory.
|
|
|
54481b |
|
|
|
54481b |
· gid: gid for cache directory.
|
|
|
54481b |
|
|
|
54481b |
· from: stage name for the root of the source. Defaults to host cache directory.
|
|
|
54481b |
|
|
|
54481b |
|
|
|
54481b |
**RUN Secrets**
|
|
|
54481b |
|
|
|
54481b |
The RUN command has a feature to allow the passing of secret information into the image build. These secrets files can be used during the RUN command but are not committed to the final image. The `RUN` command supports the `--mount` option to identify the secret file. A secret file from the host is mounted into the container while the image is being built.
|
|
|
54481b |
|
|
|
54481b |
Container engines pass secret the secret file into the build using the `--secret` flag.
|
|
|
54481b |
|
|
|
54481b |
**--mount**=*type=secret,TYPE-SPECIFIC-OPTION[,...]*
|
|
|
54481b |
|
|
|
54481b |
- `id` is the identifier for the secret passed into the `buildah bud --secret` or `podman build --secret`. This identifier is associated with the RUN --mount identifier to use in the Containerfile.
|
|
|
54481b |
|
|
|
54481b |
- `dst`|`target`|`destination` rename the secret file to a specific file in the Containerfile RUN command to use.
|
|
|
54481b |
|
|
|
54481b |
- `type=secret` tells the --mount command that it is mounting in a secret file
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
# shows secret from default secret location:
|
|
|
54481b |
RUN --mount=type=secret,id=mysecret cat /run/secrets/mysecret
|
|
|
54481b |
```
|
|
|
54481b |
```
|
|
|
54481b |
# shows secret from custom secret location:
|
|
|
54481b |
RUN --mount=type=secret,id=mysecret,dst=/foobar cat /foobar
|
|
|
54481b |
```
|
|
|
54481b |
The secret needs to be passed to the build using the --secret flag. The final image built does not container the secret file:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
buildah bud --no-cache --secret id=mysecret,src=mysecret.txt .
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
-- The **RUN** instruction executes any commands in a new layer on top of the current
|
|
|
54481b |
image and commits the results. The committed image is used for the next step in
|
|
|
54481b |
Containerfile.
|
|
|
54481b |
|
|
|
54481b |
-- Layering **RUN** instructions and generating commits conforms to the core
|
|
|
54481b |
concepts of container engines where commits are cheap and containers can be created from
|
|
|
54481b |
any point in the history of an image. This is similar to source control. The
|
|
|
54481b |
exec form makes it possible to avoid shell string munging. The exec form makes
|
|
|
54481b |
it possible to **RUN** commands using a base image that does not contain `/bin/sh`.
|
|
|
54481b |
|
|
|
54481b |
Note that the exec form is parsed as a JSON array, which means that you must
|
|
|
54481b |
use double-quotes (") around words, not single-quotes (').
|
|
|
54481b |
|
|
|
54481b |
**CMD**
|
|
|
54481b |
-- **CMD** has three forms:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
# Executable form
|
|
|
54481b |
CMD ["executable", "param1", "param2"]`
|
|
|
54481b |
|
|
|
54481b |
# Provide default arguments to ENTRYPOINT
|
|
|
54481b |
CMD ["param1", "param2"]`
|
|
|
54481b |
|
|
|
54481b |
# the command is run in a shell - /bin/sh -c
|
|
|
54481b |
CMD command param1 param2
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
-- There should be only one **CMD** in a Containerfile. If more than one **CMD** is listed, only
|
|
|
54481b |
the last **CMD** takes effect.
|
|
|
54481b |
The main purpose of a **CMD** is to provide defaults for an executing container.
|
|
|
54481b |
These defaults may include an executable, or they can omit the executable. If
|
|
|
54481b |
they omit the executable, an **ENTRYPOINT** must be specified.
|
|
|
54481b |
When used in the shell or exec formats, the **CMD** instruction sets the command to
|
|
|
54481b |
be executed when running the image.
|
|
|
54481b |
If you use the shell form of the **CMD**, the `<command>` executes in `/bin/sh -c`:
|
|
|
54481b |
|
|
|
54481b |
Note that the exec form is parsed as a JSON array, which means that you must
|
|
|
54481b |
use double-quotes (") around words, not single-quotes (').
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
FROM ubuntu
|
|
|
54481b |
CMD echo "This is a test." | wc -
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
-- If you run **command** without a shell, then you must express the command as a
|
|
|
54481b |
JSON array and give the full path to the executable. This array form is the
|
|
|
54481b |
preferred form of **CMD**. All additional parameters must be individually expressed
|
|
|
54481b |
as strings in the array:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
FROM ubuntu
|
|
|
54481b |
CMD ["/usr/bin/wc","--help"]
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
-- To make the container run the same executable every time, use **ENTRYPOINT** in
|
|
|
54481b |
combination with **CMD**.
|
|
|
54481b |
If the user specifies arguments to `podman run` or `docker run`, the specified commands
|
|
|
54481b |
override the default in **CMD**.
|
|
|
54481b |
Do not confuse **RUN** with **CMD**. **RUN** runs a command and commits the result.
|
|
|
54481b |
**CMD** executes nothing at build time, but specifies the intended command for
|
|
|
54481b |
the image.
|
|
|
54481b |
|
|
|
54481b |
**LABEL**
|
|
|
54481b |
-- `LABEL <key>=<value> [<key>=<value> ...]`or
|
|
|
54481b |
```
|
|
|
54481b |
LABEL <key>[ <value>]
|
|
|
54481b |
LABEL <key>[ <value>]
|
|
|
54481b |
...
|
|
|
54481b |
```
|
|
|
54481b |
The **LABEL** instruction adds metadata to an image. A **LABEL** is a
|
|
|
54481b |
key-value pair. To specify a **LABEL** without a value, simply use an empty
|
|
|
54481b |
string. To include spaces within a **LABEL** value, use quotes and
|
|
|
54481b |
backslashes as you would in command-line parsing.
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
LABEL com.example.vendor="ACME Incorporated"
|
|
|
54481b |
LABEL com.example.vendor "ACME Incorporated"
|
|
|
54481b |
LABEL com.example.vendor.is-beta ""
|
|
|
54481b |
LABEL com.example.vendor.is-beta=
|
|
|
54481b |
LABEL com.example.vendor.is-beta=""
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
An image can have more than one label. To specify multiple labels, separate
|
|
|
54481b |
each key-value pair by a space.
|
|
|
54481b |
|
|
|
54481b |
Labels are additive including `LABEL`s in `FROM` images. As the system
|
|
|
54481b |
encounters and then applies a new label, new `key`s override any previous
|
|
|
54481b |
labels with identical keys.
|
|
|
54481b |
|
|
|
54481b |
To display an image's labels, use the `buildah inspect` command.
|
|
|
54481b |
|
|
|
54481b |
**EXPOSE**
|
|
|
54481b |
-- `EXPOSE <port> [<port>...]`
|
|
|
54481b |
The **EXPOSE** instruction informs the container engine that the container listens on the
|
|
|
54481b |
specified network ports at runtime. The container engine uses this information to
|
|
|
54481b |
interconnect containers using links and to set up port redirection on the host
|
|
|
54481b |
system.
|
|
|
54481b |
|
|
|
54481b |
**ENV**
|
|
|
54481b |
-- `ENV <key> <value>`
|
|
|
54481b |
The **ENV** instruction sets the environment variable <key> to
|
|
|
54481b |
the value `<value>`. This value is passed to all future
|
|
|
54481b |
**RUN**, **ENTRYPOINT**, and **CMD** instructions. This is
|
|
|
54481b |
functionally equivalent to prefixing the command with `<key>=<value>`. The
|
|
|
54481b |
environment variables that are set with **ENV** persist when a container is run
|
|
|
54481b |
from the resulting image. Use `podman inspect` to inspect these values, and
|
|
|
54481b |
change them using `podman run --env <key>=<value>`.
|
|
|
54481b |
|
|
|
54481b |
Note that setting "`ENV DEBIAN_FRONTEND=noninteractive`" may cause
|
|
|
54481b |
unintended consequences, because it will persist when the container is run
|
|
|
54481b |
interactively, as with the following command: `podman run -t -i image bash`
|
|
|
54481b |
|
|
|
54481b |
**ADD**
|
|
|
54481b |
-- **ADD** has two forms:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
ADD <src> <dest>
|
|
|
54481b |
|
|
|
54481b |
# Required for paths with whitespace
|
|
|
54481b |
ADD ["<src>",... "<dest>"]
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
The **ADD** instruction copies new files, directories
|
|
|
54481b |
or remote file URLs to the filesystem of the container at path `<dest>`.
|
|
|
54481b |
Multiple `<src>` resources may be specified but if they are files or directories
|
|
|
54481b |
then they must be relative to the source directory that is being built
|
|
|
54481b |
(the context of the build). The `<dest>` is the absolute path, or path relative
|
|
|
54481b |
to **WORKDIR**, into which the source is copied inside the target container.
|
|
|
54481b |
If the `<src>` argument is a local file in a recognized compression format
|
|
|
54481b |
(tar, gzip, bzip2, etc) then it is unpacked at the specified `<dest>` in the
|
|
|
54481b |
container's filesystem. Note that only local compressed files will be unpacked,
|
|
|
54481b |
i.e., the URL download and archive unpacking features cannot be used together.
|
|
|
54481b |
All new directories are created with mode 0755 and with the uid and gid of **0**.
|
|
|
54481b |
|
|
|
54481b |
**COPY**
|
|
|
54481b |
-- **COPY** has two forms:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
COPY <src> <dest>
|
|
|
54481b |
|
|
|
54481b |
# Required for paths with whitespace
|
|
|
54481b |
COPY ["<src>",... "<dest>"]
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
The **COPY** instruction copies new files from `<src>` and
|
|
|
54481b |
adds them to the filesystem of the container at path <dest>. The `<src>` must be
|
|
|
54481b |
the path to a file or directory relative to the source directory that is
|
|
|
54481b |
being built (the context of the build) or a remote file URL. The `<dest>` is an
|
|
|
54481b |
absolute path, or a path relative to **WORKDIR**, into which the source will
|
|
|
54481b |
be copied inside the target container. If you **COPY** an archive file it will
|
|
|
54481b |
land in the container exactly as it appears in the build context without any
|
|
|
54481b |
attempt to unpack it. All new files and directories are created with mode **0755**
|
|
|
54481b |
and with the uid and gid of **0**.
|
|
|
54481b |
|
|
|
54481b |
**ENTRYPOINT**
|
|
|
54481b |
-- **ENTRYPOINT** has two forms:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
# executable form
|
|
|
54481b |
ENTRYPOINT ["executable", "param1", "param2"]`
|
|
|
54481b |
|
|
|
54481b |
# run command in a shell - /bin/sh -c
|
|
|
54481b |
ENTRYPOINT command param1 param2
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
-- An **ENTRYPOINT** helps you configure a
|
|
|
54481b |
container that can be run as an executable. When you specify an **ENTRYPOINT**,
|
|
|
54481b |
the whole container runs as if it was only that executable. The **ENTRYPOINT**
|
|
|
54481b |
instruction adds an entry command that is not overwritten when arguments are
|
|
|
54481b |
passed to `podman run`. This is different from the behavior of **CMD**. This allows
|
|
|
54481b |
arguments to be passed to the entrypoint, for instance `podman run <image> -d`
|
|
|
54481b |
passes the -d argument to the **ENTRYPOINT**. Specify parameters either in the
|
|
|
54481b |
**ENTRYPOINT** JSON array (as in the preferred exec form above), or by using a **CMD**
|
|
|
54481b |
statement. Parameters in the **ENTRYPOINT** are not overwritten by the `podman run` arguments. Parameters specified via **CMD** are overwritten by `podman run` arguments. Specify a plain string for the **ENTRYPOINT**, and it will execute in
|
|
|
54481b |
`/bin/sh -c`, like a **CMD** instruction:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
FROM ubuntu
|
|
|
54481b |
ENTRYPOINT wc -l -
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
This means that the Containerfile's image always takes stdin as input (that's
|
|
|
54481b |
what "-" means), and prints the number of lines (that's what "-l" means). To
|
|
|
54481b |
make this optional but default, use a **CMD**:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
FROM ubuntu
|
|
|
54481b |
CMD ["-l", "-"]
|
|
|
54481b |
ENTRYPOINT ["/usr/bin/wc"]
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
**VOLUME**
|
|
|
54481b |
-- `VOLUME ["/data"]`
|
|
|
54481b |
The **VOLUME** instruction creates a mount point with the specified name and marks
|
|
|
54481b |
it as holding externally-mounted volumes from the native host or from other
|
|
|
54481b |
containers.
|
|
|
54481b |
|
|
|
54481b |
**USER**
|
|
|
54481b |
-- `USER daemon`
|
|
|
54481b |
Sets the username or UID used for running subsequent commands.
|
|
|
54481b |
|
|
|
54481b |
The **USER** instruction can optionally be used to set the group or GID. The
|
|
|
54481b |
following examples are all valid:
|
|
|
54481b |
USER [user | user:group | uid | uid:gid | user:gid | uid:group ]
|
|
|
54481b |
|
|
|
54481b |
Until the **USER** instruction is set, instructions will be run as root. The USER
|
|
|
54481b |
instruction can be used any number of times in a Containerfile, and will only affect
|
|
|
54481b |
subsequent commands.
|
|
|
54481b |
|
|
|
54481b |
**WORKDIR**
|
|
|
54481b |
-- `WORKDIR /path/to/workdir`
|
|
|
54481b |
The **WORKDIR** instruction sets the working directory for the **RUN**, **CMD**,
|
|
|
54481b |
**ENTRYPOINT**, **COPY** and **ADD** Containerfile commands that follow it. It can
|
|
|
54481b |
be used multiple times in a single Containerfile. Relative paths are defined
|
|
|
54481b |
relative to the path of the previous **WORKDIR** instruction. For example:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
WORKDIR /a
|
|
|
54481b |
WORKDIR b
|
|
|
54481b |
WORKDIR c
|
|
|
54481b |
RUN pwd
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
In the above example, the output of the **pwd** command is **a/b/c**.
|
|
|
54481b |
|
|
|
54481b |
**ARG**
|
|
|
54481b |
-- ARG <name>[=<default value>]
|
|
|
54481b |
|
|
|
54481b |
The `ARG` instruction defines a variable that users can pass at build-time to
|
|
|
54481b |
the builder with the `podman build` and `buildah build` commands using the
|
|
|
54481b |
`--build-arg <varname>=<value>` flag. If a user specifies a build argument that
|
|
|
54481b |
was not defined in the Containerfile, the build outputs a warning.
|
|
|
54481b |
|
|
|
54481b |
Note that a second FROM in a Containerfile sets the values associated with an
|
|
|
54481b |
Arg variable to nil and they must be reset if they are to be used later in
|
|
|
54481b |
the Containerfile
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
[Warning] One or more build-args [foo] were not consumed
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
The Containerfile author can define a single variable by specifying `ARG` once or many
|
|
|
54481b |
variables by specifying `ARG` more than once. For example, a valid Containerfile:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
FROM busybox
|
|
|
54481b |
ARG user1
|
|
|
54481b |
ARG buildno
|
|
|
54481b |
...
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
A Containerfile author may optionally specify a default value for an `ARG` instruction:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
FROM busybox
|
|
|
54481b |
ARG user1=someuser
|
|
|
54481b |
ARG buildno=1
|
|
|
54481b |
...
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
If an `ARG` value has a default and if there is no value passed at build-time, the
|
|
|
54481b |
builder uses the default.
|
|
|
54481b |
|
|
|
54481b |
An `ARG` variable definition comes into effect from the line on which it is
|
|
|
54481b |
defined in the `Containerfile` not from the argument's use on the command-line or
|
|
|
54481b |
elsewhere. For example, consider this Containerfile:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
1 FROM busybox
|
|
|
54481b |
2 USER ${user:-some_user}
|
|
|
54481b |
3 ARG user
|
|
|
54481b |
4 USER $user
|
|
|
54481b |
...
|
|
|
54481b |
```
|
|
|
54481b |
A user builds this file by calling:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
$ podman build --build-arg user=what_user Containerfile
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
The `USER` at line 2 evaluates to `some_user` as the `user` variable is defined on the
|
|
|
54481b |
subsequent line 3. The `USER` at line 4 evaluates to `what_user` as `user` is
|
|
|
54481b |
defined and the `what_user` value was passed on the command line. Prior to its definition by an
|
|
|
54481b |
`ARG` instruction, any use of a variable results in an empty string.
|
|
|
54481b |
|
|
|
54481b |
> **Warning:** It is not recommended to use build-time variables for
|
|
|
54481b |
> passing secrets like github keys, user credentials etc. Build-time variable
|
|
|
54481b |
> values are visible to any user of the image with the `podman history` command.
|
|
|
54481b |
|
|
|
54481b |
You can use an `ARG` or an `ENV` instruction to specify variables that are
|
|
|
54481b |
available to the `RUN` instruction. Environment variables defined using the
|
|
|
54481b |
`ENV` instruction always override an `ARG` instruction of the same name. Consider
|
|
|
54481b |
this Containerfile with an `ENV` and `ARG` instruction.
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
1 FROM ubuntu
|
|
|
54481b |
2 ARG CONT_IMG_VER
|
|
|
54481b |
3 ENV CONT_IMG_VER=v1.0.0
|
|
|
54481b |
4 RUN echo $CONT_IMG_VER
|
|
|
54481b |
```
|
|
|
54481b |
Then, assume this image is built with this command:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
$ podman build --build-arg CONT_IMG_VER=v2.0.1 Containerfile
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
In this case, the `RUN` instruction uses `v1.0.0` instead of the `ARG` setting
|
|
|
54481b |
passed by the user:`v2.0.1` This behavior is similar to a shell
|
|
|
54481b |
script where a locally scoped variable overrides the variables passed as
|
|
|
54481b |
arguments or inherited from environment, from its point of definition.
|
|
|
54481b |
|
|
|
54481b |
Using the example above but a different `ENV` specification you can create more
|
|
|
54481b |
useful interactions between `ARG` and `ENV` instructions:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
1 FROM ubuntu
|
|
|
54481b |
2 ARG CONT_IMG_VER
|
|
|
54481b |
3 ENV CONT_IMG_VER=${CONT_IMG_VER:-v1.0.0}
|
|
|
54481b |
4 RUN echo $CONT_IMG_VER
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
Unlike an `ARG` instruction, `ENV` values are always persisted in the built
|
|
|
54481b |
image. Consider a `podman build` without the --build-arg flag:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
$ podman build Containerfile
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
Using this Containerfile example, `CONT_IMG_VER` is still persisted in the image but
|
|
|
54481b |
its value would be `v1.0.0` as it is the default set in line 3 by the `ENV` instruction.
|
|
|
54481b |
|
|
|
54481b |
The variable expansion technique in this example allows you to pass arguments
|
|
|
54481b |
from the command line and persist them in the final image by leveraging the
|
|
|
54481b |
`ENV` instruction. Variable expansion is only supported for [a limited set of
|
|
|
54481b |
Containerfile instructions.](#environment-replacement)
|
|
|
54481b |
|
|
|
54481b |
Container engines have a set of predefined `ARG` variables that you can use without a
|
|
|
54481b |
corresponding `ARG` instruction in the Containerfile.
|
|
|
54481b |
|
|
|
54481b |
* `HTTP_PROXY`
|
|
|
54481b |
* `http_proxy`
|
|
|
54481b |
* `HTTPS_PROXY`
|
|
|
54481b |
* `https_proxy`
|
|
|
54481b |
* `FTP_PROXY`
|
|
|
54481b |
* `ftp_proxy`
|
|
|
54481b |
* `NO_PROXY`
|
|
|
54481b |
* `no_proxy`
|
|
|
54481b |
* `ALL_PROXY`
|
|
|
54481b |
* `all_proxy`
|
|
|
54481b |
|
|
|
54481b |
To use these, pass them on the command line using `--build-arg` flag, for
|
|
|
54481b |
example:
|
|
|
54481b |
|
|
|
54481b |
```
|
|
|
54481b |
$ podman build --build-arg HTTPS_PROXY=https://my-proxy.example.com .
|
|
|
54481b |
```
|
|
|
54481b |
|
|
|
54481b |
**ONBUILD**
|
|
|
54481b |
-- `ONBUILD [INSTRUCTION]`
|
|
|
54481b |
The **ONBUILD** instruction adds a trigger instruction to an image. The
|
|
|
54481b |
trigger is executed at a later time, when the image is used as the base for
|
|
|
54481b |
another build. Container engines execute the trigger in the context of the downstream
|
|
|
54481b |
build, as if the trigger existed immediately after the **FROM** instruction in
|
|
|
54481b |
the downstream Containerfile.
|
|
|
54481b |
|
|
|
54481b |
You can register any build instruction as a trigger. A trigger is useful if
|
|
|
54481b |
you are defining an image to use as a base for building other images. For
|
|
|
54481b |
example, if you are defining an application build environment or a daemon that
|
|
|
54481b |
is customized with a user-specific configuration.
|
|
|
54481b |
|
|
|
54481b |
Consider an image intended as a reusable python application builder. It must
|
|
|
54481b |
add application source code to a particular directory, and might need a build
|
|
|
54481b |
script called after that. You can't just call **ADD** and **RUN** now, because
|
|
|
54481b |
you don't yet have access to the application source code, and it is different
|
|
|
54481b |
for each application build.
|
|
|
54481b |
|
|
|
54481b |
-- Providing application developers with a boilerplate Containerfile to copy-paste
|
|
|
54481b |
into their application is inefficient, error-prone, and
|
|
|
54481b |
difficult to update because it mixes with application-specific code.
|
|
|
54481b |
The solution is to use **ONBUILD** to register instructions in advance, to
|
|
|
54481b |
run later, during the next build stage.
|
|
|
54481b |
|
|
|
54481b |
## SEE ALSO
|
|
|
54481b |
buildah(1), podman(1), docker(1)
|
|
|
54481b |
|
|
|
54481b |
# HISTORY
|
|
|
54481b |
```
|
|
|
54481b |
May 2014, Compiled by Zac Dover (zdover at redhat dot com) based on docker.com Dockerfile documentation.
|
|
|
54481b |
Feb 2015, updated by Brian Goff (cpuguy83@gmail.com) for readability
|
|
|
54481b |
Sept 2015, updated by Sally O'Malley (somalley@redhat.com)
|
|
|
54481b |
Oct 2016, updated by Addam Hardy (addam.hardy@gmail.com)
|
|
|
54481b |
Aug 2021, converted Dockerfile man page to Containerfile by Dan Walsh (dwalsh@redhat.com)
|
|
|
54481b |
```
|