简体   繁体   English

Docker 容器无法使用容器名称访问另一个容器

[英]Docker container can't reach another container using container name

I have 2 Docker containers running in the same network and I want 1 of them to call another via spring Webclient.我有 2 个 Docker 容器在同一网络中运行,我希望其中 1 个通过 spring Webclient 调用另一个。 I'm sure they all are in the same network -> docker network inspect <network_ID> proves this.我确定它们都在同一个网络中 -> docker network inspect <network_ID>证明了这一点。 AFAIK I can ping one container from another to check if they can talk to each other by docker exec -ti attachment-loader-prim ping attachment-loader-sec If I run this - I see responses from attachment-loader-sec like 64 bytes from 172.21.0.5: seq=0 ttl=64 time=0.220 ms , which means they can communicate. AFAIK 我可以从另一个容器 ping 一个容器以检查它们是否可以通过docker exec -ti attachment-loader-prim ping attachment-loader-sec相互交谈如果我运行这个 - 我看到来自attachment-loader-sec 的响应,如64 bytes from 172.21.0.5: seq=0 ttl=64 time=0.220 ms ,这意味着他们可以通信。 When I send Postman request to attachment-loader-prim by its exposed port localhost:8085 , I expect that after some business logic it calls for attachment-loader-sec via Webclient, but on that step I get a 500 error with such a message:当我通过其暴露的端口localhost:8085向attachment-loader-prim 发送邮递员请求时,我希望在一些业务逻辑之后它通过Webclient 调用attachment-loader-sec,但在这一步我收到这样一条消息的500 错误:

"finishConnect(..) failed: Connection refused: attachment-loader-sec/172.21.0.5:80; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: attachment-loader-sec/172.21.0.5:80" “finishConnect(..) 失败:连接被拒绝:attachment-loader-sec/172.21.0.5:80;嵌套异常是 io.netty.channel.AbstractChannel$AnnotatedConnectException:finishConnect(..) 失败:连接被拒绝:attachment-loader-秒/172.21.0.5:80"

Both attachment-loader-prim and attachment-loader-sec can be accessed separately via postman and both send a response, no problem. attachment-loader-prim 和attachment-loader-sec 都可以通过邮递员单独访问并且都发送响应,没问题。 This is my docker-compose:这是我的 docker-compose:

    version: '3'
services:
  attachment-loader-prim:
    container_name: attachment-loader-prim
    build:
      context: ""
    restart: always
    image: attachment-loader:latest
    environment:
      SERVER_PORT: 8085
    networks:
      - loader_network
    expose:
      - 8085
    ports:
      - 8005:8005
      - 8085:8085

  attachment-loader-sec:
    container_name: attachment-loader-sec
    build:
      context: ""
    restart: always
    image: attachment-loader:latest
    environment:
      SERVER_PORT: 8086
    networks:
      - loader_network
    expose:
      - 8086
    ports:
      - 8006:8005
      - 8086:8086

networks:
  loader_network:
    driver: bridge

And this is a Webclient which makes a call:这是一个调用的 Web 客户端:

    class RemoteServiceCaller(private val fetcherWebClientBuilder: WebClient.Builder) {

suspend fun getAttachmentsFromRemote(id: String, params: List<Param>, username: String): Result? {

    val client = fetcherWebClientBuilder.build()

    val awaitExchange = client.post()
            .uri("/{id}/attachment", id)
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(params)
            .header(usernameHeader, username)
            .accept(MediaType.APPLICATION_OCTET_STREAM)
            .awaitExchange {
                if (it.statusCode().is2xxSuccessful) {
                    handleSucessCode(it)
                } else it.createExceptionAndAwait().run {
                    LOG.error(this.responseBodyAsString, this)
                    throw ProcessingException(this)
                }
            }

    return awaitExchange
}

private suspend fun handleSucessCode(response: ClientResponse) {
// some not important logic
}
}

PS BasicUri for Webclient defined as Config Bean like http://attachment-loader-sec/list用于 Webclient 的 PS BasicUri 定义为 Config Bean,如http://attachment-loader-sec/list

All my investigations brought me to such problems as:我所有的调查都给我带来了以下问题:

  • Calling container using localhost instead of container name使用localhost而不是容器名称调用容器
  • Containers are not in the same network.容器不在同一个网络中。

All that seems not relevant for me.所有这些似乎与我无关。

Any ideas will be really appreciated.任何想法将不胜感激。

The problem was in calling a service without its port.问题在于调用没有端口的服务。 The url became now http://attachment-loader-sec:8086/list and it is correct now.网址现在变成了http://attachment-loader-sec:8086/list ,现在是正确的。 In my case I get 404, which means that my url path is not quite correct, but that is outside of current question就我而言,我得到 404,这意味着我的 url 路径不太正确,但这不在当前问题范围内

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

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