Search code examples
dockerforkpreforking

Moving prefork application under docker


I have a prefork model server. Meaning that the application will start, bind a socket on a port, them fork X times so every children while share the same port.

I now want to port this application under docker. My understanding is one docker instance should run only one of my server, for a better usability. So if I want to run 5 instances of my process, I should run 5 docker instance all running once my application. Am I correct so far?

If yes, my problem is, is it possible to have all my docker instances all running under the same port?

I agree I could use a reverse proxy where I map a range of port for my service, but I would like to avoid this if possible.


Solution

  • Docker provides a process namespace for your container, where your process may fork children or run multi-threaded. You can easily run a multi-process application inside of a docker container, and in fact that's what you do anytime you run a bash shell and then run any other cli command inside of bash.

    The only requirement is that the process you launch with docker run takes on the role of init on a linux system as pid 1. What that means is if your pid 1 exits, the entire container immediately terminates. So your main process needs to remain running in the foreground.

    Also, if any processes are not reaped by their parents upon exit and they enter a zombie state, they will not be cleaned automatically by the host OS's init (the zombie processes won't propagate beyond the namespace. To solve that, there are projects like tini that act as an init replacement inside of a container.