Docker to host TCP connections drop after 5 minutes

Viewed 1024

I have a CentOS 7 running within docker desktop on Windows 10. I am connecting from within docker to Oracle 12c running on Windows 10. The connection if idle drops after about 5 minutes. How do I trouble shoot this?

1 Answers

I had same issue running a python app in Docker with a TCP connection to third-party app. The app maintained an indefinite idle TCP connection when run in Python on Linux, however the same app run in Docker container dropped the idle connection at exactly 5 minutes.

I've read conflicting information as to how/why idle TCP connections may drop in Docker (perhaps someone can comment with a definitive answer), nonetheless I found a fix that worked for me.

The solution came in 2 parts: one change in the Docker Compose YML configuration, the second change is in the app code itself.

First, I added sysctls option in the app's Docker Compose yml file:

app:
  image: myImage:latest
  sysctls:
    - net.ipv4.tcp_keepalive_time=200

This changes the image's default Linux setting (default is 600, I believe).

Second, I configured the app's TCP socket code to reference that TCP Keepalive value. In my case, using Python, it looked like this:

socket_obj.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

This socket option is similar in C++.

Hope that helps you and/or someone else who's tripped across this.

Related