简体   繁体   中英

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. The following endpoint works fine when started locally http://localhost:8080/central/products .

I'm on ubuntu 19.10 and running docker version 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/ . However, when I start more services with docker-compose I'm not able to reach this endpoint from my localhost.

Dockerfile for building the spring-boot application:

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:

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

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. I can also access the rabbitmq web interface and MongoDB via mongo client. The only thing that does not work is access to the rest endpoint via http://localhost:8080/central/products .

your Dockerfile is lacking the EXPOSE statement, therefore no ports are exposed to the outer world.

once you add EXPOSE 8080 to the bottom of your Dockerfile, your app will be reachable from outside the container.

The problem is caused by the application.properties file, where I still had server.address=localhost specified.

Removing this line solved the problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM