0

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.

Community
  • 1
  • 1
logi0517
  • 813
  • 1
  • 13
  • 32

1 Answers1

3

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.

StephaneM
  • 4,779
  • 1
  • 16
  • 33
  • But then it is unable to find the Dockerfile inside module_a folder (it will only look for it, inside the root folder). I hoped running the command in the root folder would mark that as the build context :( If I run the build command from the module_a folder with "image_name .", then I will get that the COPY fails because the common folder is outside of the build context. as far as I know, the argument after the image name, should be the Dockerfile location. – logi0517 Jul 17 '18 at 12:49
  • Just use the `-f' flag to tell docker where the Dockerfile is. – StephaneM Jul 17 '18 at 12:52
  • I also tried that, but it says the build command can only have 1 argument. Can you give me an example how should I format my command with both the -t and -f flags? – logi0517 Jul 17 '18 at 12:53
  • See my updated answer, the -f option must be *before* the path. – StephaneM Jul 17 '18 at 12:54
  • thank you for the example, the copy commands worked now, now I just have to find out, why the maven install failed :) edit: turns out it is just best to use WORKDIR commands instead of cd – logi0517 Jul 17 '18 at 12:58