How to Copy Multiple Files in One Layer Using a Dockerfile

Master the COPY instruction in Docker to efficiently copy multiple files into a single layer and optimize your container builds.

How to Copy Multiple Files in One Layer Using a Dockerfile

When building Docker containers, you’ll often need to copy multiple files into a single layer. The COPY instruction in Docker makes this simple and efficient.

The basic syntax is:

COPY file1.txt file2.txt /app/files/

This copies both file1.txt and file2.txt from your build context into the /app/files/ directory inside the container. You can specify as many source files as needed, separated by spaces.

Benefits of copying multiple files in one layer:

  • Faster builds: Fewer layers mean faster build times
  • Smaller images: Reduces the number of layers in your final image
  • Better organization: Groups related files together

Some other docker articles that can help you in your docker journey:

Using COPY instruction to copy files in a Dockerfile

The COPY instruction transfers files from your host machine to the container’s file system. Here’s how to use it effectively:

Basic Syntax

COPY <source> <destination>
  • <source>: File or directory on your host machine
  • <destination>: Target directory in the container

Common Use Cases

Copy a single file:

COPY app.js /app/

Copy an entire directory:

COPY src/ /app/src/

Copy multiple specific files:

COPY file1.txt file2.txt config.json /app/

Copy files with wildcards:

COPY *.txt /app/

This copies all .txt files to /app/

Including multiple source files in the COPY instruction

Copying Multiple Files in One Instruction

You can specify multiple source files in a single COPY instruction:

COPY file1.txt file2.txt config.json /app/
COPY *.csv /data/
COPY src/. /code/

This approach:

  • Reduces Dockerfile complexity: Fewer instructions to maintain
  • Improves build efficiency: Creates fewer layers
  • Preserves file order: Files are copied in the order you specify

Pro Tip: Group related files together in the same COPY instruction for better organization.

Using wildcards to copy multiple files in a single layer

Using Wildcards for Pattern Matching

Wildcards help you copy multiple files that match specific patterns:

Common Wildcard Patterns

PatternDescriptionExample
*Matches any characters*.txt copies all text files
?Matches single characterfile?.txt matches file1.txt, fileA.txt
**Matches directories recursivelysrc/**/*.js copies all JS files in subdirectories

Practical Examples

# Copy all configuration files
COPY *.conf /etc/app/

# Copy all JavaScript files from any subdirectory
COPY src/**/*.js /app/js/

# Copy specific file types
COPY *.{json,yml,yaml} /app/config/

Important: Always test your wildcard patterns to ensure they match the intended files.

Organizing files in separate directories for better readability

Organizing Files for Better Dockerfile Management

A well-organized directory structure makes your Dockerfile easier to maintain:

project/
├── src/           # Application source code
├── config/        # Configuration files
├── scripts/       # Build and deployment scripts
├── assets/        # Static assets
└── Dockerfile

Best Practices

  1. Group related files: Keep similar files in the same directory
  2. Use descriptive names: Directory names should clearly indicate their purpose
  3. Avoid deep nesting: Keep directory structure simple and shallow
  4. Document structure: Include a README explaining your organization

Example Dockerfile with organized structure:

# Copy source code
COPY src/ /app/src/

# Copy configuration files
COPY config/*.json /app/config/

# Copy build scripts
COPY scripts/build.sh /app/scripts/

Best practices for efficient file copying in a Dockerfile

Best Practices for Efficient File Copying

1. Use .dockerignore

Create a .dockerignore file to exclude unnecessary files:

node_modules/
*.log
.git/
.DS_Store

2. Order by Change Frequency

Copy files that change least frequently first:

# Dependencies (rarely change)
COPY package.json package-lock.json /app/

# Source code (changes often)
COPY src/ /app/src/
# Copy all config files together
COPY config/ /app/config/

# Copy all static assets together
COPY assets/ /app/assets/

4. Be Specific with Wildcards

# Good - specific pattern
COPY *.json /app/config/

# Better - explicit files when possible
COPY package.json tsconfig.json /app/

These practices reduce build time and create more efficient Docker images.

Conclusion

Quick Reference

Here’s a summary of key techniques for copying multiple files in Docker:

# Multiple specific files
COPY file1.txt file2.txt config.json /app/

# Wildcard patterns
COPY *.conf /etc/app/
COPY src/**/*.js /app/js/

# Directory copying
COPY src/ /app/src/
COPY config/ /app/config/

Key Takeaways

  • Use single COPY instructions for multiple related files
  • Leverage wildcards for pattern matching
  • Order files by change frequency for better caching
  • Use .dockerignore to exclude unnecessary files
  • Keep your directory structure organized

Mastering these file copying techniques will make your Docker builds faster and more efficient. Practice with different patterns to find what works best for your specific use case.

Related Posts