Search code examples
dockerdirectorydockerfile

Copy all files of sub and nested sub directories


This is my project file structure:

java-project/
├── docker.compose.yml
├── pom.xml
└── services/
    ├── a/
    │   ├── Dockerfile
    │   ├── pom.xml
    │   ├── src/
    │   │   ├── pom.xml
    │   │   ├── xxx
    │   │   └── xxx
    │   └── target/
    │       ├── pom.xml
    │       └── xxxx
    └── b/
        ├── Dockerfile
        ├── pom.xml
        ├── src/
        │   ├── pom.xml
        │   ├── xxx
        │   └── xxx
        └── target/
            ├── pom.xml
            └── xxxx

I want to copy all of the contents of the services folder of the project (including all the subfolders inside the services). Basically, I want to replicate the current project structure with every file and folder in the docker image as well for the mvn build to execute successfully.

I am doing the following in the Dockerfile, but I don't see all of the contents:

COPY services/**/pom.xml ./services/

What am I doing wrong here? TIA


Solution

  • Let's look at your COPY instruction:

    #    <src>               <dest>
    COPY services/**/pom.xml ./services/
    

    Under the hood, Docker reads the <src> using Go's filepath.Match method. This means that the instruction doesn't use the globstar (**) the way glob patterns do. However, your question suggests you want to copy everything inside services — not only pom.xml files.

    You can copy everything inside your local services directory using:

    COPY services ./services/
    

    If you want to exclude certain subdirectories or files, you can specify this using a .dockerignore.