Bird
Raised Fist0
Dockerdevops~5 mins

COPY instruction for adding files in Docker - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What does the COPY instruction do in a Dockerfile?
easy
A. It copies files or folders from your computer into the Docker image.
B. It copies files from the Docker image to your computer.
C. It copies environment variables into the container.
D. It copies running containers to new images.

Solution

  1. Step 1: Understand the purpose of COPY

    The COPY instruction is used to transfer files or folders from the build context (your computer) into the Docker image during build time.
  2. Step 2: Differentiate from other options

    Options A, B, and D describe actions not performed by COPY. COPY does not move files out of the image or handle environment variables or containers directly.
  3. Final Answer:

    It copies files or folders from your computer into the Docker image. -> Option A
  4. Quick Check:

    COPY moves files into image = C [OK]
Hint: COPY moves files from host to image during build [OK]
Common Mistakes:
  • Thinking COPY moves files from image to host
  • Confusing COPY with environment variable setting
  • Assuming COPY copies running containers
2. Which of the following is the correct syntax to copy a file named app.py from your local folder to /usr/src/app/ inside the Docker image?
easy
A. COPY app.py > /usr/src/app/
B. COPY /usr/src/app/ app.py
C. COPY app.py /usr/src/app/
D. COPY app.py : /usr/src/app/

Solution

  1. Step 1: Understand COPY syntax

    The correct syntax is COPY <source> <destination>. The source is the file on your computer, and the destination is the path inside the image.
  2. Step 2: Check each option

    COPY app.py /usr/src/app/ correctly uses COPY app.py /usr/src/app/. Options B, C, and D use incorrect order or invalid symbols.
  3. Final Answer:

    COPY app.py /usr/src/app/ -> Option C
  4. Quick Check:

    COPY source destination = A [OK]
Hint: COPY syntax is COPY source destination [OK]
Common Mistakes:
  • Reversing source and destination
  • Using symbols like > or : instead of space
  • Adding slashes incorrectly
3. Given this Dockerfile snippet:
FROM python:3.12
COPY src/ /app/
RUN ls /app/

What will be the output of RUN ls /app/ if the local folder src contains files main.py and utils.py?
medium
A. src
B. main.py
C. No such file or directory
D. main.py utils.py

Solution

  1. Step 1: Understand COPY with folder source

    Using COPY src/ /app/ copies all contents inside src into /app/ inside the image.
  2. Step 2: Predict RUN ls output

    Since src contains main.py and utils.py, listing /app/ will show these two files.
  3. Final Answer:

    main.py utils.py -> Option D
  4. Quick Check:

    COPY folder copies contents = B [OK]
Hint: COPY folder/ dest copies folder contents, not folder itself [OK]
Common Mistakes:
  • Expecting folder name to appear inside destination
  • Thinking COPY copies folder as a subfolder
  • Assuming error if folder exists
4. You wrote this Dockerfile line:
COPY --chown=appuser:appgroup config.yaml /etc/config/

But during build, you get an error: invalid flag: --chown. What is the likely cause?
medium
A. Docker version is too old and does not support --chown flag.
B. The source file config.yaml does not exist.
C. The destination path /etc/config/ is invalid.
D. The --chown flag must be placed after the destination path.

Solution

  1. Step 1: Understand --chown flag usage

    The --chown flag sets ownership of copied files but requires Docker version 17.09 or newer.
  2. Step 2: Analyze error message

    An 'invalid flag' error usually means the Docker version does not recognize the flag, indicating it is too old.
  3. Final Answer:

    Docker version is too old and does not support --chown flag. -> Option A
  4. Quick Check:

    Old Docker lacks --chown support = D [OK]
Hint: Check Docker version if --chown flag errors appear [OK]
Common Mistakes:
  • Assuming source file missing causes invalid flag error
  • Thinking --chown position matters
  • Blaming destination path for flag errors
5. You want to copy multiple files index.html and style.css from your local folder into /var/www/html/ inside the Docker image using a single COPY instruction. Which is the correct way?
hard
A. COPY ["index.html", "style.css"] /var/www/html/
B. COPY index.html style.css /var/www/html/
C. COPY index.html /var/www/html/ style.css
D. COPY index.html; style.css /var/www/html/

Solution

  1. Step 1: Understand multiple source files syntax

    To copy multiple files, list them separated by spaces before the destination path.
  2. Step 2: Evaluate options

    COPY index.html style.css /var/www/html/: COPY index.html style.css /var/www/html/ correctly lists files followed by destination. COPY ["index.html", "style.css"] /var/www/html/ mixes JSON array for sources with space-separated destination, which is invalid syntax. Options C and D have incorrect syntax.
  3. Final Answer:

    COPY index.html style.css /var/www/html/ -> Option B
  4. Quick Check:

    Multiple files listed before destination = A [OK]
Hint: List multiple files separated by spaces before destination [OK]
Common Mistakes:
  • Using JSON array syntax incorrectly
  • Placing destination between source files
  • Using semicolons instead of spaces
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard