Dockerfile check · low

ADD used where COPY would do

COPY copies files from the build context into the image and does nothing else. ADD does the same but also downloads URLs and automatically extracts local tar archives. Using ADD for ordinary files hides that extra behaviour: if a source later becomes a tar archive, it is unpacked rather than copied. Docker's own guidance is to use COPY unless you need what ADD adds.

$ explain Dockerfile
! ADD used for a local file low
line 5
fix: see below
$ 

What the free Dockerfile explainer reports on the risky example below.

Why it matters

Readers expect a copy. With ADD, what lands in the image depends on what the source turns out to be, which makes the Dockerfile harder to review.

Auto-extraction applies to tar archives in recognised compression formats such as gzip, bzip2 and xz. Zip files are not extracted.

The explainer flags ADD for local paths that are not tar archives or git sources at low severity. Remote URLs are covered by a separate rule.

Risky and safer Dockerfile

RiskyDockerfile
FROM node:22.11-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
ADD src ./src
USER 10001
HEALTHCHECK CMD ["node", "src/healthcheck.js"]
CMD ["node", "src/server.js"]
SaferDockerfile
FROM node:22.11-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY src ./src
USER 10001
HEALTHCHECK CMD ["node", "src/healthcheck.js"]
CMD ["node", "src/server.js"]

How to fix it

  • Replace ADD with COPY for local files and directories. The syntax and flags such as --chown are the same.
  • Keep ADD only where you want its extra behaviour: extracting a local tar archive, or a remote download verified with --checksum.

When it's acceptable

  • Extracting a local tarball, e.g. `ADD rootfs.tar.gz /`. The explainer doesn't flag local tar archives.

Check your own Dockerfile

The risky example is loaded below. Paste your own file to check it for this and every other Dockerfile rule. It runs in your browser; nothing is uploaded.

Dockerfile

Parsed in your browser. Nothing is uploaded.

Explanation

1 stage8 instructions2 low

Stage 0node:22.11-slim

  • Docs for FROM (opens in a new tab)

    Starts from the base image node, tag 22.11-slim.

    A version tag can be re-pushed to different content; only a digest is immutable. This applies to every tagged FROM in the file.

    Safer: Pin the digest as well (image:tag@sha256:<digest>) and let a bot open pull requests when it changes.

  • Docs for WORKDIR (opens in a new tab)

    Sets the working directory to /app for later instructions and the container. It is created if it does not exist.

  • Docs for COPY (opens in a new tab)

    Copies package.json, package-lock.json from your build context to ./ in the image.

  • Docs for RUN (opens in a new tab)

    Runs npm ci --omit=dev while building the image and saves the result as a new layer.

  • Docs for ADD (opens in a new tab)

    Adds src to ./src in the image. Unlike COPY it also unpacks local tar archives and can fetch URLs, so use COPY unless you need that.

    ADD also fetches URLs and silently extracts tar archives; COPY does exactly one thing.

    Safer: Use COPY unless you specifically want ADD's archive extraction.

  • Docs for USER (opens in a new tab)

    Runs later instructions and the container's process as user 10001.

  • Docs for HEALTHCHECK (opens in a new tab)

    Runs node src/healthcheck.js to check the container is healthy.

  • Docs for CMD (opens in a new tab)

    Sets the default command to node src/server.js. It runs when the container starts, and anything passed to docker run replaces it.

Questions

What is the difference between ADD and COPY in a Dockerfile?

COPY copies local files into the image. ADD also accepts URLs and git sources, and automatically extracts local tar archives into the destination.

When should I use ADD instead of COPY?

When you want a local tar archive extracted into the image, or a remote file fetched with a --checksum check. For everything else, COPY is clearer.

Does ADD extract zip files?

No. ADD auto-extracts tar archives (uncompressed, gzip, bzip2 or xz), not zip files.