0

Following https://stackoverflow.com/a/62543536/6009580 I've tried sending mails from a docker container using the SMTP server of the host.

What is working:

[HOST]$ echo "Test mail." | mail -s "message" my@mail.com # works
[HOST]$ sudo docker run --net=host -it python
[python-container] >>> import smtplib
[python-container] >>> s = smtplib.SMTP('localhost')
[python-container] >>> s.sendmail("from@mail.com",["my@mail.com"],"message") # works

What is not working:

[HOST]$ sudo docker run -it python
[python-container] >>> import smtplib
[python-container] >>> s = smtplib.SMTP('host.docker.internal')

socket.gaierror: [Errno -2] Name or service not known

Even if I start the container with

[HOST]$ sudo docker run --add-host=host.docker.internal:host-gateway -it python
[python-container] >>> import smtplib
[python-container] >>> s = smtplib.SMTP('host.docker.internal')

ConnectionRefusedError: [Errno 111] Connection refused

Any help is appreciated

System:

  • Rocky Linux 8.7
  • Docker 20.10.22
mdede
  • 27
  • 7
  • 1
    Your smtp server is probably configured to accept only local connection (127.0.0.1). Even if your docker runs on the local machine, for the host, the docker vm is a remote machine. – Corralien Jan 17 '23 at 07:29
  • 1
    If you use postfix on your host, check the following parameters in `main.cf`: `inet_interfaces` and `mynetworks` – Corralien Jan 17 '23 at 07:32
  • Yes, you are right, thanks. inet_interfaces was set to loopback-only and I must not change it as it is controlled by my superuser (puppet-controlled). So, I had to switch to a different solution. Thanks! – mdede Jan 19 '23 at 12:50

0 Answers0