0

I have a docker file that allows me to build a springboot project.

FROM eclipse-temurin:17-jdk-jammy as builder

RUN addgroup installergroup; adduser  --ingroup installergroup --disabled-password installer
USER installer

WORKDIR /opt/app
# note we pass the path
COPY ../../packages/backend/users/users-ws/.mvn ./.mvn
COPY ../../packages/backend/users/users-ws/mvnw ./mvnw
COPY ../../packages/backend/users/users-ws/pom.xml ./pom.xml


RUN #./mvnw dependency:go-offline
COPY ../../packages/backend/users/users-ws/src ./src
RUN ./mvnw clean install

FROM eclipse-temurin:17-jre-jammy
WORKDIR /opt/app

EXPOSE 8440
COPY --from=builder /opt/app/target/*.jar /opt/app/*.jar
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=docker", "/opt/app/*.jar" ]

This application works with postgres and pgadmin. For development purpose, I also added nexus3 to a docker-compose file.

If i run docker compose, I start my different services. Here is my implementation of my docker compose file:

version: "3.8"
services:
  postgresdb:
    image: postgres:latest
    container_name: postgres
    restart: always
    user: root
    healthcheck:
      test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
      timeout: 45s
      interval: 10s
      retries: 10
    environment: 
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
      POSTGRES_DB: innonetwork
      POSTGRESQL_MAX_IDLE_CONNS: "0"
      POSTGRESQL_MAX_OPEN_CONNS: "0"
    networks: 
      - postgres-network
    ports:
      - '5432:5432'
    expose:
      - '5432'
    volumes: 
      - ./data/dbstore/postgres/postgres:/var/lib/postgresql/data
      - ./setup/postgres:/docker-entrypoint-initdb.d
           
  pgAdmin:    
    image: dpage/pgadmin4:latest
    container_name: pgAdmin
    restart: always
    user: root
    depends_on:
      postgresdb:
        condition: service_started
    networks: 
      - postgres-network
    environment:
      PGADMIN_DEFAULT_EMAIL: root@root.com
      PGADMIN_DEFAULT_PASSWORD: root
    ports:
      - "5050:80"
    volumes:
       - ./data/dbstore/postgres/pgadmin:/var/lib/pgadmin4/storage
       - ./setup/postgres/pgadmin4.db:/pgadmin4.db
    entrypoint: /bin/sh -c "cp /pgadmin4.db /var/lib/pgadmin4/storage/pgadmin4.db && cd /pgadmin4 && /entrypoint.sh"

  mongodb:
    image: mongo:latest
    container_name: mongo
    environment:
      - MONGO_INITDB_DATABASE=innonetwork
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=root
      - MONGODB_USERNAME=root
      - MONGODB_PASSWORD=root
      - MONGODB_DATABASE=innonetwork
    volumes:
      - ./data/dbstore/mongodb/:/data/db/
      - ./setup/mongodb/docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
    restart: always
    networks: 
      - mongo-network
    ports:
      - 27017:27017
    expose:
      - '27017'

  mongo-express:
    image: mongo-express:latest
    container_name: mongo-express
    restart: always
    ports:
      - 8082:8081
    environment:
      - ME_CONFIG_MONGODB_SERVER=mongo
      - ME_CONFIG_MONGODB_ADMINUSERNAME=root
      - ME_CONFIG_MONGODB_ADMINPASSWORD=root
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
      - ME_CONFIG_BASICAUTH_USERNAME=root
      - ME_CONFIG_BASICAUTH_PASSWORD=root
    volumes:
      - ./data/dbstore/mongodb/:/data/db/
    links: 
      - mongodb
    depends_on:
      mongodb:
        condition: service_started
    networks: 
      - mongo-network

  nexus3:
    image: 'sonatype/nexus3'
    init: true
    healthcheck:
      test: ['CMD', 'curl', '-fIu', 'admin:admin123', 'http://localhost:8081/service/metrics/ping']
      interval: 45s
      timeout: 30s
      retries: 6
    restart: on-failure
    ports:
      - 8081:8081
    expose:
      - 8081/tcp
    networks:
      - internal
    volumes:
      - ./data/nexus-data:/nexus-data

networks:
  postgres-network:
  mongo-network:
  internal:
    driver: bridge

volumes:
  nexus-data:

I can use dependencies stored in nexus when creating my webservice image. it requires to run the following: docker build -t innonetwork/users-ws --network host .

While doing this, I can build my image and run my webservice. Now I want to add the microservice in my docker compose file, because, i plan to have multiple microservices and i want to centralize all my dev environement. For this I added the following to my docker compose file:

users-ws:
  image: innonetwork/users-ws
  container_name: users-ws
  network_mode: "bridge"
  restart: always
  build:
    context: ../
    dockerfile: ./docker/dockerfiles/Dockerfile.users-ws
  ports:
    - "8440:8440"
  depends_on:
    mongodb:
      condition: service_completed_successfully
    postgresdb:
      condition: service_completed_successfully
    nexus3:
      condition: service_completed_successfully
  links:
    - nexus3
    - mongodb
    - postgresdb
  networks:
    - internal

At this stage I have 2 issues:

  1. I can't set the order of deployement, even if i'm using depends on and set a condition. The first to be launched is my webservice... that requires nexus3 to get dependencies
  2. I can get dependencies from external repositories but i can't get the dependencies I created for my applications.

Here is the error message:

Failed to execute goal on project users-ws: Could not resolve dependencies for project myapp:users-ws:jar:0.0.1-SNAPSHOT: 
Failed to collect dependencies at myapp:commons:jar:1.0-SNAPSHOT: 
Failed to read artifact descriptor for myapp:commons:jar:1.0-SNAPSHOT: Could not transfer artifact myapp:commons:pom:1.0-SNAPSHOT from/to maven-public (http://localhost:8081/repository/maven-public/): 
transfer failed for http://localhost:8081/repository/maven-public/myapp/commons/1.0-SNAPSHOT/commons1.0-SNAPSHOT.pom: Connect to localhost:8081 [localhost/127.0.0.1] failed: 
Connection refused -> [Help 1]
davidvera
  • 1,292
  • 2
  • 24
  • 55

1 Answers1

1

Your application's pom.xml should point to http://nexus3:8081/repository instead of http://localhost:8081/repository (for snapshotsRepository or something like that).

And you should remove all network related configurations since you don't need them (also don't run docker-compose with networking host).

Regarding the startup order, you should change service_completed_successfully with service_healthy. Something like this:

services:
  users-ws:
    build: .
    depends_on:
      nexus3:
        condition: service_healthy
      postgresdb:
        condition: service_started

You use service_completed_successfully if you have a database migration container the starts, does a job and exits. In your case you are interested that the dependencies are healthy, not that they exit without errors.

Mihai
  • 9,526
  • 2
  • 18
  • 40
  • It did the trick partially for delaying startup. But now i have Blocked mirror for repositories: [maven-public (http://nexus3:8081/repository/maven-public/, default, releases+snapshots)] -> [Help 1] – davidvera Mar 16 '23 at 06:44