How to setup docker container to test systemd when using molecule

That docker file have
DockerFile

/bin/systemctl

System has not been booted with systemd as init system (PID 1). Can’t operate.

  - name: Create a container
      community.docker.docker_container:
        name: "{{ item.name }}"
        image: "{{ item.image }}"
        state: started
        # command: sleep 1d
        command: /lib/systemd/systemd --system --log-level=info --log-target=console
        cgroupns_mode: private
        security_opts:
          - seccomp=unconfined
        log_driver: json-file
        volumes:
          - "/sys/fs/cgroup:/sys/fs/cgroup:rw"
          - "/tmp:/tmp"
          # - "/run:/run"
        privileged: true
        tmpfs:
          - /run
          - /run/lock
        env:
          container: docker
      register: result
      loop: "{{ molecule_yml.platforms }}"

do have something miss when execute the docker image

Majority of containers do not contain systemd inside. There is a long story about that. Containers that do contain systemd commonly called init containers.

Just an example


The Universal Base Image Init is designed to run an init system as PID 1 for running multi-services inside a container. “

1 Like

You have to use Docker images that have systemd. I’m using these:

or on Docker Hub:

There are examples on how to use images with molecule in GitHub README.md. You can also check the DockerFiles to figure out how systemd enabled images are made… if you want to create your custom ones.

Great shout out to Anton Melekhin - antmelekhin - for creating these.

1 Like

I build my own images with Systemd for use with Molecule run via GitLab CI, see this thread for details: