Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to connect to Postgres Docker Conatiner From Application Server

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading