Docker System has not been booted with systemd as init system

76,250

Solution 1

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

Solution 2

In your terminal, you can type:

$ sudo dockerd

and the magic is happen

So, Open other terminal and try it

$ docker ps -a

If you still have a problem with permission, run:

$ sudo usermod -aG docker your-user

Solution 3

You need to start your container by this command to enable systemd.

docker run -itd --privileged docker pull ubuntu:18.04 /usr/sbin/init

Solution 4

Did you try to use: sudo /etc/init.d/docker start instead of systemd ?

I have a similar problem and it solves it.

Share:
76,250
Abdol Seed
Author by

Abdol Seed

Updated on July 09, 2022

Comments

  • Abdol Seed
    Abdol Seed 3 months

    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?

  • Barco
    Barco 9 months
    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.