从同一个 docker compose 中的 docker 容器拒绝连接

Posted

技术标签:

【中文标题】从同一个 docker compose 中的 docker 容器拒绝连接【英文标题】:getting connection refused from docker containers in a same docker compose 【发布时间】:2021-04-24 22:33:24 【问题描述】:

我有一个 akka http 应用程序和一个 keycloak docker container 我使用 sbt native docker plugin 创建了我的应用程序的映像,然后我编写了一个 docker compose 文件,但我的应用程序容器没有发现 keycloak 容器

这是我的 build.sbt 文件

enablePlugins(DockerPlugin)
enablePlugins(JavaAppPackaging)
dockerExposedPorts := Seq(8083)

然后我为我的项目创建了一个 docker 映像

>docker:publishLocal

然后我创建了一个 docker-compose 文件

版本:'3.3'

services:
  keycloak:
   image: jboss/keycloak
   container_name: docker-keycloak-container
   ports:
     - "8080:8080"
   environment:
     - KEYCLOAK_USER=admin
     - KEYCLOAK_PASSWORD=admin  

  akkahttpservice:
   image: myproject-auth:0.0.1
   container_name: docker-myproject-auth-container
   ports:
     - "8083:8083"
   depends_on:
      - keycloak
   

docker ps 展示

CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                              NAMES
181522f78d22        myproject-auth:0.0.1   "/opt/docker/bin/int…"   45 seconds ago      Up 40 seconds       0.0.0.0:8083->8083/tcp             docker-myproject-auth-container
d92d9c4f0a19        jboss/keycloak         "/opt/jboss/tools/do…"   5 days ago          Up 46 seconds       0.0.0.0:8080->8080/tcp, 8443/tcp   docker-keycloak-container

但是当我点击与 keycloak 对话的应用程序路由时,我得到了

14:13:32.020 [scala-execution-context-global-45] ERROR com.ifkaar.lufz.authentication.actors.worker.TokenManagerActor - Actor TokenManager: exception in fetching token
docker-myproject-auth-container | akka.stream.StreamTcpException: Tcp command [Connect(0.0.0.0:8080,None,List(),Some(10 seconds),true)] failed because of java.net.ConnectException: Connection refused
docker-myproject-auth-container | Caused by: java.net.ConnectException: Connection refused
docker-myproject-auth-container |   at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
docker-myproject-auth-container |   at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:714)
docker-myproject-auth-container |   at akka.io.TcpOutgoingConnection$$anonfun$connecting$1.$anonfun$applyOrElse$4(TcpOutgoingConnection.scala:111)
docker-myproject-auth-container |   at akka.io.TcpOutgoingConnection.akka$io$TcpOutgoingConnection$$reportConnectFailure(TcpOutgoingConnection.scala:53)
docker-myproject-auth-container |   at akka.io.TcpOutgoingConnection$$anonfun$connecting$1.applyOrElse(TcpOutgoingConnection.scala:111)
docker-myproject-auth-container |   at akka.actor.Actor.aroundReceive(Actor.scala:537)
docker-myproject-auth-container |   at akka.actor.Actor.aroundReceive$(Actor.scala:535)
docker-myproject-auth-container |   at akka.io.TcpConnection.aroundReceive(TcpConnection.scala:33)
docker-myproject-auth-container |   at akka.actor.ActorCell.receiveMessage(ActorCell.scala:577)
docker-myproject-auth-container |   at akka.actor.ActorCell.invoke(ActorCell.scala:547)
docker-myproject-auth-container |   at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:270)
docker-myproject-auth-container |   at akka.dispatch.Mailbox.run(Mailbox.scala:231)
docker-myproject-auth-container |   at akka.dispatch.Mailbox.exec(Mailbox.scala:243)
docker-myproject-auth-container |   at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
docker-myproject-auth-container |   at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
docker-myproject-auth-container |   at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
docker-myproject-auth-container |   at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

但是当我从 sbt 运行我的应用程序并使用 docker keycloak 容器时一切正常,为什么会这样?

我也在 http://0.0.0.0:8083 上运行我的 akka http 项目

请在这方面指导我

【问题讨论】:

您需要使用目标容器的名称(或ip地址)。即,您的应用程序应该连接到http://keycloak:8080 【参考方案1】:

您需要通过环境变量将 keycloak 的地址注入您的服务。为此,您可以使用服务名称作为密钥斗篷地址 (Compose docs)

services:
  keycloak:
   image: jboss/keycloak
   container_name: docker-keycloak-container
   ports:
     - "8080:8080"
   environment:
     - KEYCLOAK_USER=admin
     - KEYCLOAK_PASSWORD=admin  

  akkahttpservice:
   image: myproject-auth:0.0.1
   container_name: docker-myproject-auth-container
   environment:
     - KEYCLOAK_URL: "http://keycloak:8080"
   ports:
     - "8083:8083"
   depends_on:
      - keycloak

【讨论】:

以上是关于从同一个 docker compose 中的 docker 容器拒绝连接的主要内容,如果未能解决你的问题,请参考以下文章

how do you quit docker-compose up @ macOS?

Jenkins Pipeline插件是否支持Do cker Compose?

docker vs docker-compose 解密

从同一个 docker compose 中的 docker 容器拒绝连接

docker-compose部署模板

docker-compose常用模板