1

I have Maven Multi-Modules Project (Angular Frontend Module + SpringBoot Backend Module) residing under same Parent Maven Project. I want to run the application in Docker Multi-containers (docker container for Frontend + docker container for Backend + docker container for database). How can I use Docker Compose to run docker multi-containers?

I created "docker-compose.yml" in the parent project, and in each module I add "Dockerfile".
Here is structure of my project

enter image description here

docker-compose.yml File

version: '3'

services:
  docker-container-application-mariadb:
    image: mariadb:latest
    environment:
      - MYSQL_ROOT_PASSWORD=*****
      - MYSQL_DATABASE=*******
      - MYSQL_USER=*******
      - MYSQL_PASSWORD=*******
    volumes:
      - /data/application-mariadb
  docker-container-application-backend:
    image: docker-image-application-backend
    build:
      context: ./application-backend
      dockerfile: Dockerfile
    depends_on:
      - docker-container-application-mariadb
    ports:
      - 8087:8080
    volumes:
      - /data/application-backend

Backend Dockerfile:

FROM openjdk:8
EXPOSE 8080
ADD target/application_backend.jar application_backend.jar
# Run the jar file
ENTRYPOINT ["java", "-jar", "application_backend.jar"]
ENTRYPOINT ["java", "-jar", "application_backend.jar"]

When I run:

docker-compose up

here is the error I am getting:

Step 4/6 : ADD $PWD/application-backend/target/application_backend.jar application_backend.jar
ERROR: Service 'docker-container-application-backend' failed to build: ADD failed: stat /var/lib/docker/tmp/docker-builder971325573/esysync-backend/target/application_backend.jar: no such file or directory

Error after code update: enter image description here

Error after Code update suggested by @Milah: enter image description here

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
tlaesus
  • 197
  • 3
  • 13

1 Answers1

2

This should work. I don't have your environment to test so if you get errors let me know and I'll fix them.

Place this Docker file at the root of the project (same level as docker-compose).

Dockerfile:

FROM maven:3.6.0-jdk-8-alpine as build

WORKDIR /app

COPY . .

WORKDIR /app/application-backend

RUN mvn clean package -DskipTests=true

FROM openjdk:8

WORKDIR /app

COPY --from=build /app/application-backend/target/*.jar ./

EXPOSE 8080

# Run the jar file
ENTRYPOINT ["java", "-jar", "/app/application_backend.jar"]

Next to the Dockerfile there should be also an ignore file.

.dockerignore

application-frontend/

You should change the docker-compose file as follows:

...
  docker-container-application-backend:
    image: docker-image-application-backend
    build:
      context: ./
      dockerfile: Dockerfile
...

The declaration dockerfile: Dockerfile is redundant, can be rmoved.

Mihai
  • 9,526
  • 2
  • 18
  • 40
  • Thanks a lot for your quick answer. I tested your code however got the following errors, please see update of my question. Please just to remind I have multimodule maven project. So parent project containing 2 modules: frontend and backend. Dockerfiles in each module and docker-compose in parent project – tlaesus Apr 04 '19 at 12:41
  • my bad, I didn't see that parent pom in the root folder... it will be tough to get it work without changing the structure of the project. Do you really need the parent pom.xml? In the meantime I am thinking of a way to fix it. – Mihai Apr 04 '19 at 12:57
  • @TigrisLaesus I updated the post, I think this should work. – Mihai Apr 04 '19 at 13:09
  • All my best regards to your efforts.. I still did not test what you sent to me...But I have one question before executing your code, in fact, as I mentioned, I have another Module: Frontend, that has to run later in a seperate docker container, so this module has also its own Dockerfile, and according to you I should put the Dockerfile of the Backend at the root level (same level of the parent), so what about Frontend Module Dockerfile – tlaesus Apr 04 '19 at 15:36
  • @Milah: I executed the code you sent to me, however I am still getting errors. I already updated my question with snapshot of the error. – tlaesus Apr 04 '19 at 15:59
  • Then we have to place both dockerfiles at root level and rename them. I'll update the solution later today – Mihai Apr 04 '19 at 15:59
  • Thanks, but I just executed the code without Frontend Dockerfile, and got the errors you see in the snapshot. – tlaesus Apr 04 '19 at 16:01
  • 2
    the error you get is exactly the same which is really weird. are you sure you are running the updated version? Before running the maven build can you add this "RUN pwd && ls -lart" - this should print in the log the current location where it tries to run the build and the contents of that folder. If you . can make this available with your structure on github or similiar maybe I can fix it directly there... Keep in mind that the solution depends also on the structure of the pom files which at the moment I am only guessing. In any case... the same error is really weird. – Mihai Apr 04 '19 at 16:40