'How to run aiosmtpd via Docker

My Dockerfile

FROM python:3.10-alpine

LABEL Description="test smtp server"
EXPOSE 8025
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN python -m pip install --upgrade pip && pip install -r requirements.txt
CMD python3 -m aiosmtpd --nosetuid --debug --debug --listen 127.0.0.1:8025

When i run this container with command

docker build -t test-smtp-image . && docker run -p 8025:8025 --name skad-test-smtp test-smtp-image 

And tying to send email to it from host where container running i got next Exception

    raise SMTPConnectError(
aiosmtplib.errors.SMTPConnectError: Error connecting to 127.0.0.1 on port 8025: Unexpected EOF received

But it works great when i send email with same script and aiosmtpd running command on host

Sending mail script:

from email.message import EmailMessage
import aiosmtplib
import asyncio

m = EmailMessage()
m['From'] = "[email protected]"
m["To"] = '[email protected]'
m["Subject"] = "Test subject"
m.set_content("Test msg")

loop = asyncio.get_event_loop()
r = loop.run_until_complete(aiosmtplib.send(m, hostname="127.0.0.1", port=8025))


Solution 1:[1]

Thanks to David Maze.

Correct Dockerfile:

FROM python:3.10-alpine

LABEL Description="test smtp server"
EXPOSE 8025
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN python -m pip install --upgrade pip && pip install -r requirements.txt
CMD python3 -m aiosmtpd --nosetuid --debug --debug --listen 0.0.0.0:8025

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alpensin