简体   繁体   English

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

[英]Multi Container Docker app - Connection Refused between containers

So I've got a multi-container application with a front-end, back-end API all sat behind an API gateway.所以我有一个多容器应用程序,前端、后端 API 都位于 API 网关后面。 At the moment all apps work fine when booted independently through their respective commands (Java spring apps and an Angular front-end).目前,通过各自的命令(Java spring 应用程序和 Angular 前端)独立启动时,所有应用程序都可以正常工作。 However, when I start the apps through docker-compose up , none of the apps can communicate with each other (getting a connection refused).但是,当我通过docker-compose up启动应用程序时,没有一个应用程序可以相互通信(连接被拒绝)。

The Gateway is just a basic spring-cloud-gateway starter app which routes requests through to the correct application. Gateway 只是一个基本的 spring-cloud-gateway starter 应用程序,它将请求路由到正确的应用程序。 This is configured with the following code:这是使用以下代码配置的:

    @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();
    }

Error message错误信息

After sending a HTTP GET request to http://localhost:5000/api/categories , this error message was produced in the gateway app.在向http://localhost:5000/api/categories发送 HTTP GET 请求后,网关应用程序中产生了此错误消息。

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]

Attempted steps/more info尝试的步骤/更多信息

I have tried defining my own network but this does not change anything so I have left the containers on the default network created upon running docker-compose up .我尝试定义自己的网络,但这并没有改变任何东西,所以我将容器留在了运行docker-compose up时创建的默认网络上。

I can hit each app independently via cURL or through Postman and I can access the front-end through the browser.我可以通过cURL或 Postman 独立访问每个应用程序,并且可以通过浏览器访问前端。

Docker-Compose.yml 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 Dockerfiles

Gateway网关

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"]

API API

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"]

Front-end前端

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

Thanks to @DavidMaze for making me notice this.感谢@DavidMaze让我注意到这一点。

Seems like I was being a bit of a fool.好像我有点傻。 My apps are sending network requests to localhost.我的应用程序正在向 localhost 发送网络请求。 This works fine when they are all running outside of a container.当它们都在容器外运行时,这可以正常工作。 When in a container they need to be sending requests to the names of the other containers.在容器中时,他们需要向其他容器的名称发送请求。 For example:例如:

app_1 runs on port 8080 app_2 runs on port 5000 app_1 在 8080 端口上运行 app_2 在 5000 端口上运行

When running outside of docker, app_1 could send a network request to app_2 via http://localhost:5000 .在 docker 之外运行时,app_1 可以通过http://localhost:5000向 app_2 发送网络请求。 This does not work inside of a container as nothing is running on localhost:5000 in that container.这在容器内部不起作用,因为该容器中的localhost:5000上没有运行任何内容。 Instead, it will need to reference the other container eg: http://app_2:5000 .相反,它需要引用其他容器,例如: http://app_2:5000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在Docker容器之间进行连接时连接被拒绝(连接被拒绝) - Connection refused (Connection refused) while trying to connect between docker containers Docker容器中数据库容器和docker中的java容器之间的连接被拒绝 - Connection refused in Docker container between database container and java container in docker 无法在 docker 容器之间进行通信 - 连接被拒绝 - cannot communicate between docker container - connection refused 使用Java客户端启动Docker容器时拒绝连接 - Connection refused while launching docker containers using java client apapped和tomcat docker容器之间的连接问题 - connection issues between apacheds and tomcat docker containers docker-compose:容器之间没有连接 - docker-compose: no connection between containers 连接拒绝从 docker 连接到 Elasticsearch docker 容器 - Connection Refused connecting from docker to Elasticsearch docker container Spring App Connection Refused in Docker 组成 - Spring App Connection Refused in Docker Composition Rabbitmq连接被拒绝,在Linux服务器上的docker容器中运行 - Rabbitmq connection refused , runs inside docker container on a linux server 连接到 jmeter 从站 docker 容器时出现连接被拒绝错误 - Connection refused error while connecting to jmeter slave docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM