Connection refused when setting port to zero and inside a docker container
See original GitHub issueI am trying to develop a test email server that should run in a docker container. The docker container is run with --network=host -P; though for unit tests I would like to use --network=none.
I am deliberately specifying that port 0 should be used (let the OS pick an available port). I then have a function to get the port that was actually used. The server code is
#! /usr/bin/env python3
import asyncio #pylint: disable=unused-import
from aiosmtpd.controller import Controller
class DemoEmailHandler:
async def handle_DATA(self, server, session, envelope):
message = {
'peer' : session.peer,
'mailfrom' : envelope.mail_from,
'to': envelope.rcpt_tos,
'data' : envelope.content}
print(message)
return '250 Message accepted for delivery'
class DemoEmailServer:
def __init__(self):
self._handler = DemoEmailHandler()
self._server = Controller(
handler=self._handler,
hostname="0.0.0.0",
port=0
)
self._server.start()
self._port = self._server.server.sockets[0].getsockname()[1]
def stop(self):
"""Tell the server to stop"""
self._server.stop()
def getPort(self) -> int:
"""Return the port the SMTP (email) server is actually on"""
return self._port
SERVER = DemoEmailServer()
print(SERVER.getPort())
SERVER.stop()
I keep getting connection refused
Traceback (most recent call last):
File "./demo_email_server.py", line 36, in <module>
SERVER = DemoEmailServer()
File "./demo_email_server.py", line 25, in __init__
self._server.start()
File "/usr/local/lib/python3.8/dist-packages/aiosmtpd-1.4.2-py3.8.egg/aiosmtpd/controller.py", line 223, in start
self._trigger_server()
File "/usr/local/lib/python3.8/dist-packages/aiosmtpd-1.4.2-py3.8.egg/aiosmtpd/controller.py", line 313, in _trigger_server
s = stk.enter_context(create_connection((hostname, self.port), 1.0))
File "/usr/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
when running inside a docker container. If I run on the host then the server starts up fine.
(My host and container are both Ubuntu 20.04, python 3.8.5, the container has aiosmtpd 1.4.2 installed from source).
Issue Analytics
- State:
- Created 2 years ago
- Comments:8
Top Results From Across the Web
Connection refused on docker container - Stack Overflow
Command EXPOSE in your Dockerfile lets you bind container's port to some port on the host machine but it doesn't do anything else....
Read more >Connection refused? Docker networking and how it impacts ...
The solution: listen on all interfaces. Port forwarding can only connect to a single destination—but you can change where the server process is ......
Read more >Docker connection refused between two containers
Hey I deployed two container in the same network (tried bridge and another that i created to test). When creating the image of...
Read more >Http Client unable to connect from docker container to another ...
Exposing a container port to the host does not automatically expose it to other containers. Trying to reach 127.0.0.1:8983 inside your container ......
Read more >ssh connect to host port 22: Connection refused - Ask Ubuntu
To permanatly expose a port in Docker you need to edit the Dockerfile for the container and rebuild it. In the Dockerfile add...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Okay, so… I know it’s been a very long time since I’ve touched this, but - yeah, the fix is pretty straightforward, I think.
First off, yeah I was able to reproduce this.
Second off, I think the correct thing for us to do here is in the
_create_server
function where we doself.loop.create_server
we could change it to:By making that change locally, it all worked 👍 We’ll want some test cases for that obviously, but it shouldn’t be too hard to do!
Hopefully this will work out; thank you for look into it.