Search code examples
pythondjangodockeremaildjango-q

Django send mail from docker container how to expose port


The mail sending works fine without the docker container. I think I actually opened the SMTP Port. Here is my docker-compose file:

version: "3.9"

services:
  db:
    <some postgres setup>
  api:
    build: .
    command: python manage.py runserver 0.0.0.0:8000 --settings=api.settings.production
    volumes:
      - .:/code
    ports:
      - "8000:8000"
      - "587:587"
      - "25:25"
    expose:
      - 587
      - 25
    env_file:
      - ./.env.dev
    depends_on:
      - db
    entrypoint: /entrypoint.sh
  mail:
    build: .
    command: python manage.py qcluster --settings=api.settings.production
    depends_on:
      - api

It doesn't work to send mails no matter if I send it as an async task with django q or directly with django.core.mail.send_mail

Here is my mail settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'mail'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False

I get an OSError OSError: [Errno 99] Cannot assign requested address


Solution

  • The problem was that the port was blocked by my wifi.