0
0
DockerConceptBeginner · 3 min read

What is COPY in Dockerfile: Simple Explanation and Usage

The COPY command in a Dockerfile copies files or directories from your local machine into the Docker image during build. It helps include your app code or configuration files inside the image so they are available when the container runs.
⚙️

How It Works

Think of the COPY command as packing your suitcase before a trip. You pick the files or folders from your computer (your home) and put them inside the Docker image (your suitcase) so they travel with you. When the container starts, it has everything it needs inside.

During the Docker image build, COPY takes the specified files from the build context (the folder where you run the build) and places them into the image at the path you choose. This is a one-time action done while creating the image, not when running the container.

💻

Example

This example copies a local file named app.py into the image's /app folder.

dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY app.py /app/
CMD ["python", "app.py"]
Output
When you build and run this image, the container will have the app.py file inside /app and run it with Python.
🎯

When to Use

Use COPY when you want to add your application code, configuration files, or any static assets into the Docker image. This is essential for making your app self-contained and portable.

For example, if you have a web app, you copy the source code and static files so the container can serve them. Or if you have config files, you include them so the app knows how to behave.

Key Points

  • COPY copies files from your local build folder into the image.
  • It runs during image build, not when the container starts.
  • Paths are relative to the build context (where you run docker build).
  • Use it to include app code, configs, or assets inside the image.
  • For copying files from URLs or other images, use ADD or other commands.

Key Takeaways

COPY adds local files into the Docker image during build.
It makes your app and files available inside the container.
Use relative paths from the build context for source files.
It runs only once when building the image, not at container start.
For remote files or archives, consider ADD instead.