Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have an Ubuntu 18.04 image runing on my docker container. I login into it and installed Openresty . also installed systemd . When I use command systemctl I get this error:

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

How can I fix it?

As two general rules, you shouldn't install software inside running containers (it will get lost as soon as your container exits), and commands like systemctl just don't work inside Docker. You might think of Docker as a way to package an application and not like a full-blown VM with an init system and users and processes; Docker's Containerizing an application tutorial walks through a basic standard use case. – David Maze Dec 24, 2019 at 11:08 Does this answer your question? How do I run a Docker container that uses SystemD from the latest version of Ubuntu (18.10)? – Bruno Wego Jan 10, 2020 at 19:41

If I understand the OP, he is trying to run systemctl within the container. This does not work because systemd is not running within the container to begin with. It cannot be done in unprivileged containers. There is another question here in SO about why he should not run systemd within a container.

I quickly googled and found this 2014 page about using systemd within a container in docker, where there is a short explanation. The fix is to use a privileged container (running docker run --privileged ...), which is arguably a bad idea but might suit the OP. There is a 2019 update of that last article, and the bottomline is they developed their own container engine (so no docker).

The obvious solution would be to have a single service, so no need for systemd, although that might not be possible in the OP's case.

In summary, possible solutions:

  • not to use systemd
  • use a privileged container
  • not to use docker
  • +1 for dockerd. At least the daemon started and I can see error messages showing what's happening! But still getting, docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: can't get final child's PID from pipe: EOF: unknown. – GlenPeterson Jan 16 at 18:13 Getting this error: grpc: addrConn.createTransport failed to connect to {unix:///var/run/docker/containerd/containerd.sock <nil> 0 <nil>}. Err :connection error: desc = "transport: Error while dialing dial unix:///var/run/docker/containerd/containerd.sock: timeout". Reconnecting... module=grpc failed to start daemon: Devices cgroup isn't mounted – smartmouse Jan 30 at 22:41 Nope, he didn't mention /usr/sbin/init, his answer was not enough. This answer is doing it. – jan Mar 27 at 12:42

    After toying with Systemd myself and bumping into this I found a good solution to work around this in Docker.

    You can setup a cronjob to run on container reboot.

    Dockerfile.yml:

    COPY startup.sh /home/$USERNAME
    WORKDIR /home/$USERNAME
    RUN chmod +x startup.sh
    RUN runuser -u $USERNAME -- echo "@reboot /home/$USERNAME/startup.sh" >> cronjobs
    RUN runuser -u $USERNAME -- crontab cronjobs
    RUN runuser -u $USERNAME -- rm cronjobs
    

    https://askubuntu.com/questions/814/how-to-run-scripts-on-start-up#816

    Hmm, after further testing it seems this does not work either. Seems the best way to do this is to schedule a cron on the main machine to run a command on this docker container. In my case I only need to do this on first run. – Barco Feb 1, 2022 at 8:58

    To complement @javier-gonzalez answer, if you're following running systemd within container AND getting the error bash: /usr/sbin/init: No such file or directory when trying to run the container, you can use /lib/systemd/systemd as ENTRYPOINT in your Dockerfile instead since /usr/sbin/init since it is just a symlink to the same thing.

    FROM ubuntu:<anyversion>
    ENTRYPOINT ["/lib/systemd/systemd"]
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid …

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.