带有 Centos 7 和 systemd 的 Docker 容器

带有 Centos 7 和 systemd 的 Docker 容器

我正在尝试按照此处所述运行 centos+systemd Docker 容器https://hub.docker.com/_/centos/

  1. docker build --rm -t local/c7-systemd c7-systemd

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"]
  1. docker build --rm -t local/c7-systemd-httpd c7-systemd-httpd

Dockerfile:

    FROM local/c7-systemd
    RUN echo "myproxy" >> /etc/yum.conf
    RUN yum -y install httpd; yum clean all; systemctl enable httpd.service
    EXPOSE 80
    CMD ["/usr/sbin/init"]
  1. docker run -ti --cap-add SYS_ADMIN --security-opt seccomp:unconfined -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 local/c7-systemd /bin/bash

我也尝试过,--privileged但每次我得到这个:

    [root@e29ecfb082d8 /]# systemctl status
    Failed to get D-Bus connection: Operation not permitted

我在 Cygwin、Docker 版本 18.03.1-ce、内部版本 9ee9f40(Docker for Windows)中运行它。

您能否说一下是否有任何方法可以使用此配置获得工作的 centos7+systemd 容器?

答案1

我有一个工作容器https://hub.docker.com/r/centos/systemd/

  1. docker build --rm --no-cache -t c7-systemd-off c7-systemd-off

Dockerfile:

    FROM centos/systemd

    RUN echo "myproxy" >> /etc/yum.conf
    RUN yum -y install httpd; yum clean all; systemctl enable httpd.service

    EXPOSE 80

    CMD ["/usr/sbin/init"]
  1. docker run --privileged --name c7 -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 -d c7-systemd-off

  2. docker exec -it c7 /bin/bash

答案2

当你在容器中使用 systemd 运行服务,我同意类诺德的评论你不应该这样做。除非您需要 CentOS,否则您可以使用 Apache 的官方映像之一:

https://hub.docker.com/_/httpd

如果您需要 CentOS,Docker Hub 上也有:

https://hub.docker.com/r/centos/httpd-24-centos7

来自来源您可以看到 Red Hat 本身也没有使用 systemd 运行它:

FROM centos:centos7

# RHSCL httpd24 image.
#
# Volumes:
#  * /opt/rh/httpd24/root/var/www - Datastore for httpd
#  * /var/log/httpd24 - Storage for logs when $HTTPD_LOG_TO_VOLUME is set
# Environment:
#  * $HTTPD_LOG_TO_VOLUME (optional) - When set, httpd will log into /var/log/httpd24

EXPOSE 80
EXPOSE 443

COPY run-*.sh /usr/local/bin/
RUN mkdir -p /var/lib/httpd24
COPY contrib /var/lib/httpd24/

RUN rpmkeys --import file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 && \
    yum -y --setopt=tsflags=nodocs install https://www.softwarecollections.org/en/scls/rhscl/httpd24/epel-7-x86_64/download/rhscl-httpd24-epel-7-x86_64.noarch.rpm && \
    yum install -y --setopt=tsflags=nodocs gettext hostname bind-utils httpd24 httpd24-mod_ssl && \
    yum clean all

# When bash is started non-interactively, to run a shell script, for example it
# looks for this variable and source the content of this file. This will enable
# the SCL for all scripts without need to do 'scl enable'.
ENV BASH_ENV=/var/lib/httpd24/scl_enable \
    ENV=/var/lib/httpd24/scl_enable \
    PROMPT_COMMAND=". /var/lib/httpd24/scl_enable"


VOLUME ["/opt/rh/httpd24/root/var/www"]
VOLUME ["/var/log/httpd24"]

ENTRYPOINT ["/usr/local/bin/run-httpd24.sh"]
CMD ["httpd", "-DFOREGROUND"]

相关内容