I have been working on a dev project with a quarkus server running on my machine. I have a keycloak and postgres instances both running on docker containers. The current setup is that I have a docker-compose file for keycloak and postgres and I run quarkus from the CLI. All 3 processes can communicate with one another.
I am now at the point where I want to package up my quarkus application into a docker container and host it on the cloud, along with postgres and keycloak. Problem is I am having great difficulty in getting the app to communicate with the db and keycloak and I can only assume it is a docker networking issue....
docker-compose.yml
version: "3.3"
services:
keycloak:
image: jboss/keycloak:13.0.1
environment:
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: password
DB_VENDOR: postgres
DB_ADDR: postgres_db
DB_DATABASE: name
DB_SCHEMA: keycloak
DB_USER: admin
DB_PASSWORD: password
KEYCLOAK_IMPORT: /tmp/keycloak-realm.json
volumes:
- ./keycloak-realm.json:/tmp/keycloak-realm.json
depends_on:
- postgres_db
ports:
- "8180:8080"
networks:
- whole_network
postgres_db:
build: postgres/.
environment:
POSTGRES_PASSWORD: password
POSTGRES_USER: admin
POSTGRES_DB: name
volumes:
- ./postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "sudo pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
networks:
- whole_network
networks:
whole_network:
driver: bridge
application.properties
quarkus.http.test-port=8888
quarkus.resteasy.path=/api/v1
quarkus.http.cors=true
quarkus.oidc.auth-server-url=http://keycloak:8180/auth/realms/realm
quarkus.oidc.client-id=candledata
quarkus.oidc.application-type=SERVICE
quarkus.http.auth.permission.authenticated.paths=/*
quarkus.http.auth.permission.authenticated.policy=authenticated
quarkus.datasource.db-kind=postgresql
quarkus.datasource.jdbc.url=jdbc:postgresql://postgres_db:5432/name?sslmode=disable
quarkus.datasource.username=admin
quarkus.datasource.password=password
quarkus.liquibase.migrate-at-start=true
quarkus.liquibase.change-log=db/changelog/db.changelog-master.xml
quarkus.hibernate-orm.database.generation=drop-and-create
On the app.props file I originally had the urls as localhost
but changed them to the container name when I was putting the quarkus app into a container.
The Dockerfile is just a standard one that comes with all quarkus projects. But the commands I use to build the image and run the container are;
mvn package -DskipTests=true
docker build -f src/main/docker/Dockerfile.jvm -t backend .
docker run -i --rm -p 8888:8888 --network=cd_whole_network backend
When I run I get the following output;
exec java -Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager -XX: ExitOnOutOfMemoryError -cp . -jar /deployments/app.jar
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2021-09-29 07:32:13,750 ERROR [io.qua.run.Application] (main) Failed to start application (with profile prod): java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:777)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:702)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:829)
Googling the specific didn't prove much use for me. I assumed with all 3 containers on the one network, they would be able to communicate with one another but that is also not the case. Any help/advice on this would be greatly appreciated
CodePudding user response:
You are using the wrong port for keycloak in your application configuration.
You have this configuration :
quarkus.oidc.auth-server-url=http://keycloak:8180/auth/realms/realm
However, in your docker-compose file, for the keycloak service, you have this
ports:
- "8180:8080"
This means that you can reach keycloak on port 8180
from your host and on port 8080
from the container. As you're using docker network to reach your service, then you should use port 8080
to reach keycloak.