Search code examples
javadockermavendocker-composemonorepo

Dockerfile - get contents of the root directory


I'm building images of multiple services from docker-compose.yml. For eg if there are 3 services a,b & c and there are changes in service a folder, the image would be built using the command docker-compose a-service. however the service in the docker-compose.yml file has been written in a way where it is given a build context. for e.g

services:
   a-service:
       container-name: a-service
       build:
          context: ./services/a

and this is the project file structure

java-project/
├─ docker-compose.yml
├─ services/
│  ├─ a/
│  │  ├─ Dockerfile
│  │  ├─ pom.xml
│  ├─ b/
│  │  ├─ pom.xml
│  │  ├─ Dockerfile
├─ pom.xml

Dockerfile

#
# Build stage
#
FROM maven:3.6.3-jdk-11 as build
***fails at this steps****
RUN mvn clean install -DskipTests

#
# Package stage
#
FROM amazoncorretto:11-alpine-jdk
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
COPY src/main/resources/META-INF/xxx xxxx
ENTRYPOINT ["java","-jar","/app.jar"]

Now when the service a is built, the Dockerfile present in service a folder is referenced and also the pom.xml present in the service a folder is referenced. However, I want the pom.xml present in the root directory of the project to be referenced for the maven build as part of the image creation process (maven build requires the pom.xml to create jar files. These jar files would be copied to the image). I tried the following in the service a Dockerfile but still no luck. Any idea how I can achieve this? RUN cd ../../pom.xml or

RUN mkdir tmp-context
RUN cp -R ../../pom.xml tmp-context

or COPY ../../pom.xml . tldr - How do i get the contents of the project in the root directory from a sub directory in a Dockerfile


Solution

  • You can change the context and add the location of your dockerfile like so:

    services:
      a-service:
        container-name: a-service
          build:
            context: ./
            dockerfile: ./services/a/Dockerfile