I have 2 dockerized microserices which connect to postres container, but first container connects correctly, second fails. This is properties of order-service(which connects correctly):
spring.datasource.url=jdbc:postgresql://postgres:5431/order-service
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=matvey
spring.datasource.password=matvey
Docker compose:
postgres-order:
container_name: postgres-order
image: postgres
environment:
POSTGRES_DB: order-service
POSTGRES_USER: matvey
POSTGRES_PASSWORD: matvey
PGDATA: /data/postgres
volumes:
- ./volumes/postgres-order:/data/postgres
expose:
- "5431"
ports:
- "5431:5431"
command: -p 5431
restart: unless-stopped
order-service:
container_name: order-service
image: zaxarleningod/order-service
environment:
- SPRING_PROFILES_ACTIVE=docker
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgres-order:5431/order-service
depends_on:
- postgres-order
- broker
- discovery-server
- api-gateway
This is inventory service properties(fails):
spring.datasource.url=jdbc:postgresql://postgres:5432/inventory-service
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=matvey
spring.datasource.password=matvey
Docker compose:
postgres-inventory:
container_name: postgres-inventory
image: postgres
environment:
POSTGRES_DB: inventory-service
POSTGRES_USER: matvey
POSTGRES_PASSWORD: matvey
PGDATA: /data/postgres
volumes:
- ./volumes/postgres-inventory:/data/postgres
ports:
- "5432:5432"
restart: unless-stopped
#Inventory Service
inventory-service:
container_name: inventory-service
image: zaxarleningod/inventory-service
environment:
- SPRING_PROFILES_ACTIVE=docker
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgres-inventory:5432/inventory-service
depends_on:
- postgres-inventory
- discovery-server
- api-gateway
I can understand what problem it is:
This is what i see in logs: