Lostinpy
Lostinpy

Reputation: 15

Mail traffic between MailDev container and Django container delivers Errno 111] Connection refused

Mail traffic between MailDev container and Django container delivers

[Errno 111] Connection refused

Email traffic to console backend works, so the function should be correct.

send_mail(
            'Subject here',
            'Here is the message.',
            '[email protected]',
            ['[email protected]'],
            fail_silently=False
        )

After I switched to SMTP backend and started MailDev in Container, I got:

[Errno 111] Connection refused

in settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

in Shell:

sudo docker run -p 1080:80 -p 1025:25 --name maildev djfarrelly/maildev

My Docker Containers are:

CONTAINER ID        IMAGE                COMMAND                 CREATED             STATUS              PORTS                                        NAMES

a46040dd1259        djfarrelly/maildev   "bin/maildev --web..."   11 seconds ago      Up 9 seconds        0.0.0.0:1025->25/tcp, 0.0.0.0:1080->80/tcp   maildev

4fed036f92d4        exposee              "/usr/src/start.sh"      42 minutes ago      Up 42 minutes       0.0.0.0:8000->8000/tcp                       exposee

2c2cf7ce19e9        winauth              "gunicorn -b 0.0.0..."   43 minutes ago      Up 43 minutes       127.0.0.1:9876->9876/tcp                     winauth

afe4a449aa01        postgres             "docker-entrypoint..."   44 minutes ago      Up 44 minutes       127.0.0.1:5432->5432/tcp                     postgres

My Settings in settings.py are:

EMAIL_HOST = CONFIG.get('EMAIL_HOST', '127.0.0.1')

EMAIL_PORT = CONFIG.get('EMAIL_PORT', 1025)

EMAIL_USE_TLS = CONFIG.get('EMAIL_USE_TLS', False)

EMAIL_USE_SSL = CONFIG.get('EMAIL_USE_SSL', False)

My Configuration in config.json is:

"EMAIL_HOST": "127.0.0.1",

"EMAIL_PORT": 1025,

"EMAIL_USE_TLS": false,

"EMAIL_USE_SSL": false

Upvotes: 1

Views: 1625

Answers (1)

yamenk
yamenk

Reputation: 51768

The problem is that you are trying to connect from one container to another using 127.0.0.1. This won't work are this address will correspond to the container sending the request.

For container to be able to inter-communicate, the standard way is to connect the containers to a common network.

sudo docker network create mail-net
sudo docker run --network main-net -p 1080:80 -p 1025:25 --name maildev djfarrelly/maildev

Connect the container sending the requests to the network:

docker network connect mail-net <container-name>

And change the host from 127.0.0.1 to maildev

EMAIL_HOST = CONFIG.get('EMAIL_HOST', 'maildev')
EMAIL_PORT = CONFIG.get('EMAIL_PORT', 25)

Upvotes: 2

Related Questions