75

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?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Abdol Seed
  • 1,227
  • 1
  • 14
  • 23
  • https://stackoverflow.com/questions/52197246/system-has-not-been-booted-with-systemd-as-init-system-pid-1-cant-operate – Srini M Dec 24 '19 at 09:10
  • 3
    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](https://docs.docker.com/get-started/part2/) tutorial walks through a basic standard use case. – David Maze Dec 24 '19 at 11:08
  • 1
    Does this answer your question? [How do I run a Docker container that uses SystemD from the latest version of Ubuntu (18.10)?](https://stackoverflow.com/questions/53750952/how-do-i-run-a-docker-container-that-uses-systemd-from-the-latest-version-of-ubu) – Bruno Wego Jan 10 '20 at 19:41

9 Answers9

28

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
Javier Gonzalez
  • 399
  • 4
  • 10
19

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
Felipe Augusto
  • 1,341
  • 1
  • 16
  • 18
  • 1
    +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 '23 at 18:13
  • 1
    Getting this error: `grpc: addrConn.createTransport failed to connect to {unix:///var/run/docker/containerd/containerd.sock 0 }. 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 '23 at 22:41
15

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

I have a similar problem and it solves it.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
15

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

docker run -itd --privileged docker pull ubuntu:18.04 /usr/sbin/init
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Rounak Jain
  • 181
  • 1
  • 6
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

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Barco
  • 11
  • 3
  • 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 01 '22 at 08:58
0

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"]
C. Flor
  • 588
  • 5
  • 6
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.
shdr
  • 868
  • 10
  • 25
-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
-5

You may have forgotten to start docker before using it

 sudo service docker start