Docker System has not been booted with systemd as init system
Asked Answered
G

9

86

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?

Grandfather answered 24/12, 2019 at 8:57 Comment(3)
#52197746Heptahedron
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.Mollie
Does this answer your question? How do I run a Docker container that uses SystemD from the latest version of Ubuntu (18.10)?Dainedainty
D
32

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
Dyaus answered 20/6, 2020 at 17:8 Comment(1)
docker run --privileged ... didn't help me :(Meloniemelony
P
21

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
Prana answered 11/6, 2020 at 5:46 Comment(3)
+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.Succussion
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 mountedRhymester
In my particular case I ran sudo dockerd and everything worked correctly.Vitek
S
19

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

docker run -itd --privileged docker pull ubuntu:18.04 /usr/sbin/init
Sizzle answered 13/2, 2021 at 6:4 Comment(2)
you are duplicating what Javier Gonzalez already said in June 2020.. ;-(Marylinmarylinda
Nope, he didn't mention /usr/sbin/init, his answer was not enough. This answer is doing it.Errant
Y
17

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

I have a similar problem and it solves it.

Yser answered 21/2, 2020 at 8:53 Comment(1)
this is what was required for nginx on Debian 10Muskellunge
R
4

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"]
Rickeyricki answered 14/11, 2022 at 23:53 Comment(1)
Corrected link - developers.redhat.com/blog/2014/05/05/…Marketable
P
0

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

Paramagnetic answered 31/1, 2022 at 17:22 Comment(1)
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.Paramagnetic
N
0

If all above doesn't solve, it could be that you use wsl on vm, the you need to recheck you enabled Hardwere Virtualization:

  1. Connect to the VM manager host using the Client or Web Client.
  2. Shut down or power off the virtual machine for which you want to enable virtualization.
  3. Right-click on the virtual machine and select "Edit Settings" or "Virtual Machine Settings."
  4. In the Virtual Machine Settings window, navigate to the "VM Options" or "CPU" section.
  5. Look for an option related to virtualization, such as "Hardware virtualization" or "Expose hardware-assisted virtualization to the guest operating system."
  6. Enable the virtualization option if it's not already enabled.
  7. Save the virtual machine settings.
  8. Power on the virtual machine.
Nassir answered 17/5, 2023 at 20:10 Comment(0)
E
-2
sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig

sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target

exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME
Eggcup answered 17/8, 2023 at 9:41 Comment(0)
B
-7

You may have forgotten to start docker before using it

 sudo service docker start
Bicameral answered 1/11, 2022 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.