Make Mark
Make Mark

Reputation: 3765

Start Docker container using systemd socket activation?

Can an individual Docker container, for example a web server, that exposes (listens on) a port be started using systemd's socket activation feature? The idea is to save resources by starting a container only when it is actually needed for the first time (and possibly stop it again when idle to save resources).

Note: This question is not about launching the Docker daemon itself using socket activation (which is already supported), but about starting individual containers on demand.

Upvotes: 18

Views: 4835

Answers (3)

Erik Sjölund
Erik Sjölund

Reputation: 11408

Yes, you can with Podman. For details, see the Podman socket activation tutorial. Podman supports socket activation since version 3.4.0 (released Sep 2021).

(Docker does not yet support socket activation of containers so you would need to use Podman for this)

Example 1: mariadb

I wrote a small example demo of how to set up socket activation with systemd, podman and a MariaDB container:

https://github.com/eriksjolund/mariadb-podman-socket-activation

MariaDB supports socket activation since version 10.6 (released April 2021)

Example 2: nginx

https://github.com/eriksjolund/podman-nginx-socket-activation

See also my answer https://stackoverflow.com/a/71188085/757777

Upvotes: 4

michielbdejong
michielbdejong

Reputation: 1107

If it has to be using systemd, there was a blog post last month about that, here (haven't tried it myself yet).

If the choice of technology is not a hard constraint, you could just write a small proxy in your favorite programming language, and simply make a Docker API call to ensure the container is started. That's the way snickers (my experimental nodejs proxy) does it.

Upvotes: 2

David Timothy Strauss
David Timothy Strauss

Reputation: 732

In short, you can't.

But, if you wanted to approach a solution, you would first need to run a tool like CoreOS or geard that runs each Docker container in a systemd service.

Even then, Docker's support for inheriting the socket has come and gone. I know geard is working on stable support. CoreOS has published generalized support for socket activation in Go. Red Hat folks have also added in related patches to Fedora's Docker packages that use Go's socket activation library and improve "foreground mode," a key component in making it work.

(I am the David Strauss from Lennart's early article on socket activation of containers, and this topic interests me a lot. I've emailed the author of the patch at Red Hat and contacted the geard team. I'll try to keep this answer updated.)

Upvotes: 13

Related Questions