Sonu Kumar
2 min readJan 11, 2024

How to copy the a folder from a specific directory back to the main folder in a Dockerfile using COPY

To copy a folder from a specific directory back to the main folder in a Dockerfile, you can use the COPY instruction. The syntax for the COPY instruction is:

COPY <source> <destination>

In your case, if you want to copy a folder from two directories back to the main folder, you can use the following Dockerfile snippet:

# Assuming your Dockerfile is in the main folder
# Copy the folder from two directories back to the main folder
COPY ../../your-folder /path/in/container

Replace ../../your-folder with the actual relative path to the folder you want to copy, and /path/in/container with the destination path inside the Docker container.

Here full example with Dockerfile code:

FROM grafana/k6

# Switch to root user for installation
USER root

# Set the working directory
WORKDIR /usr/src/app

# Copy the necessary files
COPY ../../artifacts ./
COPY . /
COPY ./execute_k6.sh /execute_k6.sh

# Grant execute permissions to the script
RUN chmod +x /execute_k6.sh
RUN apk --no-cache add bash
# Switch back to the default non-root user
USER k6

# Set the default command to execute_k6.sh
CMD ["/execute_k6.sh"]

Please note that the paths are relative to the build context, so make sure your Dockerfile and the folder you want to copy are within the build context. If the paths go outside the build context, you might need to adjust the build context or use a different approach.

For more details about the COPY you can go the links and get some more details:

Sonu Kumar

Full stack Web developer with experience and I love reading article such blogs books and writing.