在同一个 docker compose 中从 docker 个容器中获取连接被拒绝

getting connection refused from docker containers in a same docker compose

我有一个 akka http 应用程序和一个 keycloak docker container 我使用 sbt native docker plugin 创建了我的应用程序的图像,然后我写了一个 docker 撰写文件,但我的应用程序容器没有发现 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.$anonfun$applyOrElse(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.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 项目

请在这方面指导我

您需要通过环境变量将keycloak的地址注入到您的服务中。为此,您可以使用服务名称作为 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