Docker(带SYSTEMCTL的CentOS 7):无法挂载tmpfs& cgroup中

时间:2016-04-14 08:18:25

标签: docker mount centos7 systemd systemctl

(我是Docker初学者。然后我关注了CentOS-7的一些教程)

在我的CentOS 7.2中,我尝试按照以下步骤学习Docker。

# docker version

Client:
 Version:      1.10.3
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   20f81dd
 Built:        Thu Mar 10 15:39:25 2016
 OS/Arch:      linux/amd64

Server:
 Version:      1.10.3
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   20f81dd
 Built:        Thu Mar 10 15:39:25 2016
 OS/Arch:      linux/amd64

# docker pull centos:latest
# docker images
centos     latest    778a53015523    12 days ago    196.7 MB

# mkdir ~/docker/centos7-systemd
# cd ~/docker/centos7-systemd
# vi Dockerfile
FROM centos
MAINTAINER "XXXX XXXX" <xxxx@xxxx.com>
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"]

# docker build --rm -t local/centos7-systemd .
..
Successfully built 1a9f1c4938b3

# docker images
centos                  latest    778a53015523    12 days ago    196.7 MB
local/centos7-systemd   latest    1a9f1c4938b3    8 seconds ago  196.7 MB

所以到目前为止,一切(似乎)都没问题 现在,当我跑步时出现问题:

# docker run -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 local/centos7-systemd
Failed to mount tmpfs at /run: Operation not permitted
Failed to mount cgroup at /sys/fs/cgroup/systemd: Operation not permitted
[!!!!!!] Failed to mount API filesystems, freezing.

这甚至意味着什么,更重要的是,正在发生什么以及如何解决这个问题

谢谢大家:)

8 个答案:

答案 0 :(得分:17)

尝试以特权模式运行容器:

docker run -ti --privileged=true -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 local/centos7-systemd

这应该可以解决你的问题

答案 1 :(得分:4)

我跟着

docker run -ti --privileged=true -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 local/centos7-systemd

并提出

Failed to insert module 'autofs4'
Failed to mount cgroup at /sys/fs/cgroup/systemd: No such file or directory
systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)
Detected virtualization docker.
Detected architecture x86-64.

Welcome to CentOS Linux 7 (Core)!

Set hostname to <c7b8edb49c60>.
Initializing machine ID from random generator.
Cannot determine cgroup we are running in: No such file or directory
Failed to allocate manager object: No such file or directory
[!!!!!!] Failed to allocate manager object, freezing.

想知道它不适用于MacOS?

答案 2 :(得分:2)

我遇到了与Docker for Windows(1.12.3)相同的问题......

$ docker logs bareos
systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)
Detected virtualization docker.
Detected architecture x86-64.

Welcome to CentOS Linux 7 (Core)!

Set hostname to <bareos>.
Failed to install release agent, ignoring: No such file or directory
Failed to create root cgroup hierarchy: No such file or directory
Failed to allocate manager object: No such file or directory
[!!!!!!] Failed to allocate manager object, freezing.

最新的boot2docker没有systemd。如果主机没有,我们在Docker容器中不能有systemd。由于该重要文件夹为/sys/fs/cgroup/systemd

最后,我在基于Alpine Linux的VitualBox中创建了default vm,并使用default驱动程序创建了docker-machine generic

答案 3 :(得分:1)

As I said here, you are not forced tu use the --privileged=true parameter (which can be dangerous IMHO), you just forgot to add -v /run to your docker run command.

So your final run command which should work would be:

docker run -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro -v /run -p 80:80 local/centos7-systemd

答案 4 :(得分:1)

在丹尼尔·沃尔什(Daniel Walsh)贡献了一系列补丁之后,更现代的方法是……

docker run -ti --tmpfs /tmp --tmpfs /run -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 local/centos7-systemd

有关更多信息,请参见https://developers.redhat.com/blog/2016/09/13/running-systemd-in-a-non-privileged-container/

答案 5 :(得分:0)

MacOS X不需要在容器中装载cgroups卷

$docker run -it -p 80:80 ${ImageID}

运行多个容器实例后,My mac陷入

[!!!!!!] Failed to mount API filesystems, freezing.

reference 目前bash模式对我来说还不错

$docker run -it -p 80:80 ${ImageID} /bin/bash

答案 6 :(得分:0)

虽然您可以在容器中运行systemd,但我认为这不是一个好主意。原因如下:

  1.   

    Docker的设计理念是每个容器只有一个服务/进程。尽管它绝对支持在容器中运行多个进程,并且绝不会阻止您这样做,但是最终您将遇到容器中的多个服务与Docker或外部工具期望不完全对应的区域。转向服务扩展或跨主机使用Docker群之类的事情仅支持每个容器提供一项服务的概念。

    https://stackoverflow.com/a/46801406/399105

  2. 更具体地与此问题相关:

      

    我是Docker初学者。

    尽管有时可能需要在容器中运行systemd,但它需要一些解决方法(如您从其他答案中看到的那样),我不认为这对学习容器的人来说是一个很好的起点。 / p>

  3. blog post链接了一个如何使其工作的示例,但是您可以看到该容器仍需要以root用户身份运行:

    EXPOSE 80
    CMD [ "/sbin/init" ]
    

    尽管这当然不是这些容器独有的,但它的安全性仍然较低:

  4. 作为进一步证明这不是一个好主意的证据,即使Red Hat自己的CentOS Dockerfile也不会这样做。例如(source):

    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"]
    

当然,如果您了解所有这些并且仍然希望在容器中运行systemd,就像其他人已经提到的那样,则是可能的。我认为重复他们已经说过的话没有任何价值。

答案 7 :(得分:0)

如果您不需要run在前台的容器,则可以以分离模式启动它,以避免此错误。例如:

docker run -d --name=my_container_name image_id

然后,您可以使用类似的方法将外壳放入容器:

docker exec -ti my_container_name /bin/bash

如果您在Dockerfile CMD的前台没有命令,导致容器立即退出,则可以添加一个使它保持运行状态的命令,例如:

docker run -d --name=my_container_name image_id tail -f /dev/null

有关上一个示例的更多详细信息,请参见SO answer