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
COPYbut 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:
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:
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:
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:
Here dependency installation depends on the ENV ARG.
Keeping conditional builds lean
- Minimize layers: use conditions to cut unnecessary
COPYorADDcommands. - 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.
| Aspect | COPY | ADD | Conditional Techniques |
| Basic Functionality | Copies files/directories from host to container | Similar to COPY but supports URL sources and compressed files | Use ARGs and ENVs for dynamic configurations |
| When to Use | Simple copying tasks | Fetching remote data or unpacking archives | Check file existence with scripts or multi-stage builds |
| Best Practice | Default choice for file transfers | Use for remote or compressed data if necessary | Limit conditions to minimize unnecessary layers |
Related reading
- Configure sendmail inside a docker container
- configured logging driver does not support reading Docker
- Configuring Kafka to accept clients both from inside and outside docker
- configuring kubernetes restart policy
- Confluent Docker log4j logger level configurations
- Confused about Docker -t option to Allocate a pseudo-TTY
- Confusion with node_modules being ignored by .dockerignore
- Connect from one Docker container to another
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.