Dockerfile copy keep subdirectory structure – Copy

Photo of author
Written By M Ibrahim
boot2docker copy-paste dockerfile

Quick Fix: Remove the asterisk (*) from the COPY command to copy the contents of the ‘files’ directory to the ‘/files’ directory in the container while preserving the subdirectory structure.

The Problem:

How can I maintain the subdirectory structure when copying files from a local directory to a Docker image using Dockerfile’s COPY command, while copying the required files into their respective subdirectories within the container’s file system?

The Solutions:

Solution 2: Merge Local Directory into Image Directory

To merge a local directory into a directory within an image, use the following syntax:

“`
COPY ./local-path/. /image-path/
“`

This will not delete any files already present within the image. It will only add files that are present locally, overwriting the files in the image if a file of the same name already exists.

By utilizing this method, the subdirectory structure of the local directory will be preserved within the image, ensuring that the files are placed in their respective directories.

Solution 3: Use "." instead of *

You can use a "." instead of *, as this will take all the files in the working directory, include the folders and subfolders:

FROM ubuntu
COPY . /
RUN ls -la /

Here’s how it works:

  1. FROM ubuntu: This line specifies the base image for your Docker image. In this case, it’s Ubuntu.

  2. COPY . /: This line copies all the files and folders from the current directory of your host machine to the root directory of the container image. The dot (".") represents the current directory.

  3. RUN ls -la /: This line runs the ls command inside the container to list all the files and folders in the root directory. You can use this command to verify that the files and folders were copied correctly.

Using this method, you can copy entire directories and their contents, preserving the subdirectory structure, into your Docker image.

Solution 4: Use a dot for the current directory

The solution is to specify the current directory by adding a dot before the destination path in the Dockerfile’s COPY command. Here’s the revised Dockerfile:

FROM ubuntu
WORKDIR /usr/local
COPY files/ ./files/

In this Dockerfile:

  • FROM ubuntu: This line specifies the base image for your Docker container. In this case, we’re using the official Ubuntu image.

  • WORKDIR /usr/local: This line sets the working directory inside the container to /usr/local. This means that all subsequent commands in the Dockerfile will be executed relative to this directory.

  • COPY files/ ./files/: This is the COPY command that copies files from your local machine into the container. The first argument,
    files/, specifies the source directory on your local machine. The second argument, ./files/, specifies the destination directory inside the container. By adding a dot (.) before the destination path, we’re telling Docker to copy the contents of the files/ directory into the files/ directory inside the container, preserving the subdirectory structure.

To verify the result:

  • docker run -it sh: This command will run an interactive shell inside the container. You can use this shell to inspect the contents of the container’s file system.

  • ls: Use the ls command to list the contents of the files/ directory. You should see subdirectories like folder1 and folder2, which contain the respective files.