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
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"?