Dockerfile COPY: copy a directory and keep its subdirectory structure
Codemia Knowledge Hub ·
COPY in a Dockerfile copies a directory recursively, so the subdirectory structure under the source is preserved. The source directory itself is not copied, only its contents: to keep the top-level folder too, name it in the destination (for example COPY source/dir1 /app/dir1/).
How COPY paths work
- Source path: the path on the host to copy, relative to the build context.
- Destination path: where the copied files go inside the image.
The build context is the directory you pass to docker build. Files outside it are not available to COPY, so to copy from elsewhere you build with a different context directory.
Copying a directory tree
Given this layout on the host:
Copy dir1 and dir2 with their contents:
The image then contains:
Copying a single subdirectory
You can also copy specific directories:
Because only the directory's contents are copied, this places file1.txt directly at /app/file1.txt. To get the layout below, with dir1 kept as a folder, write the destination as /app/dir1/:
Trailing slashes and permissions
- Trailing slashes: on the source, a trailing slash makes no difference;
COPY source/ /app/andCOPY source /app/behave the same. On the destination it matters: a trailing slash means "copy into this directory". - File permissions: permissions in the image mirror those on the host. Adjust them if your application needs different ones to run correctly in the container.
Excluding files with .dockerignore
A .dockerignore file lists files and directories to leave out of the build, which keeps unnecessary data out of the image and speeds up builds:
Using environment variables in paths
Dockerfile instructions can use environment variables for dynamic paths. Make sure the variable is defined in the Dockerfile before the COPY that uses it:
| Concept | Description |
| Build Context | Directory context from which Docker commands operate |
| Source Path | Host filesystem path for files/directories used in the build |
| Destination Path | Target location inside the Docker image |
| Trailing Slash Behavior | Ignored on the source; on the destination, copies into that directory |
| File Permissions | Host permissions affect those in the Docker image; adjust as needed |
| Environment Variables | Dockerfile commands can use environment variables for dynamic paths |
| Selective Copying | Copy specific subdirectories or files using precise path specifications |
| Ignore Files | Use .dockerignore to exclude files from being copied into docker images |
Keep the build context minimal, exclude what you don't need, and build and test your Dockerfiles locally to confirm the right files end up in the image.
Related reading
- Dockerfile if else condition with external arguments
- Dockerfile Is there any way to read variables from .env file
- Dockerfile strategies for Git
- Docker/Kubernetes Gunicorn/Celery - Multiple Workers vs Replicas?
- docker.sock permission denied
- Does Alpine have known DNS issue within Kubernetes?
- Does Kubernetes cache docker-registry secrets?
- Does kubernetes have its own Load Balancer?
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.