docker-compose:我的容器相互连接,但不连接主机

时间:2019-11-08 16:00:08

标签: mongodb spring-boot docker docker-compose dockerfile

我在docker-compose管理的两个容器中有一个spring-boot应用程序和mongodb。该应用程序似乎可以很好地连接到mongodb,但外部(即我的本地计算机)都无法访问它们。我想我需要一些专家建议,在这里...

1)安装基础:

  • Docker版本19.03.4,内部版本9013bf583a

  • docker-compose版本1.24.1,内部版本4667896b

2)docker-compose.yml:

version: "3.7"

services:
   app:
      build: .
      restart: unless-stopped
      container_name: project-service
      ports:
         - target:  8080
           published: 8080
      networks:
         - project-net
      working_dir: /app
      depends_on:
         - db

   db:
      image: mongo
      container_name: project-db
      volumes:
         - type: volume
           source: project-data
           target: /data/db
           volume:
             nocopy: true
      restart: unless-stopped
      ports:
         - target: 27017
           published: 27017
      networks:
         - project-net

networks:
   project-net:

volumes:
   project-data:

3)应用Dockerfile:

FROM maven:latest AS service
WORKDIR /src/fscl/project-service
COPY . .
RUN mvn -B -f pom.xml dependency:resolve
RUN mvn -B package -DskipTests

FROM openjdk:8-jdk-alpine AS runtime
RUN apk update && apk add bash
RUN mkdir -p /app
WORKDIR /app
COPY --from=service /src/fscl/project-service/target/project-service-0.2.0.jar .
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/project-service-0.2.0.jar"]
运行时

4)状态:

$ docker-compose up&

相关日志:

project-service | 2019-11-08 15:33:09.688  INFO 1 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[project-db:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
project-db | 2019-11-08T15:33:09.755+0000 I  NETWORK  [listener] connection accepted from 172.20.0.3:41964 #1 (1 connection now open)
project-db | 2019-11-08T15:33:09.771+0000 I  NETWORK  [conn1] received client metadata from 172.20.0.3:41964 conn1: { driver: { name: "mongo-java-driver|legacy", version: "3.11.1" }, os: { type: "Linux", name: "Linux", architecture: "amd64", version: "4.4.0-124-generic" }, platform: "Java/IcedTea/1.8.0_212-b04" }
project-service | 2019-11-08 15:33:09.809  INFO 1 --- [roject-db:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:1}] to project-db:27017
project-service | 2019-11-08 15:33:11.305  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
project-service | 2019-11-08 15:33:11.310  INFO 1 --- [           main] f.p.ProjectServiceApplication            : Started ProjectServiceApplication in 5.538 seconds (JVM running for 6.383)
$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                      NAMES
aecb3d4bb468        project-service_app   "java -jar /app/proj…"   About an hour ago   Up 2 minutes        0.0.0.0:8080->8080/tcp     project-service
257985d48264        mongo                 "docker-entrypoint.s…"   3 hours ago         Up 2 minutes        0.0.0.0:27017->27017/tcp   project-db

我认为一切顺利,但是没有:

5)问题:

 curl localhost:8080/api/v4/projects

应该返回一个JSON对象,但不是。该应用程序也未显示任何日志记录反应。没有任何东西可以进入我的服务应用程序。

$  mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017

永远吊死

$  mongo project-db:27017
MongoDB shell version v3.4.10
connecting to: project-db:27017
2019-11-08T16:42:26.869+0100 I NETWORK  [thread1] getaddrinfo("project-db") failed: Name or service not known
2019-11-08T16:42:26.869+0100 E QUERY    [thread1] Error: couldn't initialize connection to host project-db, address is invalid :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed

$ mongo localhost:27017
MongoDB shell version v3.4.10
connecting to: localhost:27017

也挂起。

我相信我已经遵循了所有示例,但是我到底需要做些什么?

0 个答案:

没有答案