Running Systemctl Inside Docker Container: A Comprehensive Guide
Introduction:
Docker has revolutionized the way we deploy and manage applications, providing a lightweight and scalable containerization solution. However, running systemd services inside a Docker container can be a bit challenging due to the differences in the container environment compared to a traditional Linux system.
In this guide, we will walk through the process of running systemctl inside a Docker container using CentOS as the base image. This allows you to manage system services and perform administrative tasks within the container, bridging the gap between traditional system administration and containerization.
- Base OS: Begin on your base operating system. If using Ubuntu AMI on AWS, ensure you have connectivity.

2. Connect to base OS: Here I have connected via EC2 instance Connect option.

3. Install Docker and verify: Install Docker on your host machine. Visit the official Docker website for installation instructions.
apt install docker.io -y # to install docker
systemctl start docker # to start the docker service
systemctl enable docker # to enable the docker service
docker info
# to check details (also to verify because if docker is not installed, this will not work)

4: Pull Ubuntu Docker Image: Pull the official centos Docker image from Docker Hub:
docker pull ubuntu:latest

verify using docker images

5: Run the Docker Container with Privileged Access
sudo docker run --privileged \
-v /run/systemd/system:/run/systemd/system \
-v /bin/systemctl:/bin/systemctl \
-v /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket \
-v /usr/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu \
-v /lib/systemd:/lib/systemd \
-it ubuntu:latest \
bash -c "ln -s /usr/lib/x86_64-linux-gnu/libtinfo.so.6 /usr/lib/x86_64-linux-gnu/libtinfo.so.5 && bash"
Here’s a comprehensive explanation of the command:
✥ sudo docker run: Launches a Docker container.
✥ — privileged: Grants extended privileges to the container, allowing it to access host devices and capabilities. Use with caution due to security implications.
✥ -v (Volume Mounts): Maps files and directories from the host to the container:
/run/systemd/system:/run/systemd/system
: Mounts the host's systemd runtime directory for container access./bin/systemctl:/bin/systemctl
: Mounts the host'ssystemctl
binary for use within the container./var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket
: Mounts the host's D-Bus socket for communication with systemd services./usr/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu
: Mounts the host's library directory for potential dependencies./lib/systemd:/lib/systemd
: Mounts the host's systemd library directory (adjust based on your Ubuntu version).
✥ -it ubuntu:latest: Runs an interactive terminal session in a container based on the ubuntu:latest
image.
-i
: Keep STDIN open even if not attached. It allows you to interact with the container.-t
: Allocate a pseudo-TTY (terminal). This is often used to keep an interactive shell session open.
✥ bash -c “ln -s … && bash”`: Executes a command within the container:
ln -s /usr/lib/x86_64-linux-gnu/libtinfo.so.6 /usr/lib/x86_64-linux-gnu/libtinfo.so.5
: Creates a symbolic link fromlibtinfo.so.6
tolibtinfo.so.5
to address missing library issues.&& bash
: Starts a Bash shell within the container after creating the link.
Key Points:
- Security: Use
--privileged
mode with caution and explore alternatives for production environments. - Complexity: Consider container-native service management methods for better maintainability.
- Compatibility: Ensure
libtinfo.so.6
is compatible withlibtinfo.so.5
in your context. - Troubleshooting: If errors persist, verify library paths and linking within the container using
ldd /bin/systemctl
. - Alternatives: Explore alternative base images, custom image building, or installing
libtinfo.so.5
within the container if needed.
6: Check systemctl Status Inside the Container
Verify that systemctl is working inside the container by checking its status:
systemctl status

This step ensures that systemd is operational within the container, allowing you to manage and monitor services as you would on a traditional Linux system.
Conclusion:
By following these steps, you’ve successfully set up a Docker container with systemd initialization, enabling you to run systemctl commands and manage system services within the container environment. This approach can be particularly useful for testing and development scenarios where you need to simulate a traditional Linux system within a containerized environment.
🙏 Thank You for Reading!
Your time and attention mean a lot to us. We greatly appreciate your engagement with our content and would love to hear your thoughts and ideas. Let’s stay connected and continue our journey through the digital world together! 🌐📚💡
Let’s Stay in Touch 🪂 Connect with us on: LinkedIn, Instagram