多容器 Docker 应用程序 - 容器之间的连接被拒绝

弗雷泽查普曼

所以我有一个多容器应用程序,前端和后端 API 都位于 API 网关后面。目前,当通过各自的命令(Java spring 应用程序和 Angular 前端)独立启动时,所有应用程序都可以正常工作。但是,当我通过 启动应用程序时docker-compose up,所有应用程序都无法相互通信(连接被拒绝)。

网关只是一个基本的 spring-cloud-gateway 启动应用程序,它将请求路由到正确的应用程序。这是使用以下代码配置的:

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("api", route -> route.path("/api/**").uri("http://localhost:5001"))
                .route("front-end", route -> route.path("/**").uri("http://localhost:4200"))
                .build();
    }

错误信息

向 发送 HTTP GET 请求后http://localhost:5000/api/categories,网关应用程序中产生了此错误消息。

api-gateway_1      | 2021-01-11 00:05:14.514 ERROR 1 --- [or-http-epoll-5] a.w.r.e.AbstractErrorWebExceptionHandler : [d29e1cbf-1]  500 Server Error for HTTP GET "/api/categories"
api-gateway_1      | 
api-gateway_1      | io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:5001
api-gateway_1      |    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
api-gateway_1      | Error has been observed at the following site(s):
api-gateway_1      |    |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
api-gateway_1      |    |_ checkpoint ⇢ HTTP GET "/api/categories" [ExceptionHandlingWebHandler]
api-gateway_1      | Stack trace:
api-gateway_1      | Caused by: java.net.ConnectException: finishConnect(..) failed: Connection refused
api-gateway_1      |    at io.netty.channel.unix.Errors.throwConnectException(Errors.java:124) ~[netty-transport-native-unix-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.unix.Socket.finishConnect(Socket.java:251) ~[netty-transport-native-unix-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.doFinishConnect(AbstractEpollChannel.java:673) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.finishConnect(AbstractEpollChannel.java:650) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.epollOutReady(AbstractEpollChannel.java:530) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:470) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1      |    at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]

尝试的步骤/更多信息

我尝试定义自己的网络,但这不会改变任何内容,因此我将容器保留在运行时创建的默认网络上docker-compose up

我可以通过cURL或通过 Postman独立点击每个应用程序,我可以通过浏览器访问前端。

Docker-Compose.yml

version: "3"

services:
  api:
    build: ./api
    ports:
      - "5001:5001"
  gateway:
    build: ./gateway
    ports:
      - "5000:5000"
  frontend:
    build: ./frontend
    ports:
      - "4200:80"

Dockerfiles

网关

FROM openjdk:11 as build

COPY . .

RUN ./gradlew build --parallel

FROM openjdk:11-jre-slim as runtime

COPY --from=build /build/libs/gateway-0.0.1-SNAPSHOT.jar /usr/app/

WORKDIR /usr/app

ENTRYPOINT ["java", "-jar", "gateway-0.0.1-SNAPSHOT.jar"]

应用程序接口

FROM openjdk:11 as build

COPY . .

RUN ./gradlew build --parallel

FROM openjdk:11-jre-slim as runtime

COPY --from=build /build/libs/api-0.0.1-SNAPSHOT.jar /usr/app/

WORKDIR /usr/app

ENTRYPOINT ["java", "-jar", "api-0.0.1-SNAPSHOT.jar"]

前端

FROM node:12.7-alpine AS build

WORKDIR usr/src/app

COPY package.json package-lock.json ./

RUN npm install

COPY . .

RUN npm run build

FROM nginx:1.17.1-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist/awards-frontend /usr/share/nginx/html
弗雷泽查普曼

感谢@DavidMaze让我注意到这一点。

看来我是有点傻了。我的应用程序正在向本地主机发送网络请求。当它们都在容器外运行时,这很好用。在容器中时,他们需要向其他容器的名称发送请求。例如:

app_1 在端口 8080 上运行 app_2 在端口 5000 上运行

在 docker 之外运行时,app_1 可以通过http://localhost:5000. 这在容器内不起作用,因为该容器中没有运行任何localhost:5000内容。相反,它将需要引用另一个容器,例如:http://app_2:5000

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法在Docker容器之间通信-连接被拒绝

无法从应用程序容器连接到PostgresDB Docker容器

如何在Docker Compose多容器应用程序中使用GoCQL连接到bitnami / cassandra?

Docker容器中的数据库容器和Java容器之间的Docker容器中的连接被拒绝

Docker容器内部的应用程序更新?

如何使用 Docker Compose 将子项目复制到多容器 Docker 应用程序中的容器?

Docker容器到容器的连接:连接被拒绝

在Docker Compose多容器应用程序中,如何防止Postgres运行先前发布的语句

在Docker Hub上共享多容器应用程序的最简单方法是什么?

使用Docker Compose运行同一多容器应用程序的副本

使用 Docker 定义和运行多容器应用程序时出错

在单个docker-compose文件中运行多个多容器应用程序

如何从Azure Pipeline中的Docker Compose任务中获取多容器应用程序的URL?

如何将Docker Web应用程序容器连接到Docker PostgreSQL容器?

运行 spring 应用程序的 Jetty Docker 容器无法连接到在 docker 容器外运行的 mysql

docker-compose:容器之间的Redis连接被拒绝

使用docker compose的容器之间的API请求拒绝连接

使用 Docker compose 时容器之间的连接被拒绝

在不同的Docker容器中运行的Spring Boot应用拒绝连接

如何在两个docker容器(mssql和.net核心应用)之间进行通信拒绝连接127.0.0.1:1433

对Docker容器应用程序使用Docker Nginx反向代理

如何使Docker容器与未经docker化的应用程序对话?

docker中的多容器连接

docker-compose应用程序容器无法连接到mongo容器

Docker 容器内的 ManageIQ Web 应用程序无法连接到容器外的 Hawkular

如何通过应用程序容器连接mysql容器的localhost:3306-docker

是否可以将非容器化的 Spring Boot 应用程序连接到 MongoDB Docker 容器?

如何将 docker 容器应用程序连接到非容器化数据库?

容器之间的连接被拒绝