docker-compose:spring-boot web服务访问postgres db服务

时间:2017-04-22 18:12:23

标签: postgresql spring-boot docker-compose

在我的docker-compose.yal文件中,我定义了一个基于spring-boot的web服务和一个postgresql数据库服务。 Web服务引用db服务,并具有数据库数据源URL的环境变量,它需要引用数据库服务的主机名。这就是它的样子:

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

当我使用docker-compose up启动此应用程序时,出现以下错误:

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

我不知道是如何创建pg_hba.conf文件以允许一个容器中的Web服务访问另一个容器中的db服务。 TIA对此有任何帮助。

2 个答案:

答案 0 :(得分:2)

default port for postgres is 5432,尝试定义它而不是5433

  

port(整数)服务器侦听的TCP端口;默认为5432。   请注意,相同的端口号用于服务器的所有IP地址   听着。此参数只能在服务器启动时设置。

答案 1 :(得分:0)

感谢@ vao-tsun建议我查看postgres端口。

我通过使用以下命令在db服务的conatiner上查看bash shell来查看我的数据库服务中postgres使用的端口:

docker exec -i -t medmap-db /bin/bash

然后在执行以下psql命令的bash shell中执行:

psql -U postgres -c "SELECT * FROM pg_settings WHERE name = 'port';"

这表明我使用的是端口5432而不是5423,这是我的网络服务在spring.datasource.url env变量指定的IDBC网址中使用的。

要解决此问题,我需要在我的数据库服务中指定PGPORT = 5433 env变量。一个常见的错误是假设docker-compose中的ports字段告诉服务应该使用哪个端口。端口字段只声明服务使用的端口。您需要以特定于服务的方式为您的服务配置端口(对于postgres,它是env变量PGPORT)。

以下是我修改过的docker-compose.yml文件:

version: '3'
services:

  web:
    container_name: medmap-server
    hostname: medmap-server
    build: ../../../build/docker
    ports: ["8090:8090"]
    links:
      - db:database
    environment:
      - server.port=8090
      - spring.datasource.url=jdbc:postgresql://medmap-db:5433/medmapdb
      - spring.datasource.username=postgres
      - spring.datasource.password=postgres      
#      - 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=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=medmapdb
      - PGDATA=/var/lib/medmap/data/pgdata
      - PGPORT=5433