Supun Kavinda
Supun Kavinda

Reputation: 356

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:

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:

Upvotes: 3

Views: 5456

Answers (3)

MartinoLaguna
MartinoLaguna

Reputation: 61

Your docker configuration looks alright. The error itself lies within the keycloak email configuration.

Just like the approved answer mentioned - the docker containers do have their own localhosts.

Since mailhog and your keycloak instance are docker containers - i'd suggest using host.docker.internal as your Host address. The port stays 1025.

Upvotes: 6

Gediminas R
Gediminas R

Reputation: 1

Make sure keycloak and mailhog are in the same network on docker.

Upvotes: 0

Jan Garaj
Jan Garaj

Reputation: 28626

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.

Upvotes: 11

Related Questions