[英]Can't access spring-boot rest-endpoint inside docker container when using docker-compose
I'm setting up spring-boot applications inside docker with docker-compose and need to access a rest endpoint from one application on port 8080 from my localhost.我正在使用 docker-compose 在 docker 中设置 spring-boot 应用程序,并且需要从本地主机的端口 8080 上的一个应用程序访问其余端点。 The following endpoint works fine when started locally http://localhost:8080/central/products
.在本地启动时,以下端点工作正常http://localhost:8080/central/products
。
I'm on ubuntu 19.10 and running docker version 18.09.5.我在 ubuntu 19.10 上运行 docker 版本 18.09.5。 When I set up a simple spring-boot application for docker like explained on https://spring.io/guides/gs/spring-boot-docker/ , everything works as expected and I can reach the endpoint on http://localhost:8080/
.当我为 docker 设置一个简单的 spring-boot 应用程序时,如https://spring.io/guides/gs/spring-boot-docker/ 上所述,一切都按预期工作,我可以到达http://localhost:8080/
上的端点http://localhost:8080/
。 However, when I start more services with docker-compose I'm not able to reach this endpoint from my localhost.但是,当我使用 docker-compose 启动更多服务时,我无法从本地主机访问此端点。
Dockerfile for building the spring-boot application:用于构建 spring-boot 应用程序的 Dockerfile:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","sample.EfridgeCentralApplication"]
docker-compose.yml file that seems to cause the problem: docker-compose.yml 文件似乎导致问题:
version: '3.7'
services:
central-london:
image: demo/efridge-central:latest
container_name: central-london
ports:
- 8080:8080
environment:
- SERVER_PORT=8080
- SPRING_PROFILES_ACTIVE=dev
- SPRING_DATA_MONGODB_HOST=mongo-central
- SPRING_DATA_MONGODB_PORT=27017
- APP_RABBIT_HOSTNAME=rabbit-efridge
factory-usa:
image: demo/efridge-factory:latest
container_name: factory-usa
ports:
- 8081:8081
environment:
- SERVER_PORT=8081
- SPRING_PROFILES_ACTIVE=usa
- SPRING_DATA_MONGODB_HOST=mongo-usa
- SPRING_DATA_MONGODB_PORT=27017
- APP_RABBIT_HOSTNAME=rabbit-efridge
factory-china:
image: demo/efridge-factory:latest
container_name: factory-china
ports:
- 8082:8082
environment:
- SERVER_PORT=8082
- SPRING_PROFILES_ACTIVE=china
- SPRING_DATA_MONGODB_HOST=mongo-china
- SPRING_DATA_MONGODB_PORT=27017
- APP_RABBIT_HOSTNAME=rabbit-efridge
mongo-central:
image: mongo:latest
container_name: mongo-central
hostname: mongo-central
ports:
- 27017:27017
mongo-usa:
image: mongo:latest
container_name: mongo-usa
hostname: mongo-usa
ports:
- 27018:27017
mongo-china:
image: mongo:latest
container_name: mongo-china
hostname: mongo-china
ports:
- 27019:27017
rabbit-efridge:
image: rabbitmq:3-management
container_name: rabbit-efridge
hostname: rabbit-efridge
ports:
- 15672:15672
- 5672:5672
Output from docker inspect:码头工人检查的输出:
"NetworkSettings": {
"Bridge": "",
"SandboxID": "b91760f810a656e382d702dd408afe3c5ffcdf4c0cd15ea8550150867ac038cc",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"8080/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
]
}
Logs from spring-boot来自 spring-boot 的日志
2019-07-03 11:54:57.654 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-07-03 11:54:57.803 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-07-03 11:54:57.804 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-07-03 11:54:58.149 INFO 1 --- [ main] o.a.c.c.C.[.[localhost].[/central] : Initializing Spring embedded WebApplicationContext
2019-07-03 11:54:58.150 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 6950 ms
2019-07-03 11:54:59.810 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[mongo-central:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-07-03 11:54:59.810 INFO 1 --- [ main] org.mongodb.driver.cluster : Adding discovered server mongo-central:27017 to client view of cluster
2019-07-03 11:55:00.256 INFO 1 --- [o-central:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:11}] to mongo-central:27017
The output from docker inspect
of the working spring-boot container and the not working looks almost the same. docker inspect
工作的 spring-boot 容器和不工作的容器的输出看起来几乎相同。 I can also access the rabbitmq web interface and MongoDB via mongo client.我还可以通过 mongo 客户端访问 rabbitmq Web 界面和 MongoDB。 The only thing that does not work is access to the rest endpoint via http://localhost:8080/central/products
.唯一不起作用的是通过http://localhost:8080/central/products
访问其余端点。
your Dockerfile is lacking the EXPOSE
statement, therefore no ports are exposed to the outer world.您的 Dockerfile 缺少EXPOSE
语句,因此没有端口暴露给外部世界。
once you add EXPOSE 8080
to the bottom of your Dockerfile, your app will be reachable from outside the container.一旦您将EXPOSE 8080
添加到EXPOSE 8080
的底部,您的应用程序将可以从容器外部访问。
The problem is caused by the application.properties
file, where I still had server.address=localhost
specified.问题是由application.properties
文件引起的,我仍然在其中指定了server.address=localhost
。
Removing this line solved the problem!删除此行解决了问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.