Run systemctl inside a docker container
Docker containers typically don't have an init system like systemd, which is what systemctl interacts with. However, there are workarounds.

Why systemd is tricky in Docker
systemd is designed to be a system init manager for an entire operating system, and Docker containers are designed to run a single process. This mismatch makes running systemd inside Docker non-trivial. However, it is possible with some adjustments, and I will walk you through the process.
Prerequisites
Ensure you have Docker installed on your machine.
Familiarity with Docker commands.
Guide to Run systemd in an Ubuntu Docker Container
- Create
Dockerfileis required like below:
FROM ubuntu:22.04
RUN echo 'root:root' | chpasswd
RUN printf '#!/bin/sh\nexit 0' > /usr/sbin/policy-rc.d
RUN apt-get update
RUN apt-get install -y systemd systemd-sysv dbus dbus-user-session
RUN printf "systemctl start systemd-logind" >> /etc/profile
ENTRYPOINT ["/sbin/init"]
/sbin/init is important to init systemd and enable systemctl.
/sbin/init is important to init systemd and enable systemctl.
- Then build the system.
docker build -t chinhnd/ubuntu-systemd -f Dockerfile .
docker run -it --privileged --cap-add=ALL chinhnd/ubuntu-systemd
Access the running container
Now that your container is running systemd, you can access it and use systemctl inside the container.
Enter the container:
docker exec -it <container_id> bashReplace
<container_id>with the actual container ID from thedocker psoutput.Then log in with root/root.
Check if
systemdis running:Inside the container, run:
systemctlIf everything is set up correctly, you should see the output from
systemctlshowing the system services running.



