0

I have the docker-compose.yml:

services:
  belt-microservice:
    build: belt-microservices
    container_name: belt-microservice
    restart: always
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://db-belt:5432/belt_microservices
    ports:
      - "8080:8080"
    depends_on:
      - db-belt
  crossbelt-microservice:
    build: crossbelt-microservices
    container_name: crossbelt-microservice
    restart: always
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://db-crossbelt:5432/crossbelt_microservices
      - BELT_URL=http://belt-microservice:8080
    ports:
      - "8082:8082"
    depends_on:
      - db-crossbelt
      - belt-microservice
  report-microservice:
    build: report-microservices
    container_name: report-microservice
    restart: always
    ports:
      - "8083:8083"
  db-belt:
    image: postgres
    container_name: db-belt
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=belt-microservices
      - PGDATA=/var/lib/postgresql/data/pgdata
    ports:
      - "5000:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
    restart: always
  db-crossbelt:
    image: postgres
    container_name: db-crossbelt
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=crossbelt-microservices
      - PGDATA=/var/lib/postgresql/data/pgdata
    ports:
      - "5001:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:

My solution is based on Docker Compose + Spring Boot + Postgres connection, as per the comment on my previous question Connection to postgresql from spring boot refused in oracle openjdk docker container.

5 services:

  • 3 Spring Boot apps:
    • belt microservice depending on database db_microservices
    • crossbelt microservice depending on the aforementioned belt microservice and db_crossbelt
    • report microservice
  • 2 database services based on postgres:
    • db-belt
    • db-crossbelt

Belt microservice has the application.yml:

server:
  port: 8080
spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://0.0.0.0:5432/belt_microservices
    username: postgres
    password: password

The other microservice, crossbelt microservice has the application.yml:

server:
  port: 8082
belt-service-url: ${BELT_URL=http://localhost:8080}
spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/crossbelt_microservices
    username: postgres
    password: password

As per the answer on the other question I assume that spring.datasource.url will be overridden by the env variable SPRING_DATASOURCE_URL set in the docker-compose.yml.

I'm assuming that setting POSTGRES_DB environment var in the database services' configurations they're gonna rename the databases inside of them to the given names. As per the answers to the question Multiple databases in docker and docker-compose I assume that it's a good approach to have a separate databases for my microservices.

Now the problem is that during setting it up using docker compose, I get

crossbelt-microservice: org.postgresql.util.PSQLException: FATAL: database "crossbelt_microservices" does not exist

But then I changed crossbelt-microservices to crossbelt_microservices in the service's configuration and after running it again I got

db-crossbelt            | 
db-crossbelt            | PostgreSQL Database directory appears to contain a database; Skipping initialization
db-crossbelt            | 
db-belt                 | 
db-belt                 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db-belt                 | 

and the same connection problem in the spring app containers.

There's a problem with the containers, how to solve it?

1 Answer 1

0

I guess I'm getting forward.

I deleted the volume enter image description here associated with postgresql data. I guess you can do the same using docker volume ls and docker volume rm <volume name>. Then I reran it with the db names changed to underscores and now I'm having another problem.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.