Skip to content

Commit ff09b52

Browse files
authored
Update best-practices.md (#21045)
## Description Adding `--no-install-recommends` is a common best practice and can massively reduce image sizes. Since people copy from this page it is good to add.
1 parent 548f411 commit ff09b52

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ download of base images and dependencies.
9898
```dockerfile
9999
# syntax=docker/dockerfile:1
100100
FROM ubuntu:24.04
101-
RUN apt-get -y update && apt-get install -y python
101+
RUN apt-get -y update && apt-get install -y --no-install-recommends python3
102102
```
103103

104104
Also consider [pinning base image versions](#pin-base-image-versions).
@@ -165,7 +165,7 @@ review. Adding a space before a backslash (`\`) helps as well.
165165
Here’s an example from the [buildpack-deps image](https://github.com/docker-library/buildpack-deps):
166166

167167
```dockerfile
168-
RUN apt-get update && apt-get install -y \
168+
RUN apt-get update && apt-get install -y --no-install-recommends \
169169
bzr \
170170
cvs \
171171
git \
@@ -322,7 +322,7 @@ For example, you can chain commands with the `&&` operator, and use
322322
escape characters to break long commands into multiple lines.
323323

324324
```dockerfile
325-
RUN apt-get update && apt-get install -y \
325+
RUN apt-get update && apt-get install -y --no-install-recommends \
326326
package-bar \
327327
package-baz \
328328
package-foo
@@ -337,7 +337,7 @@ with a pipeline operator:
337337
```dockerfile
338338
RUN <<EOF
339339
apt-get update
340-
apt-get install -y \
340+
apt-get install -y --no-install-recommends \
341341
package-bar \
342342
package-baz \
343343
package-foo
@@ -356,7 +356,7 @@ Always combine `RUN apt-get update` with `apt-get install` in the same `RUN`
356356
statement. For example:
357357

358358
```dockerfile
359-
RUN apt-get update && apt-get install -y \
359+
RUN apt-get update && apt-get install -y --no-install-recommends \
360360
package-bar \
361361
package-baz \
362362
package-foo
@@ -370,7 +370,7 @@ subsequent `apt-get install` instructions to fail. For example, this issue will
370370

371371
FROM ubuntu:22.04
372372
RUN apt-get update
373-
RUN apt-get install -y curl
373+
RUN apt-get install -y --no-install-recommends curl
374374
```
375375

376376
After building the image, all layers are in the Docker cache. Suppose you later
@@ -381,7 +381,7 @@ modify `apt-get install` by adding an extra package as shown in the following Do
381381

382382
FROM ubuntu:22.04
383383
RUN apt-get update
384-
RUN apt-get install -y curl nginx
384+
RUN apt-get install -y --no-install-recommends curl nginx
385385
```
386386

387387
Docker sees the initial and modified instructions as identical and reuses the
@@ -390,14 +390,14 @@ because the build uses the cached version. Because the `apt-get update` isn't
390390
run, your build can potentially get an outdated version of the `curl` and
391391
`nginx` packages.
392392

393-
Using `RUN apt-get update && apt-get install -y` ensures your Dockerfile
393+
Using `RUN apt-get update && apt-get install -y --no-install-recommends` ensures your Dockerfile
394394
installs the latest package versions with no further coding or manual
395395
intervention. This technique is known as cache busting. You can also achieve
396396
cache busting by specifying a package version. This is known as version pinning.
397397
For example:
398398

399399
```dockerfile
400-
RUN apt-get update && apt-get install -y \
400+
RUN apt-get update && apt-get install -y --no-install-recommends \
401401
package-bar \
402402
package-baz \
403403
package-foo=1.3.*
@@ -411,7 +411,7 @@ Below is a well-formed `RUN` instruction that demonstrates all the `apt-get`
411411
recommendations.
412412

413413
```dockerfile
414-
RUN apt-get update && apt-get install -y \
414+
RUN apt-get update && apt-get install -y --no-install-recommends \
415415
aufs-tools \
416416
automake \
417417
build-essential \

0 commit comments

Comments
 (0)