0

I have a Postgres image running my database and I run this as a Docker container and I can connect to it from psql, but when I connect to it from my application server, I get the following error:

org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
cpo-platform-server-cpo-platform-1  |   at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:342)
cpo-platform-server-cpo-platform-1  |   at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:54)
cpo-platform-server-cpo-platform-1  |   at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:263)
cpo-platform-server-cpo-platform-1  |   at org.postgresql.Driver.makeConnection(Driver.java:443)
cpo-platform-server-cpo-platform-1  |   at org.postgresql.Driver.connect(Driver.java:297)
cpo-platform-server-cpo-platform-1  |   at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
cpo-platform-server-cpo-platform-1  |   at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
cpo-platform-server-cpo-platform-1  |   at doobie.util.transactor$Transactor$FromDriverManagerUnapplied.$anonfun$apply$19(transactor.scala:378)
cpo-platform-server-cpo-platform-1  |   at doobie.util.transactor$Transactor$FromDriverManagerUnapplied.$anonfun$create$2(transactor.scala:347)
cpo-platform-server-cpo-platform-1  |   at blocking @ doobie.util.transactor$Transactor$FromDriverManagerUnapplied.$anonfun$create$1(transactor.scala:347)
cpo-platform-server-cpo-platform-1  |   at fromAutoCloseable @ doobie.util.transactor$Transactor$FromDriverManagerUnapplied.$anonfun$create$1(transactor.scala:347)
cpo-platform-server-cpo-platform-1  |   at fromAutoCloseable @ doobie.util.transactor$Transactor$FromDriverManagerUnapplied.$anonfun$create$1(transactor.scala:347)
cpo-platform-server-cpo-platform-1  |   at use @ doobie.util.transactor$Transactor$$anon$4.apply(transactor.scala:164)

Is there anything that I have to watch out for when connecting from one Docker container where my application server is running to another Docker container where my database is running? I use the docker compose to start both of them like this:

services:
  my-app:
    image: me/my-app-server:1.1-SNAPSHOT
    ports:
      - 127.0.0.1:9000:9000
    environment:
      configEnv: test

  my-db:
    image: my-db-postgres-test-container
    ports:
      - 127.0.0.1:5432:5432

After starting, I can connect to the database using psql, but connecting via my application server fails. The credentials, database server etc., are all correctly configured, but still it won't connect.

Any ideas?

0

1 Answer 1

1

When you have two containers started, exposed ports for the host are not exposed across containers.

Use the hostname of the postgres container, my-db in your case, instead of localhost, to connect from my-app.

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

2 Comments

Yes, that was it. Thanks!
You're welcome. This is one of the big benefits of docker - your application and other containers are networked together "naturally" without fear of port conflicts between them. (docker-compose puts all its containers in a single docker network by default)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.