What is the security problem with this Dockerfile, and how would you write it better?
FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install -y curl
update and install sit in separate cached layers, so a rebuild can reuse a stale package index and install outdated or vulnerable packages, and Ubuntu 18.04 itself no longer gets standard security updates.
Every instruction becomes a layer, and Docker caches layers: on a rebuild, an instruction whose text has not changed is taken from the cache, not executed again. The line RUN apt-get update never changes, so after the first build it is never re-run. When the install line is later changed (say, curl wget), it installs against a package index that may be months old. Packages may then be outdated (missing security fixes) or fail to install because the old versions have been removed from the mirrors.
The fix is to update and install in one instruction, so they are always cached and re-run together:
FROM ubuntu:24.04
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*
The other improvements matter too:
- A supported base image.
ubuntu:18.04reached the end of its standard support in May 2023, so its packages no longer get free security patches. --no-install-recommendsinstalls fewer extra packages, so less attack surface.- Deleting the apt lists in the same layer keeps the image small.
- Pin versions (
curl=8.5.0-…) when builds must be reproducible.
Go deeper:
Docker Docs: Building best practices, apt-get — the official explanation of cache-busting update and install.
Ubuntu release cycle — when each LTS release leaves standard support.