以centos 7.2容器中运行httpd服务为例子,默认下直接运行
[[email protected] /]# systemctl start httpd.service Failed to get D-Bus connection: Operation not permitted
在网上找到的解决方法:
- 创建Dockerfile创建一个基础镜像
FROM centos:7.2 MAINTAINER [email protected] ENV container docker RUN yum -y update && yum clean all && 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"]
[[email protected] ~]# docker build -t centos_test_systemctl .
2.在上面镜像的基础上再构建apache镜像
FROM centos_test_systemctl MAINTAINER [email protected] RUN yum -y install httpd && yum clean all && systemctl enable httpd.service EXPOSE 80 CMD ["/usr/sbin/init"]
[[email protected] ~]# docker build -t centos_test_httpd .
3.然后就启动容器
[[email protected] ~]# docker run --privileged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 centos_test_httpd
时间: 2024-10-24 06:16:26