docker-composer: access to web service startup in advance postgres db service

advertisements

In my docker-compose.yal file I define a spring-boot based web service and a postgresql database service. The web services references the db services and has an environment variable for the database datasource URL where it needs to reference the hostname of the database service. This is what it looks like:

version: '3'
services:

  web:
    container_name: medmap-server
    hostname: medmap-server
    build: ../../../build/docker
    ports: ["8090:8090"]
    links:
      - db:database
    environment:
      - spring.datasource.url=jdbc:postgresql://medmap-db:5433/medmapdb
      - spring.datasource.username=docker
      - spring.datasource.password=docker
      - logging.level.org.hibernate=DEBUG

  db:
    container_name: medmap-db
    hostname: medmap-db
    image: postgres:9.6.2
    ports:
      - "5433:5433"
    volumes:
      - /var/lib/medmap/data
    environment:
      - POSTGRES_PASSWORD=docker
      - POSTGRES_USER=docker
      - POSTGRES_DB=medmapdb
      - PGDATA=/var/lib/medmap/data/pgdata

When I start this application using docker-compose up I get the following error:

medmap-server | Caused by: org.postgresql.util.PSQLException: Connection to medmap-db:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
.
medmap-server |         at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:239) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server |         at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server |         at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:127) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server |         at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server |         at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server |         at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:41) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server |         at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server |         at org.postgresql.Driver.makeConnection(Driver.java:414) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server |         at org.postgresql.Driver.connect(Driver.java:282) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server |         at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server |         at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server |         at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:732) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server |         at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:664) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server |         at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:479) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server |         at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server |         at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server |         at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server |         at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server |         at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.0.12.Final.jar!/:
5.0.12.Final]
medmap-server |         at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5
.0.12.Final.jar!/:5.0.12.Final]
medmap-server |         at org.hibernate.tool.schema.extract.internal.ExtractionContextImpl.getJdbcConnection(ExtractionContextImpl.java:62) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
medmap-server |         ... 38 common frames omitted
medmap-server | Caused by: java.net.ConnectException: Connection refused (Connection refused)
medmap-server |         at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_121]
medmap-server |         at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_121]
medmap-server |         at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_121]
medmap-server |         at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_121]
medmap-server |         at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_121]
medmap-server |         at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_121]
medmap-server |         at org.postgresql.core.PGStream.<init>(PGStream.java:61) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server |         at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:121) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server |         ... 58 common frames omitted
medmap-server |
medmap-server exited with code 1

What I do not know is how the pg_hba.conf file is created to allow the web service in one container to access the db service in another container. TIA for any help on this.


The default port for postgres is 5432, try defining it instead of 5433

port (integer) The TCP port the server listens on; 5432 by default. Note that the same port number is used for all IP addresses the server listens on. This parameter can only be set at server start.