Docker
Dockerfile
Containerization
File Structure
Development

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

dockerfile
COPY <source-path> <destination-path>
  • 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:

 
1    /project
2    ├── Dockerfile
3    └── source
4        ├── dir1
5        │   └── file1.txt
6        └── dir2
7            └── file2.txt

Copy dir1 and dir2 with their contents:

dockerfile
    FROM ubuntu:latest
    WORKDIR /app
    COPY source/ /app/

The image then contains:

 
1    /app
2    ├── dir1
3    │   └── file1.txt
4    └── dir2
5        └── file2.txt

Copying a single subdirectory

You can also copy specific directories:

dockerfile
    COPY source/dir1 /app/

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/:

 
    /app
    └── dir1
        └── file1.txt

Trailing slashes and permissions

  • Trailing slashes: on the source, a trailing slash makes no difference; COPY source/ /app/ and COPY 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:

 
**/node_modules
**/*.log
.temp

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:

dockerfile
ENV SRC_DIR /source

COPY $SRC_DIR /app/
ConceptDescription
Build ContextDirectory context from which Docker commands operate
Source PathHost filesystem path for files/directories used in the build
Destination PathTarget location inside the Docker image
Trailing Slash BehaviorIgnored on the source; on the destination, copies into that directory
File PermissionsHost permissions affect those in the Docker image; adjust as needed
Environment VariablesDockerfile commands can use environment variables for dynamic paths
Selective CopyingCopy specific subdirectories or files using precise path specifications
Ignore FilesUse .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.


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