0

I building an app using Django 4.0.4, allauth, Postgres, and Docker with Docker Compose.

My Dockerfile contains:

FROM python:3.10.4-slim-bullseye

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# Copy project
COPY . .

docker-compose.yml

version: '3.9'

services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
      - 587:587
    depends_on:
      - db
    environment:
      - DJANGO_SECRET_KEY=blablabla
      - DJANGO_DEBUG=True
      - POSTGRES_NAME=blablabla
      - POSTGRES_USER=blablabla
      - POSTGRES_PASSWORD=blablabla
  db:
    image: postgres:13
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust
      - POSTGRES_DB=blablabla
      - POSTGRES_USER=blablabla
      - POSTGRES_PASSWORD=blablabla

volumes:
  postgres_data:

Django settings.py related to email backend (a bit more)

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

AUTH_USER_MODEL = "accounts.CustomUser"

# django-crispy-forms
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = "bootstrap5"

# django-allauth config
LOGIN_REDIRECT_URL = "home"
ACCOUNT_LOGOUT_REDIRECT = "home"
SITE_ID = 1
AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
)
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
ACCOUNT_SESSION_REMEMBER = True
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True

DEFAULT_FROM_EMAIL = "admin@admin.com"
EMAIL_HOST = 'localhost'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'mail'
EMAIL_HOST_PASSWORD = 'password'

I keep receiving this error message:

  File "/usr/local/lib/python3.10/site-packages/django/core/mail/message.py", line 298, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/usr/local/lib/python3.10/site-packages/django/core/mail/backends/smtp.py", line 124, in send_messages
    new_conn_created = self.open()
  File "/usr/local/lib/python3.10/site-packages/django/core/mail/backends/smtp.py", line 80, in open
    self.connection = self.connection_class(
  File "/usr/local/lib/python3.10/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/local/lib/python3.10/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/local/lib/python3.10/smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/usr/local/lib/python3.10/socket.py", line 845, in create_connection
    raise err
  File "/usr/local/lib/python3.10/socket.py", line 833, in create_connection
    sock.connect(sa)

Exception Type: OSError at /accounts/signup/
Exception Value: [Errno 99] Cannot assign requested address
David Maze
  • 130,717
  • 29
  • 175
  • 215
panos
  • 328
  • 1
  • 4
  • 16
  • Your Compose file suggests your container accepts inbound connections on port 587, but your configuration file suggest it's making outbound connections to that port. It seems unlikely that your Django application (`localhost`) is accepting encrypted SMTP connections (`EMAIL_HOST`) on port 587; where is the mail server actually running? – David Maze May 07 '23 at 22:49

0 Answers0