0

I use Spring Boot application inside Docker container on Ubuntu 18 (in a Virtual Box). My application should connect to Postgres database installed on this same VM.

I followed all steps with this tutorial: https://nickjanetakis.com/blog/docker-tip-35-connect-to-a-database-running-on-your-docker-host

I defined IP of my host by ifconfig: 192.168.1.67

enter image description here

My application.properties:

server.port=2020
ip.address=localhost      # <- will be overwritten
spring.datasource.driverClassName =org.postgresql.Driver
spring.jpa.database=POSTGRESQL
spring.datasource.url=jdbc:postgresql://${ip.address}:5432/postgres

Dockerfile:

FROM openjdk:8-alpine

MAINTAINER Kirill Ch "kirill.m.ch@gmail.com"

EXPOSE 2020

WORKDIR /usr/local/bin

COPY server.jar .

CMD ["java","-Dip.address=192.168.1.67", "-jar", "server.jar"]

I create image:

docker image build -t ws-server .

My PosgreSQL server is up and running. However when I run:

docker container run -it -p 2020:2020 ws-server

I receive this error:

org.postgresql.util.PSQLException: Connection to 192.168.1.67:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

If I run my app normally without Docker all goes right.

I suspect that the issue with a virtual machine however do not know how to solve it if so.

If I use --network=host parameter all works:

docker container run -it --network=host ws-server

What do I miss, how do it properly, without "host"?

Community
  • 1
  • 1
Kirill Ch
  • 5,496
  • 4
  • 44
  • 65
  • your application inside the docker is trying to reach something outside the docker. Then you need to expose that port and also define it when starting up. – Toerktumlare Aug 07 '19 at 21:43
  • 1
    @ThomasAndolf "Expose" doesn't mean very much in modern-day Docker. As a general rule you don't need to do anything special to make outbound connections from a container; a `--expose` or `-p` option will not make any difference here. – David Maze Aug 07 '19 at 23:05
  • okey read now that things have changed since i worked with docker, i learnt something new today, thank you – Toerktumlare Aug 08 '19 at 00:22

0 Answers0