Good to know: Linux services and Docker

Docker and Unix services
As a general rule of thumb there is one docker container for each service. It’s like a mantra echoing in the internet to separate concerns and to follow the Unix design principle of doing one thing, and doing one thing right (see Wikipedia) (Btw. look at this video from the vaults explaining the UNIX operating system in 1982 – containing interviews with primary developers Ritchie, Thompson, Brian Kernighan, and many others… Youtube – AT&T Archives: The UNIX Operating System).

The idea is that containers should serve a distinct purpose and so should only run one service (highlander-principle). And there are un-doubtful disadvantages of merging several services into on container: polluted logs from different services, dependencies and difficult restarts in case of crashes (see a good thread here: devops.stackexchange – Why it is recommended to run only one process in a container?).
However there are use-cases where it makes sense to run multiple processes in a single docker-container, as long as all the processes support a single, modular function. This is the case eg. in Dev-Containers (see former blog-posts) – i do need this for the web-devcontainer containing 🙂 nginx, or lighttpd and mariadb.

Run multiple services in a container
The official docker-documentation describes the case for running “multiple services in a container” – but also states, you should have good reasons to do so and otherwise better use user-definded networks and shared volumes to connect containers. There are 3 possibilities:
1) Run a wrapper shell-script in the container’s main running process via CMD (Dockerfile)
2) same as 1, but additionally use bash’s job control – i would not need this so far.
3) use a process manager like supervisord. – However not packed in my base-image and seems overkill. See this great post at digitalocean about the different service management daemons.

Setup for Devcontainer
In a Devcontainer, the above mentioned ways need a “”overrideCommand”: false” set as a prerequisite. But, anyhow, for a Devcontainer there is a more elegant way. You can use the “postStartCommand” in the devcontainer.json file:


"postStartCommand": "nohup bash -c 'service lighttpd start && service mariadb start'"

Or just:

"postStartCommand": "service lighttpd start && service mariadb start"

For details see: https://code.visualstudio.com/remote/advancedcontainers/start-processes?WT.mc_id=devcloud-0000-buhollan


Resources:

https://docs.docker.com/config/containers/multi-service_container/
https://www.digitalocean.com/ Service restart

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.