0
0
Dockerdevops~5 mins

COPY instruction for adding files in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When building a Docker image, you often need to add files from your computer into the image. The COPY instruction lets you do this simply and safely by copying files or folders into the image during build time.
When you want to include your application code inside a Docker image to run it in a container.
When you need to add configuration files or scripts into the image for your app to use.
When you want to copy static assets like images or HTML files into the container.
When you want to add a local folder with multiple files into the image in one step.
When you want to prepare the image with all necessary files before running it.
Config File - Dockerfile
Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY src/ ./src/
CMD ["python", "src/app.py"]

FROM sets the base image with Python 3.11 slim version.

WORKDIR sets the working directory inside the image to /app.

COPY requirements.txt ./ copies the requirements file from your computer into the image's /app folder.

RUN pip install installs Python packages listed in requirements.txt.

COPY src/ ./src/ copies the entire local src folder into /app/src inside the image.

CMD defines the command to run your app when the container starts.

Commands
This command builds a Docker image named 'my-python-app' using the Dockerfile in the current folder. It processes the COPY instructions to add files into the image.
Terminal
docker build -t my-python-app .
Expected OutputExpected
Sending build context to Docker daemon 12.3MB Step 1/6 : FROM python:3.11-slim ---> 123abc456def Step 2/6 : WORKDIR /app ---> Using cache ---> 789def012abc Step 3/6 : COPY requirements.txt ./ ---> Using cache ---> 345ghi678jkl Step 4/6 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in abc123def456 Collecting flask Downloading Flask-2.3.2-py3-none-any.whl (96 kB) Installing collected packages: flask Successfully installed flask-2.3.2 Removing intermediate container abc123def456 ---> 901mno234pqr Step 5/6 : COPY src/ ./src/ ---> Using cache ---> 567stu890vwx Step 6/6 : CMD ["python", "src/app.py"] ---> Using cache ---> 234yz012abc Successfully built 234yz012abc Successfully tagged my-python-app:latest
-t - Assigns a name and optionally a tag to the image
This command runs a container from the 'my-python-app' image. It uses the files copied into the image to start the Python app.
Terminal
docker run --rm my-python-app
Expected OutputExpected
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
--rm - Automatically removes the container when it stops
Key Concept

The COPY instruction copies files or folders from your local machine into the Docker image during build time, making them available inside containers.

Common Mistakes
Using COPY with a source path that does not exist on the local machine.
Docker build will fail because it cannot find the files to copy.
Make sure the source path in COPY exists relative to the Docker build context folder.
Copying files outside the Docker build context folder.
Docker cannot access files outside the build context, causing build errors.
Place all files to copy inside the folder where you run docker build or its subfolders.
Using ADD instead of COPY when only copying local files.
ADD has extra features like extracting archives or downloading URLs, which can cause unexpected behavior.
Use COPY for simple file copying to keep builds predictable and clear.
Summary
COPY adds files or folders from your computer into the Docker image during build.
Use COPY to include app code, configs, or assets inside the image.
Build the image with docker build and run it with docker run to use the copied files.