Search code examples
keycloakmailhog

can't establish SMTP connection to the Mailhog from keycloak


I have a docker-compose and it contains Keycloak and Mailhog. I configured the Mailhog in Keycloak but it does not work. I tried MailHog in Powershell it works. Can someone help me figure out why it does not work?

Mailhog configuration step on Keycloak:

  • First I add the email address ([email protected]) for the current Keycloak user.
  • Then I configure the email like below

enter image description here

when I hit the "Test Connection" it gives "Error! Failed to send email" error message. But I try send an email using the below cmd it works.

Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test email1" -SmtpServer "localhost" -Port 1025

Docker compose file

        version: '3.6'
    
    services:
      mailhog-keycloak:
        image: mailhog/mailhog
        container_name: mailhog
        logging:
          driver: 'none'  # disable saving logs
        ports:
          - 1025:1025 # smtp server
          - 8025:8025 # web ui
        networks:
          - dev_network
      postgres-keycloak:
        image: postgres:9.6-alpine
        container_name: postgres
        environment:
          POSTGRES_DB: keycloak
          POSTGRES_USER: keycloak
          POSTGRES_PASSWORD: password
        networks:
          - dev_network
    
      keycloak:
        image: jboss/keycloak:10.0.2
        container_name: keycloak_10
        environment:
          DB_VENDOR: POSTGRES
          DB_ADDR: postgres
          DB_DATABASE: keycloak
          DB_USER: keycloak
          DB_PASSWORD: password
          KEYCLOAK_USER: admin
          KEYCLOAK_PASSWORD: admin
        ports:
          - 8080:8080
        external_links:
          - postgres
        volumes:
          - ./themes/keycloak:/opt/jboss/keycloak/themes/shoping
          - postgres-keycloak
        networks:
          - dev_network
    networks:
      dev_network:

Solution

  • Problem is the localhost - each container has own namespace, so each container has own localhost, which is independent from other container localhosts.

    Use mailhog (maybe mailhog-keycloak) instead of localhost in the Host field - that's a container/service name, which should resolve current mailhog container IP, so it should works.