解決Docker中CentOS鏡像無法使用systemd的問題
-
創建DockerFile
mkdir /root/centos-systemd cd /root/centos-systemd vim DockerFile FROM centos:7 ENV container docker RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \ rm -f /lib/systemd/system/multi-user.target.wants/*;\ rm -f /etc/systemd/system/*.wants/*;\ rm -f /lib/systemd/system/local-fs.target.wants/*; \ rm -f /lib/systemd/system/sockets.target.wants/*udev*; \ rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \ rm -f /lib/systemd/system/basic.target.wants/*;\ rm -f /lib/systemd/system/anaconda.target.wants/*; VOLUME [ "/sys/fs/cgroup" ] CMD ["/usr/sbin/init"] ESC :wq
-
下載鏡像並命名爲centos-systemd:7
cd /root/centos-systemd docker build --rm -t centos-systemd:7 .
-
通過centos-systemd:7鏡像生成一個名爲centos-systemd的容器
docker run -d -i -t --privileged=true --name centos-systemd -v /sys/fs/cgroup:/sys/fs/cgroup:ro centos-systemd:7
-
連接到名爲centos-systemd的容器
docker exec -i -t centos-systemd /bin/bash
-
測試systemd啓動是否成功
systemctl --help