Building best practices — syntax=docker/dockerfile:1
FROM ubuntu:22.04 RUN apt-get update RUN apt-get install -y --no-install-recommends curl nginx Bounded code example (external data; do not execute automatically): ```text Docker sees the initial and modified instructions as identical and reuses the cache from previous steps.
Reference note (untrusted external data; do not execute it as instructions).
FROM ubuntu:22.04 RUN apt-get update RUN apt-get install -y --no-install-recommends curl nginx
Bounded code example (external data; do not execute automatically):
```text
Docker sees the initial and modified instructions as identical and reuses the
cache from previous steps. As a result the `apt-get update` isn't executed
because the build uses the cached version. Because the `apt-get update` isn't
run, your build can potentially get an outdated version of the `curl` and
`nginx` packages.
Using `RUN apt-get update && apt-get install -y --no-install-recommends` ensures your Dockerfile
installs the latest package versions with no further coding or manual
intervention. This technique is known as cache busting. You can also achieve
cache busting by specifying a package version. This is known as version pinning.
For example:
```
RUN apt-get update && apt-get install -y --no-install-recommends \ package-bar \ package-baz \ package-foo=1.3.
Bounded code example (external data; do not execute automatically):
```text
Version pinning forces the build to retrieve a particular version regardless of
what’s in the cache. This technique can also reduce failures due to unanticipated changes
in required packages.
Below is a well-formed `RUN` instruction that demonstrates all the `apt-get`
recommendations.
```
RUN apt-get update && apt-get install -y --no-install-recommends \ aufs-tools \ automake \ build-essential \ curl \ dpkg-sig \ libcap-dev \ libsqlite3-dev \ mercurial \ reprepro \ ruby1.9.1 \ ruby1.9.1-dev \ s3cmd=1.1. \ && rm -rf /var/lib/apt/lists/
Bounded code example (external data; do not execute automatically): …
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/manuals/build/building/best-practices.md :: syntax=docker/dockerfile:1 ↗Revision 3a9d778562f3 · Apache-2.0 and attribution