Skip to content

Commit 02d82e2

Browse files
github-actions[bot]dvdksn
authored andcommitted
docs: address issue #23194
This change was automatically generated by the documentation agent team in response to issue #23194. 🤖 Generated with cagent
1 parent 8c3b5db commit 02d82e2

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

content/manuals/build/building/best-practices.md

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,51 @@ dependencies can considerably lower the attack surface.
7777

7878
## Rebuild your images often
7979

80-
Docker images are immutable. Building an image is taking a snapshot of that
81-
image at that moment. That includes any base images, libraries, or other
82-
software you use in your build. To keep your images up-to-date and secure, make
83-
sure to rebuild your image often, with updated dependencies.
80+
Docker images are immutable. Building an image is taking a snapshot of
81+
that image at that moment. That includes any base images, libraries, or
82+
other software you use in your build. To keep your images up-to-date and
83+
secure, rebuild your images regularly with updated dependencies.
8484

85-
To ensure that you're getting the latest versions of dependencies in your build,
86-
you can use the `--no-cache` option to avoid cache hits.
85+
### Use --pull to get fresh base images
8786

88-
```console
89-
$ docker build --no-cache -t my-image:my-tag .
90-
```
91-
92-
The following Dockerfile uses the `24.04` tag of the `ubuntu` image. Over time,
93-
that tag may resolve to a different underlying version of the `ubuntu` image,
94-
as the publisher rebuilds the image with new security patches and updated
95-
libraries. Using the `--no-cache`, you can avoid cache hits and ensure a fresh
96-
download of base images and dependencies.
87+
The following Dockerfile uses the `24.04` tag of the `ubuntu` image.
88+
Over time, that tag may resolve to a different underlying version of the
89+
`ubuntu` image, as the publisher rebuilds the image with new security
90+
patches and updated libraries.
9791

9892
```dockerfile
9993
# syntax=docker/dockerfile:1
10094
FROM ubuntu:24.04
10195
RUN apt-get -y update && apt-get install -y --no-install-recommends python3
10296
```
10397

98+
To get the latest version of the base image, use the `--pull` flag:
99+
100+
```console
101+
$ docker build --pull -t my-image:my-tag .
102+
```
103+
104+
The `--pull` flag forces Docker to check for and download a newer
105+
version of the base image, even if you have a version cached locally.
106+
107+
### Use --no-cache for clean builds
108+
109+
The `--no-cache` flag disables the build cache, forcing Docker to
110+
rebuild all layers from scratch:
111+
112+
```console
113+
$ docker build --no-cache -t my-image:my-tag .
114+
```
115+
116+
This gets the latest available versions of dependencies from package
117+
managers like `apt-get` or `npm`. However, `--no-cache` doesn't pull a
118+
fresh base image - it only prevents reusing cached layers. For a
119+
completely fresh build with the latest base image, combine both flags:
120+
121+
```console
122+
$ docker build --pull --no-cache -t my-image:my-tag .
123+
```
124+
104125
Also consider [pinning base image versions](#pin-base-image-versions).
105126

106127
## Exclude with .dockerignore
@@ -639,10 +660,10 @@ RUN ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet
639660
```
640661

641662
For more information about `ADD` or `COPY`, see the following:
663+
642664
- [Dockerfile reference for the ADD instruction](/reference/dockerfile.md#add)
643665
- [Dockerfile reference for the COPY instruction](/reference/dockerfile.md#copy)
644666

645-
646667
### ENTRYPOINT
647668

648669
The best use for `ENTRYPOINT` is to set the image's main command, allowing that
@@ -695,7 +716,6 @@ fi
695716
exec "$@"
696717
```
697718

698-
699719
This script uses [the `exec` Bash command](https://wiki.bash-hackers.org/commands/builtin/exec) so that the final running application becomes the container's PID 1. This allows the application to receive any Unix signals sent to the container. For more information, see the [`ENTRYPOINT` reference](/reference/dockerfile.md#entrypoint).
700720

701721
In the following example, a helper script is copied into the container and run via `ENTRYPOINT` on

0 commit comments

Comments
 (0)