Next js and Keycloak Runs in cointainer but not connecting each other

My Keycloak is runnining in docker cointainer from starting onwords and the next js application with keycloak configuration is running locally fine. when i made my next js build and made cointainerized then the 3000 port of my next js is running when tray to sign in throw keycloak it shows in Try signing in with a different account. and in docker console shows this error . [next-auth][error][SIGNIN_OAUTH_ERROR]
2024-01-22 15:55:56 Errors | NextAuth.js connect ECONNREFUSED {
2024-01-22 15:55:56 error: {
2024-01-22 15:55:56 message: ‘connect ECONNREFUSED’,
2024-01-22 15:55:56 stack: ‘Error: connect ECONNREFUSED\n’ +
2024-01-22 15:55:56 ’ at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1571:16)',
2024-01-22 15:55:56 name: ‘Error’
2024-01-22 15:55:56 },
2024-01-22 15:55:56 providerId: ‘keycloak’,
2024-01-22 15:55:56 message: ‘connect ECONNREFUSED’
2024-01-22 15:55:56 }
2024-01-22 15:55:12
2024-01-22 15:55:12 > assess-it@0.1.0 start
2024-01-22 15:55:12 > next start

1 Like

Share docker-compose.yml or your CLI commands.

Hii ,

This is my docker-compose.yml file when i run this by docker-compose up command it will create new keycloak for me,but my keycloak is already running in another container with my backend and front end configuration. version: ‘3’

image: assessit

  • “3000:3000”
  • keycloak

image: jboss/keycloak

  • “8080:8080”

You config gives me the impression you are a beginner, not sure if an enterprise tool like keycloak is the right one. Format config before and after with 3 backticks.

When using multiple containers, you usually attach them to a Docker network for internal communication, only the proxy opens the ports externally (see simple Traefik example). Then you can access other service/container by service name through Docker DNS.

You can not use localhost/ inside a container to connect to another, as that is localhost inside the container, not the one of the node/host.

Why did you tag this post with “swarm”?

Hello @akashtpd,
It looks like you are having a network issue. I explained how you can setup this environment in this post:

I used next-auth but still if you take a look at the .env.local and route.ts configuration I’m pretty sure you can solve it.

Take care and let me know if you have any questions!