Search code examples
javamavendockerdockerfiledocker-build

Docker build fails to find source folder for COPY command


I have a multi module maven project, and I want to create and publish an image for each module, except for a common, utility module, which is a dependency for all the other modules.

Project structure is like this:

project_root
 common_module
    -pom.xml
 module_a
    -pom.xml
    -Dockerfile
 module_b
    -pom.xml
    -Dockerfile
 -pom.xml
 -docker-compose.yml

To build module_a fox example, I need to copy the common_module folder too to the container, and run mvn install on the common folder first. If that is built, I can run mvn install on the module_a pom.xml too.

I'm new to Docker, and this is what I've been trying, which I think should work, according to the documentation of the COPY command, which states:

Multiple src resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build.

I have the following Dockerfile in module_a:

FROM openjdk:8-jre-alpine

# Install Maven
# (skipped for brevity)

# Create the project root
RUN mkdir -p /usr/src/project_root
WORKDIR /usr/src/project_root

# Copy all the necessary files for the packaging
# Copy the common module
COPY common_module /usr/src/project_root/common_module 
# Copy the service registry module (src + config folder)
COPY module_a /usr/src/project_root/module_a 

# Run maven install
RUN cd common_module 
RUN mvn clean install

RUN cd ../module_a 
RUN mvn clean install

And I issue this command from the project_root folder (making it the context of the build?):

docker build -t image_name:4.0 ./module_a

The build fails at the first COPY command, stating:

COPY failed: stat /var/lib/docker/tmp/docker-builder380120612/common_module: no such file or directory

I'm not even sure the RUN commands would work, as the build fails before those.

What am I doing wrong? How can one copy folders/files outside of the Dockerfile location?

By the way I'm running Docker version 18.03.1-ce, build 9ee9f40, on Windows 10 Pro.


Solution

  • When you run the command

    docker build -t image_name:4.0 ./module_a
    

    You define the build context as the module_a directory. Then you try to COPY the directory common_module into your container but it's not possible as it is not in the build context.

    Use this command instead:

    docker build -t image_name:4.0 -f ./module_a/Dockerfile .
    

    This way module_a, common_module and module_b will be included in the build context.