Centos7解决systemctl无法使用

root
233
文章
0
评论
2020年5月18日16:45:35 评论 3626字阅读12分5秒

systemd整合:

因为systemd要求CAPSYSADMIN授权,从而得到了读取到宿主机cgroup的能力,Centos7中已经用fakesystemd代替了systemd来解决依赖问题。如果仍然希望使用systemd,可用参考下面的Dockerfile:

mkdir systemd/
vim dockerfile

# systemd_centos7
FROM centos:7
ENV container docker ##环境
RUN yum -y swap -- remove fakesystemd -- install systemd systemd-libs
RUN yum -y ipdate; yum clean all; \
(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"]

# systemd_centos7
FROM centos:7
ENV container docker ##环境
RUN yum -y swap -- remove fakesystemd -- install systemd systemd-libs
RUN yum -y ipdate; yum clean all; \
(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"]

[root@hwf systemd]# docker build -t "sys_cen7" . ##创建镜像

[root@hwf systemd]# docker build -t "sys_cen7" .
Sending build context to Docker daemon   2.56kB
Step 1/6 : FROM centos:7
 ---> b5b4d78bc90c
Step 2/6 : ENV container docker ##环境
 ---> Using cache
 ---> 8d7308d9a749
Step 3/6 : RUN yum -y swap -- remove fakesystemd -- install systemd systemd-libs
 ---> Running in ac776d9b3ade
Loaded plugins: fastestmirror, ovl
No Match for argument: fakesystemd
swap remove fakesystemd
Removing intermediate container ac776d9b3ade
 ---> 332338bcc8de
Step 4/6 : RUN yum -y ipdate; yum clean all; (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/*;
 ---> Running in 7e3e0c23ece8
Loaded plugins: fastestmirror, ovl
No such command: ipdate. Please use /usr/bin/yum --help
Loaded plugins: fastestmirror, ovl
Cleaning repos: base extras updates
Removing intermediate container 7e3e0c23ece8
 ---> 1a7433e1a4eb
Step 5/6 : VOLUME [ "/sys/fs/cgroup" ]
 ---> Running in 48fd11b7429a
Removing intermediate container 48fd11b7429a
 ---> 92bfddc0fa36
Step 6/6 : CMD ["/usr/sbin/init"]
 ---> Running in bf744c04a978
Removing intermediate container bf744c04a978
 ---> e9a3fe778a78
Successfully built e9a3fe778a78
Successfully tagged sys_cen7:latest

在这个镜像的基础上写一个httpd的dockerfile进行测试

mkdir httpd
vim dockerfile
# centos7_systemd_httpd
FROM sys_cen7:latest
RUN yum -y install httpd
EXPOSE 80
CMD ["/usr/sbin/init"]

制作镜像,登陆进去进行验证

[root@hwf httpd]# docker build -t "http_sys" .

启动容器

--privileged 授权参数必须加, -v /sys/fs/cgroup/:/sys/fs/cgroup:ro 参数必须加

[root@hwf httpd]# docker container run --privileged -it -v /sys/fs/cgroup/:/sys/fs/cgroup:ro -p 8003:80 http_sys:latest

[root@hwf httpd]# docker container run --privileged -it -v /sys/fs/cgroup/:/sys/fs/cgroup:ro -p 8003:80 http_sys:latest

这里会卡主,然后退出容器,不要关闭,用exec连接进去

[root@hwf httpd]# docker container exec -it 9d514ce07b08 bash

进行验证

[root@9d514ce07b08 /]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)

 

 

继续阅读
weinxin
我的微信
这是我的微信扫一扫
  • 文本由 发表于 2020年5月18日16:45:35
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
Harbor镜像仓库 Docker

Harbor镜像仓库

Harbor镜像仓库 一、Harbor特性介绍 基于角色访问控制 每个人角色不同,需求也不同,因此就需要访问权限控制,根据角色分配相应的权限。例如,开发人员需要对项目构建这就需要用到读写权限(push...
Dockaer网络 Docker

Dockaer网络

Dockaer网络 docker本地网络类型 查看本地支持网络类型 docker network ls # docker network ls NETWORK ID NAME DRIVER SCOPE...
Dockerfile练习 Docker

Dockerfile练习

Dockerfile练习 实验一:要求使用centos7基础镜像,搭建httpd+sshd+php的镜像,实现httpd与sshd服务能够正常访问 创建存放dockerfile的目录 mkdir -p...
Docker-compose Docker

Docker-compose

Docker-compose 任务编排介绍 场景: 我们在工作中未来完成业务目标,首先把业务拆分成多个子任务,然后对这些子任务进行顺序组合,当子任务按照方案执行完毕后,就完成了业务目标 任务编排,就是...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: