2024-04-10 06:10:10 -04:00
# build (legacy builder)
2015-06-21 16:41:38 -04:00
2023-01-06 13:04:05 -05:00
<!-- - MARKER_GEN_START -->
2016-07-07 14:43:18 -04:00
Build an image from a Dockerfile
2015-06-21 16:41:38 -04:00
2023-01-06 13:04:05 -05:00
### Aliases
2024-04-10 06:10:10 -04:00
`docker image build` , `docker build` , `docker builder build`
2023-01-06 13:04:05 -05:00
### Options
2024-04-10 06:10:10 -04:00
| Name | Type | Default | Description |
|:-----------------------------------------------------------------------------------------------------------------------------------------------------|:--------------|:----------|:------------------------------------------------------------------|
| [`--add-host` ](https://docs.docker.com/reference/cli/docker/buildx/build/#add-host ) | `list` | | Add a custom host-to-IP mapping (`host:ip`) |
| [`--build-arg` ](https://docs.docker.com/reference/cli/docker/buildx/build/#build-arg ) | `list` | | Set build-time variables |
| `--cache-from` | `stringSlice` | | Images to consider as cache sources |
| [`--cgroup-parent` ](https://docs.docker.com/reference/cli/docker/buildx/build/#cgroup-parent ) | `string` | | Set the parent cgroup for the `RUN` instructions during build |
2024-07-03 02:29:57 -04:00
| `--compress` | `bool` | | Compress the build context using gzip |
2024-04-10 06:10:10 -04:00
| `--cpu-period` | `int64` | `0` | Limit the CPU CFS (Completely Fair Scheduler) period |
| `--cpu-quota` | `int64` | `0` | Limit the CPU CFS (Completely Fair Scheduler) quota |
| `-c` , `--cpu-shares` | `int64` | `0` | CPU shares (relative weight) |
| `--cpuset-cpus` | `string` | | CPUs in which to allow execution (0-3, 0,1) |
| `--cpuset-mems` | `string` | | MEMs in which to allow execution (0-3, 0,1) |
| `--disable-content-trust` | `bool` | `true` | Skip image verification |
| [`-f` ](https://docs.docker.com/reference/cli/docker/buildx/build/#file ), [`--file` ](https://docs.docker.com/reference/cli/docker/buildx/build/#file ) | `string` | | Name of the Dockerfile (Default is `PATH/Dockerfile` ) |
2024-07-03 02:29:57 -04:00
| `--force-rm` | `bool` | | Always remove intermediate containers |
2024-04-10 06:10:10 -04:00
| `--iidfile` | `string` | | Write the image ID to the file |
| [`--isolation` ](#isolation ) | `string` | | Container isolation technology |
| `--label` | `list` | | Set metadata for an image |
| `-m` , `--memory` | `bytes` | `0` | Memory limit |
| `--memory-swap` | `bytes` | `0` | Swap limit equal to memory plus swap: -1 to enable unlimited swap |
| [`--network` ](https://docs.docker.com/reference/cli/docker/buildx/build/#network ) | `string` | `default` | Set the networking mode for the RUN instructions during build |
2024-07-03 02:29:57 -04:00
| `--no-cache` | `bool` | | Do not use cache when building the image |
2024-04-10 06:10:10 -04:00
| `--platform` | `string` | | Set platform if server is multi-platform capable |
2024-07-03 02:29:57 -04:00
| `--pull` | `bool` | | Always attempt to pull a newer version of the image |
| `-q` , `--quiet` | `bool` | | Suppress the build output and print image ID on success |
2024-04-10 06:10:10 -04:00
| `--rm` | `bool` | `true` | Remove intermediate containers after a successful build |
| [`--security-opt` ](#security-opt ) | `stringSlice` | | Security options |
| `--shm-size` | `bytes` | `0` | Size of `/dev/shm` |
2024-07-03 02:29:57 -04:00
| [`--squash` ](#squash ) | `bool` | | Squash newly built layers into a single new layer |
2024-04-10 06:10:10 -04:00
| [`-t` ](https://docs.docker.com/reference/cli/docker/buildx/build/#tag ), [`--tag` ](https://docs.docker.com/reference/cli/docker/buildx/build/#tag ) | `list` | | Name and optionally a tag in the `name:tag` format |
| [`--target` ](https://docs.docker.com/reference/cli/docker/buildx/build/#target ) | `string` | | Set the target build stage to build. |
| `--ulimit` | `ulimit` | | Ulimit options |
2023-01-06 13:04:05 -05:00
<!-- - MARKER_GEN_END -->
2015-06-21 16:41:38 -04:00
2017-02-07 18:42:48 -05:00
## Description
2024-09-23 07:38:33 -04:00
> [!IMPORTANT]
2024-04-10 06:10:10 -04:00
> This page refers to the **legacy implementation** of `docker build`,
> using the legacy (pre-BuildKit) build backend.
> This configuration is only relevant if you're building Windows containers.
>
> For information about the default `docker build`, using Buildx,
> see [`docker buildx build`](https://docs.docker.com/reference/cli/docker/build/).
2015-06-21 16:41:38 -04:00
2024-04-10 06:10:10 -04:00
When building with legacy builder, images are created from a Dockerfile by
running a sequence of [commits ](./container_commit.md ). This process is
inefficient and slow compared to using BuildKit, which is why this build
strategy is deprecated for all use cases except for building Windows
containers. It's still useful for building Windows containers because BuildKit
doesn't yet have full feature parity for Windows.
2015-06-21 16:41:38 -04:00
2024-04-10 06:10:10 -04:00
Builds invoked with `docker build` use Buildx (and BuildKit) by default, unless:
2015-06-21 16:41:38 -04:00
2024-04-10 06:10:10 -04:00
- You're running Docker Engine in Windows container mode
- You explicitly opt out of using BuildKit by setting the environment variable `DOCKER_BUILDKIT=0` .
2015-06-21 16:41:38 -04:00
2024-04-10 06:10:10 -04:00
The descriptions on this page only covers information that's exclusive to the
legacy builder, and cases where behavior in the legacy builder deviates from
behavior in BuildKit. For information about features and flags that are common
between the legacy builder and BuildKit, such as `--tag` and `--target` , refer
to the documentation for [`docker buildx build` ](https://docs.docker.com/reference/cli/docker/buildx/build/ ).
2015-06-21 16:41:38 -04:00
2024-04-10 06:10:10 -04:00
### Build context with the legacy builder
2015-06-21 16:41:38 -04:00
2024-04-10 06:10:10 -04:00
The build context is the positional argument you pass when invoking the build
command. In the following example, the context is `.` , meaning current the
working directory.
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2016-07-20 10:18:44 -04:00
$ docker build .
```
2015-09-27 21:45:10 -04:00
2024-04-10 06:10:10 -04:00
When using the legacy builder, the build context is sent over to the daemon in
its entirety. With BuildKit, only the files you use in your builds are
transmitted. The legacy builder doesn't calculate which files it needs
beforehand. This means that for builds with a large context, context transfer
can take a long time, even if you're only using a subset of the files included
in the context.
2016-08-15 07:32:03 -04:00
2024-04-10 06:10:10 -04:00
When using the legacy builder, it's therefore extra important that you
carefully consider what files you include in the context you specify. Use a
2024-08-13 05:48:29 -04:00
[`.dockerignore` ](https://docs.docker.com/build/concepts/context/#dockerignore-files )
2024-04-10 06:10:10 -04:00
file to exclude files and directories that you don't require in your build from
being sent as part of the build context.
2015-09-27 21:45:10 -04:00
2024-04-10 06:10:10 -04:00
#### Accessing paths outside the build context
2015-09-27 21:45:10 -04:00
2024-04-10 06:10:10 -04:00
The legacy builder will error out if you try to access files outside of the
build context using relative paths in your Dockerfile.
2015-09-27 21:45:10 -04:00
2024-04-10 06:10:10 -04:00
```dockerfile
FROM alpine
COPY ../../some-dir .
2016-07-20 10:18:44 -04:00
```
2015-09-27 21:45:10 -04:00
2021-08-21 08:54:14 -04:00
```console
2016-07-20 10:18:44 -04:00
$ docker build .
2024-04-10 06:10:10 -04:00
...
Step 2/2 : COPY ../../some-dir .
COPY failed: forbidden path outside the build context: ../../some-dir ()
2016-07-20 10:18:44 -04:00
```
2015-06-21 16:41:38 -04:00
2024-04-10 06:10:10 -04:00
BuildKit on the other hand strips leading relative paths that traverse outside
of the build context. Re-using the previous example, the path `COPY
../../some-dir .` evaluates to `COPY some-dir .` with BuildKit.
2014-11-14 13:59:14 -05:00
2024-04-10 06:10:10 -04:00
## Examples
2016-06-07 15:15:50 -04:00
2023-01-06 13:28:29 -05:00
### <a name="isolation"></a> Specify isolation technology for container (--isolation)
2015-11-09 04:11:10 -05:00
This option is useful in situations where you are running Docker containers on
Windows. The `--isolation=<value>` option sets a container's isolation
technology. On Linux, the only supported is the `default` option which uses
Linux namespaces. On Microsoft Windows, you can specify these values:
2022-03-30 06:37:35 -04:00
| Value | Description |
|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `default` | Use the value specified by the Docker daemon's `--exec-opt` . If the `daemon` does not specify an isolation technology, Microsoft Windows uses `process` as its default value. |
| `process` | Namespace isolation only. |
| `hyperv` | Hyper-V hypervisor partition-based isolation. |
2015-11-09 04:11:10 -05:00
Specifying the `--isolation` flag without a value is the same as setting `--isolation="default"` .
2016-04-21 12:08:37 -04:00
2024-04-10 06:10:10 -04:00
### <a name="security-opt"></a> Optional security options (--security-opt)
2023-04-06 05:49:46 -04:00
2024-04-10 06:10:10 -04:00
This flag is only supported on a daemon running on Windows, and only supports
the `credentialspec` option. The `credentialspec` must be in the format
`file://spec.txt` or `registry://keyname` .
2023-04-06 05:49:46 -04:00
2023-01-06 13:28:29 -05:00
### <a name="squash"></a> Squash an image's layers (--squash) (experimental)
2016-04-21 12:08:37 -04:00
2017-02-13 09:01:03 -05:00
#### Overview
2024-08-16 05:02:10 -04:00
> [!NOTE]
2023-05-02 07:39:06 -04:00
> The `--squash` option is an experimental feature, and should not be considered
> stable.
2016-04-21 12:08:37 -04:00
2023-05-02 07:39:06 -04:00
Once the image is built, this flag squashes the new layers into a new image with
a single new layer. Squashing doesn't destroy any existing image, rather it
creates a new image with the content of the squashed layers. This effectively
makes it look like all `Dockerfile` commands were created with a single layer.
The `--squash` flag preserves the build cache.
2016-04-21 12:08:37 -04:00
2017-10-24 08:23:04 -04:00
Squashing layers can be beneficial if your Dockerfile produces multiple layers
2023-05-02 07:39:06 -04:00
modifying the same files. For example, files created in one step and
2017-10-24 08:23:04 -04:00
removed in another step. For other use-cases, squashing images may actually have
2023-05-02 07:39:06 -04:00
a negative impact on performance. When pulling an image consisting of multiple
layers, the daemon can pull layers in parallel and allows sharing layers between
2017-10-24 08:23:04 -04:00
images (saving space).
2019-02-07 07:23:13 -05:00
For most use cases, multi-stage builds are a better alternative, as they give more
2017-10-24 08:23:04 -04:00
fine-grained control over your build, and can take advantage of future
2023-12-13 09:16:56 -05:00
optimizations in the builder. Refer to the [Multi-stage builds ](https://docs.docker.com/build/building/multi-stage/ )
section for more information.
2017-10-24 08:23:04 -04:00
#### Known limitations
The `--squash` option has a number of known limitations:
2023-12-13 09:16:56 -05:00
- When squashing layers, the resulting image can't take advantage of layer
2017-10-24 08:23:04 -04:00
sharing with other images, and may use significantly more space. Sharing the
base image is still supported.
- When using this option you may see significantly more space used due to
storing two copies of the image, one for the build cache with all the cache
2021-07-14 16:15:46 -04:00
layers intact, and one for the squashed version.
2017-10-24 08:23:04 -04:00
- While squashing layers may produce smaller images, it may have a negative
impact on performance, as a single layer takes longer to extract, and
2023-05-02 07:39:06 -04:00
you can't parallelize downloading a single layer.
2023-12-13 09:16:56 -05:00
- When attempting to squash an image that doesn't make changes to the
2017-10-24 08:23:04 -04:00
filesystem (for example, the Dockerfile only contains `ENV` instructions),
2019-02-07 07:23:13 -05:00
the squash step will fail (see [issue #33823 ](https://github.com/moby/moby/issues/33823 )).
2017-01-13 10:01:58 -05:00
2017-02-13 09:01:03 -05:00
#### Prerequisites
2023-04-10 08:14:29 -04:00
The example on this page is using experimental mode in Docker 23.03.
2020-05-11 11:32:52 -04:00
2023-05-02 07:39:06 -04:00
You can enable experimental mode by using the `--experimental` flag when starting
2020-05-11 11:32:52 -04:00
the Docker daemon or setting `experimental: true` in the `daemon.json` configuration
file.
By default, experimental mode is disabled. To see the current configuration of
2023-12-13 09:16:56 -05:00
the Docker daemon, use the `docker version` command and check the `Experimental`
2020-05-11 11:32:52 -04:00
line in the `Engine` section:
```console
Client: Docker Engine - Community
2023-04-10 08:14:29 -04:00
Version: 23.0.3
API version: 1.42
Go version: go1.19.7
Git commit: 3e7cbfd
Built: Tue Apr 4 22:05:41 2023
2020-05-11 11:32:52 -04:00
OS/Arch: darwin/amd64
2023-04-10 08:14:29 -04:00
Context: default
2020-05-11 11:32:52 -04:00
Server: Docker Engine - Community
Engine:
2023-04-10 08:14:29 -04:00
Version: 23.0.3
API version: 1.42 (minimum version 1.12)
Go version: go1.19.7
Git commit: 59118bf
Built: Tue Apr 4 22:05:41 2023
2020-05-11 11:32:52 -04:00
OS/Arch: linux/amd64
Experimental: true
2017-02-13 09:01:03 -05:00
[...]
```
2023-05-02 07:39:06 -04:00
#### Build an image with the `--squash` flag
2017-02-13 09:01:03 -05:00
2023-05-02 07:39:06 -04:00
The following is an example of a build with the `--squash` flag. Below is the
`Dockerfile` :
2017-02-13 09:01:03 -05:00
2020-03-17 10:01:52 -04:00
```dockerfile
2017-02-13 09:01:03 -05:00
FROM busybox
RUN echo hello > /hello
RUN echo world >> /hello
RUN touch remove_me /remove_me
2020-09-23 06:42:17 -04:00
ENV HELLO=world
2017-02-13 09:01:03 -05:00
RUN rm /remove_me
```
2017-10-24 08:23:04 -04:00
2023-05-02 07:39:06 -04:00
Next, build an image named `test` using the `--squash` flag.
2017-02-13 09:01:03 -05:00
2021-08-21 08:54:14 -04:00
```console
2017-02-13 09:01:03 -05:00
$ docker build --squash -t test .
```
2023-05-02 07:39:06 -04:00
After the build completes, the history looks like the below. The history could show that a layer's
name is `<missing>` , and there is a new layer with COMMENT `merge` .
2017-02-13 09:01:03 -05:00
2021-08-21 08:54:14 -04:00
```console
2018-04-12 04:58:13 -04:00
$ docker history test
2017-02-13 09:01:03 -05:00
IMAGE CREATED CREATED BY SIZE COMMENT
4e10cb5b4cac 3 seconds ago 12 B merge sha256:88a7b0112a41826885df0e7072698006ee8f621c6ab99fca7fe9151d7b599702 to sha256:47bcc53f74dc94b1920f0b34f6036096526296767650f223433fe65c35f149eb
< missing > 5 minutes ago /bin/sh -c rm /remove_me 0 B
< missing > 5 minutes ago /bin/sh -c #(nop) ENV HELLO=world 0 B
< missing > 5 minutes ago /bin/sh -c touch remove_me /remove_me 0 B
< missing > 5 minutes ago /bin/sh -c echo world >> /hello 0 B
< missing > 6 minutes ago /bin/sh -c echo hello > /hello 0 B
< missing > 7 weeks ago /bin/sh -c #(nop) CMD ["sh"] 0 B
< missing > 7 weeks ago /bin/sh -c #(nop) ADD file:47ca6e777c36a4cfff 1.113 MB
```
2017-10-24 08:23:04 -04:00
2020-05-11 11:32:52 -04:00
Test the image, check for `/remove_me` being gone, make sure `hello\nworld` is
in `/hello` , make sure the `HELLO` environment variable's value is `world` .