43

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?

vinzee
  • 17,022
  • 14
  • 42
  • 60
Abdol Seed
  • 877
  • 1
  • 10
  • 21
  • 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
  • 2
    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

6 Answers6

23

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
  • 341
  • 2
  • 10
12

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
  • 957
  • 11
  • 14
8

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

I have a similar problem and it solves it.

vinzee
  • 17,022
  • 14
  • 42
  • 60
8

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

docker run -itd --privileged docker pull ubuntu:18.04 /usr/sbin/init
vinzee
  • 17,022
  • 14
  • 42
  • 60
Rounak Jain
  • 101
  • 1
  • 4
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

vinzee
  • 17,022
  • 14
  • 42
  • 60
Barco
  • 11
  • 2
  • 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
-2

Try sudo service ssh start and follow the rabbit.

Samuel Nwaokoro
  • 166
  • 1
  • 10