Swarm 1.12 Routing data to specfic container sticky session

I have created swarm cluster with 1 manager and 3 workers. I have created 10 containers for a web based application using docker swarm and I can see all the 10 if I list using docker service task <SERVICE_ID> command. Native load balancing is also working fine with the public facing port, and the requests are distributed to all the containers by swarm. But only problem that we are facing is, as this is a web based application, we need to maintain the session and all the requests coming for that session should land in the same container. But as of now the requests are distributing irrespective of the session. Can any one explored ,how to achieve sticky sessions with Swarm 1.12 native load balancing.

There is no sticky session support in the current implementation. Read @nathanleclaire answer in Docker 1.12 swarm still needs http (reverse) proxy?

Some interesting solutions. I hope in the future somthing like this is built-in where it’s super easy to turn on. I need sticky sessions for websockets unfortunately.

Won’t most websocket sessions use just one persistent connection which will “stick” anyway? Why would it need to re-connect to the same host it originally connected to in the case of a disconnect? (And what happens if that host goes down?) With a little bit of careful design you might be able to happily have arbitrary servers taking and servicing websocket requests, but it does require a bit of discipline to keep state out of the app itself and store it in a data layer.

So glad that I finally figured a way on how to do it.

Wrote a blog for this problem in detail overe here…

I saw this post too late… I’ve made my own reverse HTTP proxy that supports sticky sessions.

Here is the link if you’d like to take a look.

https://hub.docker.com/r/cendra/virtualhost/

Honestly, this is such a common situation you’d think Docker would suggest solutions for this more helpfully. Who runs multiple websites on different ports?

For info, there are existing reverse proxies that do this and automatically reconfigure as Swarm services change:
https://traefik.io/