以centos 7.2容器中运行httpd服务为例子,默认下直接运行

[root@b0b2a3ed40d6 /]# systemctl start httpd.service
Failed to get D-Bus connection: Operation not permitted

在网上找到的解决方法:

  1. 创建Dockerfile创建一个基础镜像

FROM  centos:7.2 
MAINTAINER hpsfpcca@163.com

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"]
[root@dev-ops ~]# docker build -t centos_test_systemctl .

2.在上面镜像的基础上再构建apache镜像

FROM centos_test_systemctl
MAINTAINER hpsfpcca@163.com

RUN yum -y install httpd && \
    yum clean all && \
    systemctl enable httpd.service

EXPOSE 80
CMD ["/usr/sbin/init"]
[root@dev-ops ~]# docker build -t centos_test_httpd .


3.然后就启动容器

[root@dev-ops ~]# docker run --privileged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 centos_test_httpd