0

My Postgres DB is running in a Docker container. When container is started, it says it's ready to listen on 5432. My application container is set to depend on it.

    container_name: my_postgres_db
    image: library/postgres:latest
    network_mode: bridge
    expose:
      - 5432
    ports:
      - 5432:5432
    environment:
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=admin
      - POSTGRES_DB=localdb
    restart: always

The config for app:

    container_name: my_test_app
    depends_on:
      - db
    build:
      context: ./
      dockerfile: Dockerfile
    image: my_test_app
    ports:
      - 8080:8080

Based on solutions to the similar questions, I changed the localhost in the DB URL to: spring.datasource.url=jdbc:postgresql://db:5432/localdb

It causes another error = "Unknown host exception". Even if I manage to build app this way, it still doesn't work. Logs say, Connection to localhost:5432 refused What else am I missing? Why is it still listening to localhost:5432 when I obviously changed it to db:5432 and gradlew clean/build it?

1
  • i assume you have a problem with the networking you can read more docs.docker.com/compose/networking and fix it by adding networks instead of exposing the port Commented Sep 27, 2020 at 12:31

1 Answer 1

0

just change the network_mode in postgres and app to host

network_mode: host

note that this will ignore the expose option and will use the host network an containers network

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

1 Comment

Thank you very much! It did the trick. I set both 'app' and 'db' network_mode in docker-compose.yml to host. I mark your answer as the solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.