0

I have postgres db in docker container and spring web app also in docker container, but when I start container with web app i have an error:

Caused by: java.net.UnknownHostException: postgres

at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220)

at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)

at java.base/java.net.Socket.connect(Socket.java:609)

at org.postgresql.core.PGStream.createSocket(PGStream.java:238)

at org.postgresql.core.PGStream.<init>(PGStream.java:98)

at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:100)

at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:215)

... 70 more

Dockerfile for db:

FROM postgres:14.1
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=root
ENV POSTGRES_DB=internship_db

Dockerfile for web:

FROM tomcat:9
WORKDIR D:/internship_project
ADD target/internship_project.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]

docker-compose.yml:

version: "3"

services:

  tomcat:
    build: ../
    ports:
      - 8080:8080
    depends_on:
      - postgres

  postgres:
    build: ./db
    ports:
      - 5432:5432

I'm trying to connect to jdbc:postgresql://postgres:5432/internship_db. But also tried localhost:5432, 0.0.0.0:5432 and got:

Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

My postgresql.conf listen_address="*" My pg_hba.conf

TYPE  DATABASE        USER            CIDR-ADDRESS            METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            TRUE
# IPv6 local connections:
host    all             all             ::1/128                 TRUE

Any ideas what's the problem?

Alex
  • 33
  • 1
  • 6
  • This solution only works if the containers use the same virtual network. Make the network explicit if you're not sure if this is the case. Can you reach the DB in another way once it's up? Are there no errors on starting it? Also note, depends does NOT wait for the database to be up, it just means that the other container needs to be started before this one does. It could simply be trying to connect to a database which is not yet up and running. – JustLudo Jan 27 '22 at 10:23
  • Create a docker network and assign the same network to both docker services. – Sagar Vaghela Jan 27 '22 at 11:35
  • 1
    You should not need to create a network manually; [Compose creates a network named `default` and assigns it to containers by default](https://docs.docker.com/compose/networking/). The `depends_on: [postgres]` should (usually) solve the "unknown host" error (as distinct from a "connection refused" error, for example). Is the database exiting for some reason before the application can connect to it? If you separately run `docker-compose build` and `docker-compose up`, which one produces the error? – David Maze Jan 27 '22 at 11:54
  • Hope this answer helpful [https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects](https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects) – Sagar Vaghela Jan 27 '22 at 11:55
  • @DavidMaze Neither command produces an error. Can u tell me pls, what url should I use in connection to my db from my web app in container: localhost or name of service(in my case - postgres)? – Alex Jan 27 '22 at 12:20
  • @DavidMaze Thank you! Your answer helped me, the reason it didn't work was because I created the containers separately from each other. But with docker-compose up everything works – Alex Jan 27 '22 at 12:24

0 Answers0