我有一个akka HTTP应用程序和一个keycloak docker容器我使用SBT本地docker插件来创建我的应用程序的图像,然后我写了一个docker撰写文件,但我的应用程序容器没有发现keycloak容器
这是我的构建。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项目
请指教我一下
您需要通过环境变量将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