Docker:Springboot容器无法连接到PostgreSql容器连接错误 [英] Docker: Springboot container can not connect to PostgreSql Container Connection error
问题描述
我正在构建我的第一个Springboot 2.0应用程序。我试图将我的Springboot应用程序放入一个docker容器中,并将PostgresDB放入另一个容器。
I am building my first Springboot 2.0 application. I am trying to put my Springboot application into one docker container and my PostgresDB into another container.
我的Dockerfile
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD springboot-api-demo-0.1*.jar app.jar
RUN sh -c 'touch /app.jar'
EXPOSE 9443
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/urandom -jar /app.jar" ]
我的docker-compose.yml文件
version: "2.1"
services:
springboot-api-demo:
image: "fw/springboot-api-demo"
mem_limit: 1024m
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=local
- AWS_REGION=local
- ENVIRONMENT=local
- AUTH_ENABLED=false
postgres:
container_name: pgdb
image: postgres:9.6-alpine
environment:
- 'POSTGRES_ROOT_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
ports:
- "54321:5432"
I我在 application.properties
spring.datasource.url= jdbc:postgresql://localhost:54321/java_learning
spring.datasource.username=postgres
spring.datasource.password=postgres
我可以测试两个图像是否都向上。同样从docker日志和docker事件中,我看到postgres Container运行良好,即使我可以访问它,也创建了一个数据库。
但是springboot容器启动了,但是我死了,因为它无法连接到postgress并在下面抛出错误。
I can test that Both of the Images are up. Also from docker log and docker events, I see that postgres Container is running fine, even I can access it and also created a DB too. But springboot container started but i died because it could not connect to postgress and throwing error below.
无法从数据库获取连接:连接尝试
失败
Unable to obtain connection from database: The connection attempt failed
请注意,我的主机已经在端口5432上安装了Postgres,这就是为什么我在postgres容器上进行了端口映射ofr 54321:5432的原因。这是证明:)-
Note that my host machine already has Postgres on port 5432 thats why I did a port mapping ofr 54321:5432 on my postgres container. Here is Proof :) -
➜ springboot-api-demo git:(master) ✗ lsof -i:54321
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 44345 shailendra.singh 18u IPv4 0xf62897fbdd69e31d 0t0 TCP *:54321 (LISTEN)
com.docke 44345 shailendra.singh 21u IPv6 0xf62897fbdd119975 0t0 TCP localhost:54321 (LISTEN)
➜ springboot-api-demo git:(master) ✗ lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 715 shailendra.singh 5u IPv6 0xf62897fbb43e03b5 0t0 TCP localhost:postgresql (LISTEN)
postgres 715 shailendra.singh 6u IPv4 0xf62897fbbaeea9bd 0t0 TCP localhost:postgresql (LISTEN)
我不确定是什么问题。但是我的Springboot应用程序无法连接我的postgres容器,该容器运行良好且具有适当的凭据。
I am not sure what is the problem. But my Springboot application is not able to connect my postgres container which is running fine with proper creadentials.
推荐答案
尝试使用:
spring.datasource.url= jdbc:postgresql://pgdb:5432/java_learning
postgres数据库未在localhost上运行,而是在具有其他IP(但未知)的另一个容器中运行。
The postgres database is not running on localhost, it's running in the other container which has an other IP (yet unknown).
感谢docker-compose会自动在docker-compose.yml中的所有容器之间创建一个共享的网络(除非明确表示不这样做),因此您可以神奇地使用服务名作为主机名。
Thanksfully, docker-compose automatically create a network shared among all the containers in the docker-compose.yml (unless explicitly said to do not), as a result you can magically use the service name as an hostname.
此外,您的端口中有一个错字,Postgres使用 5432
默认,而不是 54321
Also, you have a typo in the port, Postgres use 5432
by default, not 54321
这篇关于Docker:Springboot容器无法连接到PostgreSql容器连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!