Germano
Germano

Reputation: 624

Docker Mailhog with Docker django error: [Errno 111] Connection refused

I have 2 containers running through docker-compose, one with Django, the other with Mailhog. But when I send_mail through Django python manage.py runserver, it is possible to send, if i run a docker-compose up when I send email this error is returned:

[Errno 111] Connection refused

My docker-compose is:

services:
    mailhog:
        image: mailhog/mailhog
        logging:
            driver: 'none' # disable saving logs
        ports:
            - 1025:1025 # smtp server
            - 8025:8025 # web ui
        networks:
            - my_net
    api:
        build: .
        container_name: my_api
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
            - .:/src
        ports:
            - '8000:8000'
        env_file:
            - '.env'
        depends_on:
            - mailhog
        networks:
            - my_net

networks:
    my_net:

My env file is:

EMAIL_HOST = '0.0.0.0'
EMAIL_PORT = '1025'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''

What should I do?

Upvotes: 1

Views: 1013

Answers (1)

Germano
Germano

Reputation: 624

Found it, i had to change 2 things:

1- changed my docker-compose, adding a container_name to mailhog container and linking api to mailhog

services:
    mailhog:
        image: mailhog/mailhog
        container_name: mailhog
        logging:
            driver: 'none' # disable saving logs
        ports:
            - 1025:1025 # smtp server
            - 8025:8025 # web ui
        networks:
            - my_net
    api:
        build: .
        container_name: my_api
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
            - .:/src
        ports:
            - '8000:8000'
        env_file:
            - '.env'
        depends_on:
            - mailhog
        networks:
            - my_net
        links:
            - 'mailhog'

networks:
    my_net:

2- and changed my EMAIL_HOST to the docker container_name

EMAIL_HOST = 'mailhog'
EMAIL_PORT = '1025'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''

Upvotes: 1

Related Questions