在docker的centos里启用systemd

最后编辑:2 周

[MD]

1. 拉取镜像[1]

docker pull centos

2. 创建一个DockerFile文件

vi mydockerfile
# 以下是mydockerfile文件的内容
FROM centos:7
MAINTAINER "Yourname" <[email protected]>
ENV container docker
RUN yum -y update; yum clean all
RUN yum -y install systemd; yum clean all; \
(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"]
# mydockerfile内容结束

3. 将centos镜像打包为新的centos7-systemd镜像

docker build -t centos7-systemd - < mydockerfile

4. 创建并运行docker容器centos7[2]

docker run --name centos7 --net=host --privileged=true -d -e container=docker  -v /sys/fs/cgroup:/sys/fs/cgroup  centos7-systemd

  1. 参考资料:https://serverfault.com/questions/824975/failed-to-get-d-bus-connection-operation-not-permitted ↩︎

  2. 原文为docker run --rm --privileged -ti -e container=docker -v /sys/fs/cgroup:/sys/fs/cgroup centos7-systemd /usr/sbin/init ↩︎