16

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
bdparrish
  • 3,216
  • 3
  • 37
  • 58
  • Can you post your setup with the container names? Being into the docker network should not be a matter for the containers. If your container is publicly accessible, OIDC should work properly. – Aritz Jun 04 '18 at 13:00

2 Answers2

7

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

Then the login should be redirected to that url.

Patrick Lindner
  • 291
  • 3
  • 9
2

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.

IMRZ
  • 206
  • 2
  • 6
  • 1
    i'm sure there is a way to manually configure as you have stated, but i am looking more for a Keycloak configuration that will correct the issue or even a token request workflow configuration – bdparrish Jan 03 '20 at 18:22
  • 1
    As pointed here: https://stackoverflow.com/questions/57213611 there seems to be no better way – dve Jun 03 '20 at 10:16