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 127.0.0.1:8080 {
2024-01-22 15:55:56 error: {
2024-01-22 15:55:56 message: ‘connect ECONNREFUSED 127.0.0.1:8080’,
2024-01-22 15:55:56 stack: ‘Error: connect ECONNREFUSED 127.0.0.1:8080\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 127.0.0.1:8080’
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’

services:
nextjs-app:
image: assessit
ports:

  • “3000:3000”
    depends_on:
  • keycloak

keycloak:
image: jboss/keycloak
environment:

  • KEYCLOAK_USER=akash
  • KEYCLOAK_PASSWORD=Akash@123
    ports:
  • “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/127.0.0.1 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!