Search code examples
dockersystemd

RUN in Dockerfile with systemd as pid 1


Is it possible to use RUN in a dockerfile while having systemd as pid 1?

I am trying to execute an install script that requires systemd to be present and running on the system, inside a dockerfile. I.e.

FROM debian:stable  

RUN apt install -y systemd

RUN someInstallScriptThatRequiresSystemd.sh

Solution

  • Is it possible to use RUN in a dockerfile while having systemd as pid 1?

    No. Each RUN command generally runs in its own container, and the command itself (or the sh -c wrapper Docker provides) will be pid 1.

    Also remember that a Docker image doesn't contain running processes, only a filesystem image and metadata that says how to run a container. You can't persist an image with systemd or other services running, and since the image doesn't container running services, it doesn't make sense to restart a service in a Dockerfile.

    Systemd wants to control a lot of things, most of which are host-level things that a container shouldn't be thinking about. You usually shouldn't run it in a container at all; if you must, the better setups remove most of the built-in system-setup tasks. Better is to use a lighter-weight supervisor like supervisord; better still is to run one process per container, and a minimal init like tini if you need it.

    If you just need to let this installer run systemctl, you can cause that "command" to exist:

    RUN ln -s /bin/true /sbin/systemctl
    RUN systemctl restart my-service # does nothing, successfully