0

i have database in docker on my server, i write spring boot App that connect to that database from my local computer sucessfull, however when i was trying to containerize my app with docker in same server, app cannot connect to my database. in both cases i using my server public IP.

here is my database docker-compose:

version: '3.8'
services:
  db:
    image: postgres:14.1-alpine
    restart: always
    environment:
      - POSTGRES_USER=xxx
      - POSTGRES_PASSWORD=xxx
    ports:
      - '5432:5432'
    volumes:
      - db:/var/lib/postgresql/data
  pgadmin:
    container_name: pgadmin4_container
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: xxx
      PGADMIN_DEFAULT_PASSWORD: xxx
    ports:
      - "5050:80"
volumes:
  db:
    driver: local

and my app docker-compose:

version: '3'
services:
  backend:
    build: .
    ports:
      - 9090:9090
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://XXX_IP:5432/db_name
      - SPRING_DATASOURCE_USERNAME=XXX
      - SPRING_DATASOURCE_PASSWORD=XXX
      - DRIVER_CLASS_NAME=org.postgresql.Driver

and if it matters my app Dockerfile:

FROM openjdk:19-jdk-alpine
ARG JAR_FILE=*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

i aslo trying link my db docker compose in app compose with something like that:

version: '3'
services:
  backend:
    build: .
    ports:
      - 9090:9090
    links:
      - db
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/io_projekt
      - SPRING_DATASOURCE_USERNAME=xxx
      - SPRING_DATASOURCE_PASSWORD=xxx
      - DRIVER_CLASS_NAME=org.postgresql.Driver

but but it caused an error: no such service: db.

so ther is my question: what i doing wrong, and what can i do to connect my app docker to database docker?

  • The easiest approach is just to put your application-specific database container into the same Compose file as the application proper. But if you really need multiple Compose files, you need to arrange for them to use the same Docker network; the linked question has several examples. – David Maze Jan 20 '23 at 20:49

0 Answers0