我有一个带有两个服务的docker-compose.yml:
version: '2'
services:
stuffer:
container_name: stuffer_container
build: .
ports:
- "8080:8080"
environment:
- spring.profiles.active=dev
depends_on:
- postgreDB
postgreDB:
container_name: postgreDB_container
image: "postgres:9.6.2"
ports:
- "5432:5432"
expose:
- "5432"
environment:
- POSTGRES_DB=stuffer
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root123
volumes:
- /home/iwaneez/postgreData:/var/lib/postgresql/data
我的应用有application-dev.properties:
## Datasource config
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://postgreDB:5432/stuffer
spring.datasource.username=postgres
spring.datasource.password=root123
当我使用
运行它们时docker-compose up
我得到连接拒绝例外:
stuffer_container | Caused by: org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
stuffer_container | at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:60) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container | at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:192) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
stuffer_container | at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runSchemaScripts(DataSourceInitializer.java:92) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
stuffer_container | at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.init(DataSourceInitializer.java:83) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
stuffer_container | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
stuffer_container | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
stuffer_container | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
stuffer_container | at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
stuffer_container | at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container | at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container | at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container | ... 61 common frames omitted
stuffer_container | Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
stuffer_container | at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container | at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:46) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container | ... 71 common frames omitted
stuffer_container | Caused by: org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
stuffer_container | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:138) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container | at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container | at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container | at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container | at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:32) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container | at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container | at org.postgresql.Driver.makeConnection(Driver.java:393) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container | at org.postgresql.Driver.connect(Driver.java:267) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container | at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container | at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container | at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:732) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container | at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:664) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container | at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:479) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container | at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container | at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container | at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container | at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container | at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container | at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container | ... 72 common frames omitted
stuffer_container | Caused by: java.net.ConnectException: Connection refused (Connection refused)
stuffer_container | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_131]
stuffer_container | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_131]
stuffer_container | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_131]
stuffer_container | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_131]
stuffer_container | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_131]
stuffer_container | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_131]
stuffer_container | at java.net.Socket.connect(Socket.java:538) ~[na:1.8.0_131]
stuffer_container | at java.net.Socket.<init>(Socket.java:434) ~[na:1.8.0_131]
stuffer_container | at java.net.Socket.<init>(Socket.java:211) ~[na:1.8.0_131]
stuffer_container | at org.postgresql.core.PGStream.<init>(PGStream.java:62) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:76) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container | ... 91 common frames omitted
如何将应用程序连接到在docker中运行的postgre?
我还尝试将ubuntu映像作为第三个服务运行,并且可以使用
成功连接到dbtelnet postgreDB 5432
所以看起来它是可见的,但应用仍然无法连接到它。
将数据源url属性从postgreDB更改为localhost,如
spring.datasource.url=jdbc:postgresql://localhost:5432/stuffer
让我将应用程序从docker外部连接到postgre,但我需要它们都在docker中运行。
答案 0 :(得分:1)
尝试将数据源设置为jdbc:postgresql://postgreDB:5432/stuffer
。 docker-compose根据提供的别名为每个容器分配主机名,或者如果未指定别名,则使用docker-compose.yml中指定的服务名称。这都在由docker主机管理的网络中。
答案 1 :(得分:1)
我设法通过运行
来解决问题docker-compose build
跑步前
docker-compose up
问题是有一个旧图像通过 localhost 指向postgre。并且我将配置中的主机更改为 postgreDB 它不是在 up 命令期间构建的,正如我从命令手册中所想到的那样。 因此,结果中没有反映出任何变化。
答案 2 :(得分:0)
如果您只使用docker运行postgresDB,假设您已转发端口,则主机操作系统上提供postgres,您可以使用localhost:5432
但是当你使用docker compose运行时,每个容器都是不同的,因此你无法从spring spring容器中访问postgresDB:5432
。在这种情况下,postgresDB在单独的容器中运行,您需要使用容器的IP地址访问它。
但是当您在服务之间配置了依赖关系时,docker compose为您解决了这个问题,docker compose将在您的容器中添加一个主机条目,这意味着您现在可以使用弹簧应用程序容器中的application-dev.properties
访问postgresDB。 / p>
查看您的localhost
配置看起来是正确的,但包含{{1}}的数据源网址在docker compose的情况下无效。