Finally systemd is running inside RedHat / CentOS Containers

Abdennour Toumi
2 min readOct 11, 2019

--

RHEL Sample

docker run \
--rm \
--name rhel8 \
--privileged \
-v /sys/fs/cgroup:/sys/fs/cgroup:ro \
--tmpfs /run \
-p 8080:80 \
-d \
registry.access.redhat.com/ubi8/ubi-init

Check it’s up docker ps | grep rhel8

Install Web Server

docker exec -it rhel8 curl -o /etc/yum.repos.d/nginx.repo https://gist.githubusercontent.com/abdennour/2e1360aabd0c9af04c6b02c2d1208ca1/raw/eaee9dffd6cfc862339bbbc5a2642c82a1404010/nginx.repo
# If you don't have subscription-manager registration, run above command
docker exec -it rhel8 yum update -y
docker exec -it rhel8 yum install nginx -y
docker exec -it rhel8 systemctl start nginx

Validate

Go to your browser http://localhost:8080

Congrats 🎉🎊 !!!

docker exec -it rhel8 systemctl enable nginx is useful to auto-start nginx after restarting the container (docker restart rhel8 )

Cleanup

docker rm -f rhel8

Centos Sample

docker run \
--rm \
--name centos7 \
--privileged \
-v /sys/fs/cgroup:/sys/fs/cgroup:ro \
-p 8080:80 \
-d \
centos/systemd

Check it’s up docker ps | grep centos7

Install Web Server

docker exec -it centos7 yum update -y
docker exec -it centos7 yum install nginx -y
docker exec -it centos7 systemctl start nginx

Validate

Go to your browser http://localhost:8080 .

Congrats 🎉🎊 !!!

docker exec -it centos7 systemctl enable nginx is useful to auto-start nginx after restarting the container (docker restart centos7 )

Cleanup

docker rm -f centos7

Force it to Work !😎

You may feel like this 😩 if it does not work.

Thus, you can add these options:

--tmpfs /run \--tmpfs /run/lock \--tmpfs /tmp \--cap-add SYS_ADMIN \

🎉🎉🎉🎉🎉🎉🎉🎉 I am now able to spin up a full staging environment of containers for my Ansible playbooks and point my inventory to them.

--

--

Abdennour Toumi

Software engineer, Cloud Architect, 5/5 AWS|GCP|PSM Certified, Owner of kubernetes.tn