Docker 中使用 CentOS7 systemd 报错解决

分享 青牛 ⋅ 于 2016-12-04 16:30:44 ⋅ 13762 阅读

在docker中使用 CentOS7 官方镜像的时候,使用Systemd 的话会报错:

➜  ~ docker run -it centos:7 /bin/bash
[root@ead8524a303e /]# systemctl list-units
Failed to get D-Bus connection: Operation not permitted
[root@ead8524a303e /]# exit

# 加上权限参数也不行
➜  ~ docker run -it --privileged centos:7 /bin/bash
[root@fd105049c04c /]# systemctl list-units
Failed to get D-Bus connection: Operation not permitted

查了一下 官方的文档说明https://hub.docker.com/_/centos/,需要使用/usr/sbin/init 初始化才能使用Systemd服务,因为好多服务没启动。

测试了一下,分享一下我的Dockerfile:

FROM centos:7
MAINTAINER "sandy" <sandy@hainiubl.com>
ENV container docker
RUN echo 'root:hainiu' | chpasswd

RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done)
RUN 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/*;
CMD ["/usr/sbin/init"]

build的一下:

docker build --rm -t hainiubl/centos7 .

再运行一下试试:

~ docker run -it --privileged --hostname master.hadoop hainiubl/centos7

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 <master.hadoop>.
Initializing machine ID from random generator.
Failed to install release agent, ignoring: No such file or directory
[  OK  ] Reached target Paths.
[  OK  ] Reached target Swap.
[  OK  ] Reached target Local File Systems.
[  OK  ] Created slice Root Slice.
[  OK  ] Created slice System Slice.
[  OK  ] Created slice User and Session Slice.
[  OK  ] Reached target Slices.
[  OK  ] Listening on Journal Socket.
         Starting Create Volatile Files and Directories...
[  OK  ] Listening on Delayed Shutdown Socket.
         Starting Journal Service...
[  OK  ] Started Create Volatile Files and Directories.
[ INFO ] Update UTMP about System Boot/Shutdown is not active.
[DEPEND] Dependency failed for Update UTMP about System Runlevel Changes.
Job systemd-update-utmp-runlevel.service/start failed with result 'dependency'.
[  OK  ] Started Journal Service.
[  OK  ] Reached target System Initialization.
[  OK  ] Reached target Timers.
[  OK  ] Listening on D-Bus System Message Bus Socket.
[  OK  ] Reached target Sockets.
[  OK  ] Reached target Basic System.
         Starting Permit User Sessions...
[  OK  ] Started D-Bus System Message Bus.
         Starting D-Bus System Message Bus...
         Starting Login Service...
[  OK  ] Started Permit User Sessions.
         Starting Cleanup of Temporary Directories...
[  OK  ] Started Console Getty.
         Starting Console Getty...
[  OK  ] Reached target Login Prompts.
[  OK  ] Started Login Service.
[  OK  ] Reached target Multi-User System.
[  OK  ] Started Cleanup of Temporary Directories.

CentOS Linux 7 (Core)
Kernel 4.4.27-moby on an x86_64

master login: root
Password:
[root@master ~]#
[root@master ~]# systemctl list-units
UNIT                      LOAD   ACTIVE     SUB       DESCRIPTION
dev-vda2.device           loaded activating tentative /dev/vda2
-.mount                   loaded active     mounted   /
dev-mqueue.mount          loaded active     mounted   POSIX Message Queue File S
etc-hostname.mount        loaded active     mounted   /etc/hostname
etc-hosts.mount           loaded active     mounted   /etc/hosts
etc-resolv.conf.mount     loaded active     mounted   /etc/resolv.conf
systemd-ask-password-wall.path loaded active     waiting   Forward Password Requ
ests

......

这样就可以了,很多情况下使用docker其实用不到Systemd,docker推崇单服务容器。

版权声明:原创作品,允许转载,转载时务必以超链接的形式表明出处和作者信息。否则将追究法律责任。来自海汼部落-青牛,http://hainiubl.com/topics/1
点赞
成为第一个点赞的人吧 :bowtie:
回复数量: 0
    暂无评论~~
    • 请注意单词拼写,以及中英文排版,参考此页
    • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
    • 支持表情,可用Emoji的自动补全, 在输入的时候只需要 ":" 就可以自动提示了 :metal: :point_right: 表情列表 :star: :sparkles:
    • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif,教程
    • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
    Ctrl+Enter