1
votes

Using docker to connect springboot to postgres via docker-compose. Using port 5432 on postgres works fine, if i try an port other than that it fails

working code

spring

spring.datasource.url=jdbc:postgresql://db:5432/wwc
spring.datasource.username=wwc
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver

docker-compose

version: '2.1'
services:
  db:
    container_name: db
    image: postgres:9.4
    ports:
      - 5432:5432
    volumes:
      - /tmp:/var/lib/postgresql
    environment:
      - POSTGRES_USER=wwc
      - POSTGRES_DB=wwc
      - POSTGRES_PASSWORD=test
  server:
    container_name: spring-boot-rest-server
    build:
      context: .
      dockerfile: Dockerfile.server
    ports:
      - 8080:8080
    logging:
      driver: json-file
    depends_on:
      - db

  web:
    container_name: nginx-web
    links:
      - "server:springboot"
    build:
      context: .
      dockerfile: Dockerfile.web
    ports:
      - 80:80
      - 8088:8088
    logging:
      driver: json-file
    depends_on:
      - server

**connection refused code **

spring

spring.datasource.url=jdbc:postgresql://db:6000/wwc
spring.datasource.username=wwc
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver

docker-compose

version: '2.1'
services:
  db:
    container_name: db
    image: postgres:9.4
    ports:
      - 6000:5432
    volumes:
      - /tmp:/var/lib/postgresql
    environment:
      - POSTGRES_USER=wwc
      - POSTGRES_DB=wwc
      - POSTGRES_PASSWORD=test
  server:
    container_name: spring-boot-rest-server
    build:
      context: .
      dockerfile: Dockerfile.server
    ports:
      - 8080:8080
    logging:
      driver: json-file
    depends_on:
      - db

  web:
    container_name: nginx-web
    links:
      - "server:springboot"
    build:
      context: .
      dockerfile: Dockerfile.web
    ports:
      - 80:80
      - 8088:8088
    logging:
      driver: json-file
    depends_on:
      - server

error:

spring-boot-rest-server | org.postgresql.util.PSQLException: Connection to db:6000 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

What am i doing wrong?

2

2 Answers

4
votes

You are confusing a bit the ports: your "db" container only exports 1 port: 5432. The 6000 that you put in your docker-compose is the port on localhost that you map to that container (db) on that port (5432).

You shouldn't even use the port mappings for the postgres container unless you want to connect from localhost which I guess you don't.

If you want to use another port than 5432 you need to extend the postgres Dockerfile and change the configuration so that postgres starts listening on a different port.

Hope this helps.

0
votes

In other words: The port mapping configured in docker-compose has no relevancy to how the containers connect to each other. The mapping is only relevant when something/someone attempts to connect to your containers within the docker-compose from the outside. (Like from the localhost, as @Mihai remarked.)