Run systemd Inside Docker: Turn Your Container into a Full‑Featured Linux Host
This guide explains how to configure a Docker image to run systemd, enabling a lightweight container to behave like a full Linux system for testing, service development, and learning, while covering Dockerfile setup, build/run commands, service management, and security considerations.
Introduction
When developing or testing, a clean system environment is often required. Virtual machines provide isolation but are heavy; Docker containers are lightweight but normally run a single process and lack an init system like systemd. This guide shows how to configure a Docker image to run systemd, enabling the container to behave like a full Linux system.
Dockerfile Configuration
The following Dockerfile builds an Ubuntu‑based image with systemd and common tools.
FROM ubuntu:latest
RUN apt-get update && apt-get install -y systemd net-tools iproute2 inetutils-ping ca-certificates netcat openssh-server
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN echo root:1|chpasswd
CMD ["/lib/systemd/systemd"] FROM ubuntu:latest: base image. RUN apt-get update && apt-get install -y …: installs systemd and utilities. RUN sed -i … /etc/ssh/sshd_config: enables root password login. RUN echo root:1|chpasswd: sets root password to “1”. CMD ["/lib/systemd/systemd"]: runs systemd as PID 1.
Build and Run the Container
Save the Dockerfile as Dockerfile.systemd and build:
docker build -t my-systemd-ubuntu -f Dockerfile.systemd .Run the image in privileged mode and bind the host cgroup directory:
docker run -d --privileged -v /sys/fs/cgroup:/sys/fs/cgroup --name my-systemd-container my-systemd-ubuntu --privileged: grants the container permission to manage cgroups. -v /sys/fs/cgroup:/sys/fs/cgroup: mounts the host cgroup hierarchy. --name my-systemd-container: assigns a friendly name.
Install and Operate Services Inside the Container
Enter the container: docker exec -it my-systemd-container bash Systemd starts services such as SSHD automatically. You can also start or check them manually:
systemctl start sshd
systemctl status sshdObtain the container’s IP address and connect via SSH from the host:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-systemd-container
ssh [email protected] # password: 1Use Cases
System testing : quickly spin up an isolated Linux environment.
Service development & debugging : work on systemd‑dependent services without a full VM.
Learning & experimentation : practice Linux administration in a lightweight container.
Considerations
Security : enabling root password login is unsafe for production; use keys or strong passwords.
Resource management : multiple services still consume CPU and memory; allocate resources wisely.
Network configuration : the default bridge network may require port mapping or host mode for external access.
Conclusion
Running systemd inside a Docker container provides a lightweight yet fully functional Linux environment suitable for testing, development, and learning, combining the flexibility of virtual machines with the efficiency of containers.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
