dockerfile:
FROM anapsix/alpine-java:8_server-jre_unlimited
# copy application
COPY server.jar /opt/test/
COPY application.yml /opt/test/
# expose server ports
EXPOSE 8080 18080
# Run via dumb-init
WORKDIR /opt/test
ENTRYPOINT ["java"]
CMD ["-jar", "server.jar"]
docker-compose文件:
services:
backend-server:
image: test.com/server:latest
build: .
depends_on:
- database-server
ports:
- "127.0.0.1:8080:8080"
- "127.0.0.1:18080:18080"
database-server:
image: postgres:9.6
ports:
- "127.0.0.1:5432:5432"
environment:
- POSTGRES_PASSWORD testtest
应用程序属性文件:
spring:
datasource:
# use default user/database created by PostgreSQL Docker image upon startup
url: jdbc:postgresql://localhost/postgres
username: postgres
password: testtest
driver-class-name: org.postgresql.Driver
当我运行docker-compose时,它将创建一个数据库容器和一个连接到数据库容器的应用程序容器。
但是spring应用程序容器在启动过程中遇到一些问题,并显示错误:
org.postgresql.util.PSQLException:与本地主机的连接:5432被拒绝。检查主机名和端口是否正确以及邮局主管正在接受TCP / IP连接。
docker container ls
显示数据库正在运行,如果直接运行spring应用程序,则可以访问它。
a295bfd16e2c postgres:9.6 "docker-entrypoint.s…" About a minute ago Up About a minute 127.0.0.1:5432->5432/tcp server_database-server_1
那我搞砸了会是什么问题?
我找到的解决方案
services:
backend-server:
image: test.com/server:latest
build: .
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://database-server/postgres
depends_on:
- database-server
ports:
- 8080:8080
- 18080:18080
database-server:
image: postgres:9.6
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD testtest
但是我仍然无法从计算机的localhost:8080
访问应用程序,即使容器的日志显示为
2018-10-31 15:02:40.881信息1 --- [主要] o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcat在端口上启动: 8080(http),上下文路径为“
2018-10-31 15:02:40.881信息1 --- [主要] com.test.server.Application:在中启动的应用程序 12.823秒(JVM运行13.235)
答案 0 :(得分:0)
我建议按如下所示修改docker-compose.yml文件并进行测试
services:
backend-server:
image: test.com/server:latest
build: .
depends_on:
- database-server
ports:
- 8080:8080
- 18080:18080
database-server:
image: postgres:9.6
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD testtest
答案 1 :(得分:0)
services:
backend-server:
image: test.com/server:latest
build: .
depends_on:
- database-server
ports:
- "127.0.0.1:8080:8080" #change to 8080:8080 without ""and by default set to local so you dont need add local ip
- "127.0.0.1:18080:18080" #change to 18080:18080 without ""and by default set to local so you dont need add local ip
database-server:
image: postgres:9.6
ports:
- "127.0.0.1:5432:5432" #change to 5432:5432 without ""and by default set to local so you dont need add local ip
environment:
- POSTGRES_PASSWORD testtest