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