在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推崇单服务容器。