zl程序教程

您现在的位置是:首页 >  系统

当前栏目

centos7底层系统容器使用systemctl启动服务的方法

centos7方法容器系统服务 启动 底层 systemctl
2023-09-14 09:15:43 时间

centos7容器使用systemctl启动服务的方法

1.实现思路

思路:centos7想以systemctl命令启动,就需要开启init进程,init进程必须在系统启动的时候开启,作为第一个进程,init无法在脚本中启动,因此只能是将容器的启动命令设置成/usr/sbin/init,然后将启动服务的命令写成脚本,然后把执行脚本的命令写入/etc/rc.local中,这样就可以在centos7容器中使用systemctl启动服务了

这种方法特别适合那种喜欢把一个容器做成一个小的VMware虚拟机

以下操作都在容器中执行

2.编写服务启动脚本

1.写脚本
[root@e31b4d686cda ~]# vim /data/entrypoint.sh
#!/bin/bash
systemctl start jenkins
systemctl enable jenkins
tail -f /var/log/jenkins/jenkins.log
[root@e31b4d686cda ~]# chmod a+x /data/entrypoint.sh 

2.将启动脚本加到/etc/rc.local文件里,当系统启动时就执行此脚本
[root@e31b4d686cda ~]# vim /etc/rc.local
sh /data/entrypoint.sh

3.编写Dockerfile封装启动命令

[root@k8s-node2 ~/docker-jenki