Why does my service hang up immediately?

So, I've developed a dummy SMTP/POP3 service to use while working on applications that send/receive email. I was super excited to build it in Swift, and the first thing I did was implement it as a desktop application with SwiftUI. However, since (one of the) applications that interacts with it is running in a Docker container, I needed to have it running in another container so it could be reached by the Vapor server. Anyway.

When I'm running a shell in the container that is running my dummy email server, I can telnet in to the port it's listening on and interact with it no problem. However, when I telnet in from outside that container -- no matter whether it's on another container in the same network or from my host laptop -- the connection gets terminated as soon as it's established, even before my server can send its welcome message.

I have tried setting the server's socket receive timeout during its initialization, and that seems not to be making any difference. Is anyone aware of a setting, whether with Docker or with Swift NIO, that might cause this kind of behavior?

Hi @sbeitzel,

When I'm running a shell in the container that is running my dummy email server, I can telnet in to the port it's listening on and interact with it no problem. However, when I telnet in from outside that container -- no matter whether it's on another container in the same network or from my host laptop -- the connection gets terminated as soon as it's established, even before my server can send its welcome message.

This sounds very much like your are not binding the container port to your host machines port.

If you have further problems, please copy and paste your docker run command here so that we can fugure out the problem together.

1 Like

Are you maybe listening to just localhost/127.0.0.1/::1 in the container? If yes, try to listen on 0.0.0.0/::, otherwise your server won't accept connections that don't come from localhost and that would have exactly the effect you describe.

I believe you're seeing that the connection is first accepted and then immeidately closed because I reckon you use Docker for Mac. If you do a port forward in Docker for Mac, then docker will accept any connections and then (inside the Linux VM) try to connect itself. But if that connection fails with ECONNREFUSED (because there's nothing listening) then Docker for Mac will then close the connection it just accepted.

So if that happens what you'll see is a seemingly successful connection followed by either a clean close or ECONNRESET very quickly after the connection opened.

3 Likes

That was absolutely the problem, thank you!

So, the server bootstrap I'm using had looked like:

serverChannel = try bootstrap.bind(host: "localhost", port: port).wait()

Simply changing localhost to 0.0.0.0 resolved the issue.

3 Likes