Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keycloak in docker-compose network

I want to setup a Docker network that contains a keycloak, postgres, and webapp instances.

Is there a way to have network communications between containers but also understand oidc client redirects as well? I am having an issue where containers can talk to each other just fine if i setup OIDC with container names for the docker network, but then I run into issues with the client that cannot connect to the those same URLs outside of the docker network on the host machine.

Can anyone point me to the right docker documentation to look at for possible solutions with DNS or host to container communication?

---- EDIT ----

To clarify. The containers can talk to each other just fine under their container names, but the client (i.e., Chrome) has to use localhost to talk to everything. In my setup for my OIDC connection in the ui web application I have to use container names or localhost. How do I get my client to understand container names in order to make the right request?

version: '2'

services:

  ui:
    container_name: 'ui'
    image: 'bdparrish/ui:0.1'
    build:
      context: .
      dockerfile: ./ui/Dockerfile
    ports:
      - "8085:80"
    depends_on:
      - "postgres"
      - "keycloak"
    networks:
      - auth-network
    environment:
      - ASPNETCORE_ENVIRONMENT=Docker

  postgres:
    container_name: postgres
    image: 'postgres'
    environment:
      POSTGRES_PASSWORD: password
    ports:
      - "5432:5432"
    networks:
      - auth-network

  keycloak:
    container_name: keycloak
    image: jboss/keycloak
    ports: 
      - "8080:8080"
    depends_on:
      - postgres
    environment:
      DB_VENDOR: "POSTGRES"
      DB_ADDR: postgres
      DB_PORT: 5432
      DB_USER: keycloak
      DB_PASSWORD: password
      KEYCLOAK_USER: admin
      KEYCLOAK_PASSWORD: password
    restart: always
    networks:
      - auth-network

networks:
  auth-network:
    driver: bridge
like image 352
bdparrish Avatar asked Jun 03 '18 20:06

bdparrish


2 Answers

You don't have to modify the etc/hosts file. There is an environment variable for keycloak named KEYCLOAK_FRONTEND_URL especial for this purpose.

Edit your docker compose file to look like this:

version: '2'

services:

  ui:
    container_name: 'ui'
    image: 'bdparrish/ui:0.1'
    build:
      context: .
      dockerfile: ./ui/Dockerfile
    ports:
      - "8085:80"
    depends_on:
      - "postgres"
      - "keycloak"
    networks:
      - auth-network
    environment:
      - ASPNETCORE_ENVIRONMENT=Docker

  postgres:
    container_name: postgres
    image: 'postgres'
    environment:
      POSTGRES_PASSWORD: password
    ports:
      - "5432:5432"
    networks:
      - auth-network

  keycloak:
    container_name: keycloak
    image: jboss/keycloak
    ports: 
      - "8080:8080"
    depends_on:
      - postgres
    environment:
      DB_VENDOR: "POSTGRES"
      DB_ADDR: postgres
      DB_PORT: 5432
      DB_USER: keycloak
      DB_PASSWORD: password
      KEYCLOAK_USER: admin
      KEYCLOAK_PASSWORD: password
      KEYCLOAK_FRONTEND_URL=http://localhost:8080/auth
    restart: always
    networks:
      - auth-network

networks:
  auth-network:
    driver: bridge

Than the login should be redirected to that url.

like image 181
Patrick Lindner Avatar answered Oct 17 '22 10:10

Patrick Lindner


All you need to do is add an entry to your hosts file:

  • Windows: C:\Windows\System32\drivers\etc\hosts
  • Linux: /etc/hosts

Append this to the end of the file: 127.0.0.1 keycloak

Then use keycloak:8080 from your UI to talk to your keycloak server instead of localhost:8080. You can still use localhost:8580 to visit the UI in the browser.

like image 21
IMRZ Avatar answered Oct 17 '22 10:10

IMRZ