无法将dockerized spring boot应用程序连接到dockerized postgre sql
我有一个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
当我运行他们都使用
码头工人组成
我得到连接拒绝异常:
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的图像作为第三个服务,并可以成功连接到数据库使用
telnet postgreDB 5432
所以它看起来像是可见的,但应用程序仍然无法连接到它。
将数据源url属性从postgreDB更改为localhost
spring.datasource.url=jdbc:postgresql://localhost:5432/stuffer
让我从Docker外部连接应用程序到postgre,但是我需要它们都在docker中运行。
尝试将您的数据源设置为jdbc:postgresql://postgreDB:5432/stuffer
。 docker-compose根据提供的别名为每个容器分配一个主机名,或者如果没有指定别名,则使用在docker-compose.yml中分配的服务名称。 这全部在码头主机管理的网络内。
我设法通过运行来解决问题
docker-compose build
在运行之前
docker-compose up
问题是有一个老的图像通过本地主机指向postgre。 尽管我将配置中的主机改为postgreDB,但是在命令执行期间并没有建立,正如我从命令手册中想到的那样。 所以没有变化反映在结果。
当您只使用docker运行postgresDB时,假设您已经转发了端口,postgres在您的主机操作系统上可用,您可以使用localhost:5432
访问该服务
但是当你使用docker compose运行时,每个容器都是不同的,因此你不能从你的spring应用程序容器中访问localhost:5432
。 在这种情况下,postgresDB运行在单独的容器中,您需要使用容器的IP地址访问它。
但是docker compose在你配置服务之间的依赖关系的时候解决了这个问题,docker compose会在你的容器中添加一个host项,这意味着你现在可以使用postgresDB:5432
从你的spring应用容器中访问postgresDB了。
看看你的application-dev.properties
配置看起来是正确的,但是包含localhost
的数据源url在docker撰写的情况下将不起作用。