Docker
Dockerfile
COPY command
ADD command
conditional statements

Conditional COPY/ADD in a Dockerfile (copy if exists, build args)

Codemia Knowledge Hub ·

COPY and ADD cannot run conditionally on their own. To vary what gets copied, put a build argument or environment variable in the source path, handle file-existence checks in a script or a separate build stage, or move the conditional logic into a RUN instruction in a multi-stage build.

COPY vs. ADD

  • COPY copies local files or directories to a path in the image.
  • ADD works like COPY but also auto-extracts compressed files and can fetch files from remote URLs.

Use COPY for simple cases and reserve ADD for when you need its extra features.

Choosing the source path with a build argument

The ARG instruction lets you select a path at build time:

dockerfile
ARG ENVIRONMENT
COPY config/${ENVIRONMENT}/settings.json /app/settings.json

Set the environment when you build: docker build --build-arg ENVIRONMENT=production ..

Choosing the source path with an environment variable

COPY also expands ENV variables, but an ENV value is fixed in the Dockerfile and docker build has no flag to change it. To keep a default that a build can override, feed the ENV from an ARG:

dockerfile
ARG ENVIRONMENT=development
ENV ENVIRONMENT=${ENVIRONMENT}
COPY config/${ENVIRONMENT}/settings.json /app/settings.json

docker build --build-arg ENVIRONMENT=production . then copies the production file, and the value stays available as an environment variable when the container runs.

Copying only if a file exists

COPY and ADD cannot check whether a file exists, but a script or separate build stage can:

dockerfile
1FROM alpine as builder
2COPY check_file.sh /app/check_file.sh
3RUN /bin/sh /app/check_file.sh
4
5FROM alpine
6COPY --from=builder /app/conditional_file /app/

check_file.sh decides whether to move or create files based on your conditions.

Branching in a multi-stage build

Multi-stage builds can hold conditional logic in a separate stage:

dockerfile
1FROM node:14 as builder
2WORKDIR /app
3ARG ENV
4COPY package.json ./
5
6RUN if [ "$ENV" = "production" ]; then \
7      npm install --only=prod; \
8    else \
9      npm install; \
10    fi
11
12COPY . .
13
14FROM node:14
15WORKDIR /app
16COPY --from=builder /app /app
17CMD ["node", "index.js"]

Here dependency installation depends on the ENV ARG.

Keeping conditional builds lean

  • Minimize layers: use conditions to cut unnecessary COPY or ADD commands.
  • Use .dockerignore: keep unnecessary files out of the build context.
  • Separate concerns: put complex condition checks in scripts instead of cluttering the Dockerfile.
  • Multi-stage builds: split conditions into separate build stages when possible.
AspectCOPYADDConditional Techniques
Basic FunctionalityCopies files/directories from host to containerSimilar to COPY but supports URL sources and compressed filesUse ARGs and ENVs for dynamic configurations
When to UseSimple copying tasksFetching remote data or unpacking archivesCheck file existence with scripts or multi-stage builds
Best PracticeDefault choice for file transfersUse for remote or compressed data if necessaryLimit conditions to minimize unnecessary layers

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design