I have containers running locally for both Keycloak and Mailhog. I then wanted to send some test emails from Keycloak, but I am always getting the error below. I have made the configuration as outlined at the bottom (localhost:1025
) and tried various other things - all with no success unfortunately. I have also found a handful of questions on this topic here on stackoverflow - unfortunately however, the answers given there did either not solve my problem (changing the hostname) or I could not really understand (changing php.ini).
keycloak-custom-keycloak-1 | com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 1025; timeout 10000;
keycloak-custom-keycloak-1 | nested exception is:
keycloak-custom-keycloak-1 | java.net.ConnectException: Connection refused (Connection refused)
From what I can see, I have configured everything properly, checked whether the containers are running with the correct ports, but still I am getting this error. (Details see below).
My docker-compose.yml
file looks like this:
version: '3.3'
services:
keycloak:
image: jboss/keycloak #:${KEYCLOAK_VERSION}
ports:
- "8080:8080"
environment:
- KEYCLOAK_USER=${KEYCLOAK_USER}
- KEYCLOAK_PASSWORD=${KEYCLOAK_PASSWORD}
- DB_DATABASE=${KEYCLOAK_DATABASE_NAME}
- DB_USER=${KEYCLOAK_DATABASE_USER}
- DB_PASSWORD=${KEYCLOAK_DATABASE_PASSWORD}
- DB_ADDR=${KEYCLOAK_DATABASE_HOST}
- DB_VENDOR=${KEYCLOAK_DATABASE_VENDOR}
- KEYCLOAK_IMPORT=/tmp/realm-export.json
volumes:
- ./keycloak/realms/realm-export.json:/tmp/realm-export.json
- ./keycloak/scripts/disable-theme-cache.cli:/opt/jboss/startup-scripts/disable-theme-cache.cli
- ./keycloak/themes/gesetzeio:/opt/jboss/keycloak/themes/gesetzeio
networks:
internal:
depends_on:
- keycloakdb
keycloakdb:
image: postgres:${POSTGRES_VERSION}
ports:
- "5433:5432"
environment:
- POSTGRES_USER=${KEYCLOAK_DATABASE_USER}
- POSTGRES_PASSWORD=${KEYCLOAK_DATABASE_PASSWORD}
- POSTGRES_DB=${KEYCLOAK_DATABASE_NAME}
volumes:
- keycloak-postgres:/var/lib/postgresql/data
networks:
internal:
mailhog:
image: mailhog/mailhog:latest
restart: always
ports:
- 1025:1025
- 8025:8025
volumes:
keycloak-postgres:
networks:
internal:
When I start the Mailhog container, I get the following message in the logs:
2022/12/09 16:12:28 Using in-memory storage
2022/12/09 16:12:28 [SMTP] Binding to address: 0.0.0.0:1025
2022/12/09 16:12:28 Serving under http://0.0.0.0:8025/
[HTTP] Binding to address: 0.0.0.0:8025
In Keycloak, I have entered the following test configuration:
Screenshot of the Email settings in my local Keycloak container
I have further tried - alternatively to the hostname localhost
- the following options
0.0.0.0
127.0.0.1
- the Mailhog container's name
keycloak-custom-mailhog-1
(as suggested here)
None of them however worked.
What am I doing wrong?
The communication between containers needs to happen using the hostnames declared in the docker compose file.
This means that you need to use the hostname
mailhog
, not127.0.0.1
(and0.0.0.0
should never work, because that can only be used when binding, with the meaning "all available IP addresses of this host", though some tools will use 127.0.0.1 when they are configured to connect to 0.0.0.0).However, to be able to actually access
mailhog
, you need to be sure it is up and running, so you also need to addmailhog
to thedepends_on
ofkeycloak
. In addition, the mailhog container has to be on the same virtual network as keycloak, so you need to add anetworks
with valueinternal
tomailhog
.