主要解决systemctl命令在docker构建centos7镜像失效的问题
-主要报错有Failed to get D-Bus connection: Operation not permitted
-Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running
官方文档说在centos7.2以后版本会解决这个问题,但是没有默认开启,需要手动制作,而且docker 默认下载的是最新镜像1810 cat /etc/redhat-release可查
1.先构建一个支持systemd的centos7镜像Dockerfile如下
FROM centos:latest
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/*;
CMD ["/usr/sbin/init"]
创建的镜像名称为basecentos:1.0
2.构建docker中centos7环境安装docker的Dockerfile如下
FROM basecentos:1.0
MAINTAINER libaojia
RUN yum install -y yum-utils device-mapper-persistent-data lvm2
RUN yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
RUN yum install -y docker-ce
RUN systemctl enable docker
CMD ["/usr/sbin/init"]
注意,一定要用systemctl enable 服务名,不能用systemctl start 服务名,虽然基础镜像支持systemd,但是systemctl start会构建失败,enable也会开启服务并且开机自启
这样构建yum某些包的时候还是有Failed to get D-Bus connection: Operation not permitted但不影响
3.运行镜像docker run --privileged -tid 镜像id /usr/sbin/init
4.进入容器docker exec -it 容器id bash
5.docker version; docker info ;systemctl status docler无误即可
[root@0265f84f0b1d /]# docker version
Client:
Version: 18.09.3
API version: 1.39
Go version: go1.10.8
Git commit: 774a1f4
Built: Thu Feb 28 06:33:21 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.3
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 774a1f4
Built: Thu Feb 28 06:02:24 2019
OS/Arch: linux/amd64
Experimental: false
[root@0265f84f0b1d /]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 18.09.3
Storage Driver: vfs
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: e6b3f5632f50dbc4e9cb6288d911bf4f5e95b18e
runc version: 6635b4f0c6af3810594d2770f662f34ddc15b40d
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.5.1.el7.x86_64
Operating System: CentOS Linux 7 (Core) (containerized)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.777GiB
Name: 0265f84f0b1d
ID: HZTQ:RZ7V:YASA:NZUV:GEPD:UUTL:R45I:KXAE:IDPC:P7OV:CE52:ZEQU
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
[root@0265f84f0b1d /]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2019-03-18 15:44:34 UTC; 52s ago
Docs: https://docs.docker.com
Main PID: 26 (dockerd)
Tasks: 13
Memory: 33.4M
CGroup: /docker/0265f84f0b1d87c1882256ab72654a011672c6dc5022516ddf27d95279539b6c/system.slice/docker.service
└─26 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
‣ 26 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Mar 18 15:44:33 0265f84f0b1d dockerd[26]: time="2019-03-18T15:44:33.916484928Z" level=info msg="Loading containers: start."
Mar 18 15:44:33 0265f84f0b1d dockerd[26]: time="2019-03-18T15:44:33.918422064Z" level=warning msg="Running modprobe ...tus 1"
Mar 18 15:44:33 0265f84f0b1d dockerd[26]: time="2019-03-18T15:44:33.920687312Z" level=warning msg="Running modprobe ...tus 1"
Mar 18 15:44:33 0265f84f0b1d dockerd[26]: time="2019-03-18T15:44:33.922394600Z" level=warning msg="Running modprobe ...tus 1"
Mar 18 15:44:34 0265f84f0b1d dockerd[26]: time="2019-03-18T15:44:34.023614987Z" level=info msg="Default bridge (dock...dress"
Mar 18 15:44:34 0265f84f0b1d dockerd[26]: time="2019-03-18T15:44:34.080245396Z" level=info msg="Loading containers: done."
Mar 18 15:44:34 0265f84f0b1d dockerd[26]: time="2019-03-18T15:44:34.108745258Z" level=info msg="Docker daemon" commi...8.09.3
Mar 18 15:44:34 0265f84f0b1d dockerd[26]: time="2019-03-18T15:44:34.108916830Z" level=info msg="Daemon has completed...ation"
Mar 18 15:44:34 0265f84f0b1d systemd[1]: Started Docker Application Container Engine.
Mar 18 15:44:34 0265f84f0b1d dockerd[26]: time="2019-03-18T15:44:34.119937142Z" level=info msg="API listen on /var/r....sock"
Hint: Some lines were ellipsized, use -l to show in full.
参考文档docker Hub官网
Failed to get D-Bus connection: Operation not permitted
网友评论