首页 > 系统相关 >二进制安装docker的shell脚本和python脚本

二进制安装docker的shell脚本和python脚本

时间:2022-11-20 18:23:30浏览次数:51  
标签:脚本 shell infinity service python containerd system Path docker

1. python脚本

点击查看代码
#!/bin/python
import os
from pathlib import Path
import tarfile
import shutil
import grp

def AddGroup():
    group_name = "docker"
    all_groups = grp.getgrall()
    for i in all_groups:
        if i[0].startswith(group_name):
            print("{} 组已经存在".format(group_name))
        else:
            os.system("groupadd -r {}".format(group_name))
            break
def Unpackage():
    p = Path('/usr/local/src/docker-19.03.15.tgz')
    with tarfile.open(p, "r", encoding="utf-8") as tf:
        tf.extractall(p.parent)
    os.remove(p)


def Copy():
    src_dir = Path('/usr/local/src/docker')
    dest_dir = Path('/usr/bin')
    file_list = os.listdir(src_dir)
    for file in file_list:
        file_name = Path(src_dir, file)
        shutil.copy(file_name, dest_dir)


def EditContainerdServices():
    filename = Path('/lib/systemd/system/containerd.service')
    with open(filename, 'w', encoding='utf-8') as f:
        f.write('[Unit]\n'
                'After=network.target local-fs.target\n'
                '[Service]\n'
                'ExecStartPre=-/sbin/modprobe overlay\n'
                'ExecStart=/usr/bin/containerd\n'
                'Type=notify\n'
                'Delegate=yes\n'
                'KillMode=process\n'
                'Restart=always\n'
                'RestartSec=5\n'
                'LimitNPROC=infinity\n'
                'LimitCORE=infinity\n'
                'LimitNOFILE=infinity\n'
                'TasksMax=infinity\n'
                'OOMScoreAdjust=-999\n'
                '[Install]\n'
                'WantedBy=multi-user.target\n')


def EditDcokerSocketServices():
    filename = Path('/lib/systemd/system/docker.socket')
    with open(filename, 'w', encoding='utf-8') as f:
        f.write('[Unit]\n'
                'PartOf=docker.service\n'
                '[Socket]\n'
                'ListenStream=/var/run/docker.sock\n'
                'SocketMode=0660\n'
                'SocketUser=root\n'
                'SocketGroup=docker\n'
                '[Install]\n'
                'WantedBy=sockets.target')


def EditDcokerServices():
    filename = Path('/lib/systemd/system/docker.service')
    with open(filename, 'w', encoding='utf-8') as f:
        f.write('[Unit]\n'
                'BindsTo=containerd.service\n'
                'After=network-online.target firewalld.service containerd.service\n'
                'Wants=network-online.target\n'
                'Requires=docker.socket\n'
                '[Service]\n'
                'Type=notify\n'
                'ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock\n'
                'ExecReload=/bin/kill -s HUP $MAINPID\n'
                'TimeoutSec=0\n'
                'RestartSec=2\n'
                'Restart=always\n'
                'StartLimitBurst=3\n'
                'StartLimitInterval=60s\n'
                'LimitNOFILE=infinity\n'
                'LimitNPROC=infinity\n'
                'LimitCORE=infinity\n'
                'TasksMax=infinity\n'
                'Delegate=yes\n'
                'KillMode=process\n'
                '[Install]\n'
                'WantedBy=multi-user.target\n')


def StartService():
    os.system(
        'systemctl daemon-reload \
        && systemctl start containerd && systemctl enable containerd \
        && systemctl start docker && systemctl enable docker \
        && systemctl start docker.socket')


if __name__ == '__main__':
    AddGroup()
    Unpackage()
    Copy()
    EditContainerdServices()
    EditDcokerSocketServices()
    EditDcokerServices()
    StartService()

2. shell脚本

点击查看代码
#!/bin/bash
function Addgroup(){
        group=docker
        egrep "^$group" /etc/group > /dev/null
        if [ $? -ne 0 ]
        then
             groupadd -r docker
        fi
}

function Unpackage(){
	cd /usr/local/src && tar xf docker-19.03.15.tgz && cp docker/* /usr/bin/
}

function EditContainerdServices(){
cat > /lib/systemd/system/containerd.service << EOF
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target

[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/bin/containerd

Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
TasksMax=infinity
OOMScoreAdjust=-999

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && systemctl start containerd && systemctl enable containerd
}

sleep 5

function EditDcokerSocketServices(){
cat > /lib/systemd/system/docker.socket << EOF
[Unit]
Description=Docker Socket for the API
PartOf=docker.service
[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target
EOF
}

function EditDcokerServices(){
cat > /lib/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target
EOF
}

function StartService(){
  systemctl daemon-reload && systemctl start docker && systemctl enable docker && systemctl start docker.socket
}
main(){
  Addgroup
  Unpackage
  EditContainerdServices
  EditDcokerSocketServices
  EditDcokerServices
  StartService
}

main

标签:脚本,shell,infinity,service,python,containerd,system,Path,docker
From: https://www.cnblogs.com/yufenchi/p/16909131.html

相关文章

  • Java中使用脚本引擎运行脚本语言
    在Java中运行脚本语言,例如JavaScript。步骤:1、创建脚本引擎管理器ScriptEngineManager2、从管理器中获取一个引擎ScriptEngine3、通过put(key,valu......
  • PYTHON_pandas
    1.读写文件importpandasaspddata=pd.read_excel('C:/.../stock.xlsx')data2=pd.read_csv('C:/.../stock.csv')2.展示前n行data.head(5)3.基本信息data......
  • 分享一个连接远端计算机与传输文件的脚本
    分享一个连接远端计算机与传输文件的脚本用了一个月超算,由于本地是linux系统,需要用到ssh命令连接远端计算机,需要用到scp命令进行文件传输。但是连接远端的命令太复杂,于是......
  • python进程
    今日内容概要同步与异步阻塞与非阻塞创建进程的多种方式进程join方法进程间数据隔离进程间通信之IPC机制进程对象诸多方法生产者消费者模型互斥锁今日内容......
  • pywin32-自动点击脚本
    pywin32-自动点击脚本参考资料:https://nymrli.top/2018/08/31/python-win32api-win32gui-win32con-PyUserInput实现自动化脚本/pywin32操作及句柄的......
  • Python单行代码
    1一行For循环#For循环在一行mylist=[200,300,400,500]#正常方式result=[]forxinmylist:ifx>250:result.append(x)prin......
  • PYTHON 安装后没有pip命令
        python安装后没有pip命令解决方法:windows任一文件中,按SHIFT,右击,选择“在此处打开powershell"输入命令:python-mensurepip......
  • python中的拷贝问题
    分为:赋值、浅拷贝、深拷贝deepcopy需要导入copy模块    而numpy中的解释不一样:https://www.numpy.org.cn/user/quickstart.html#%E5%AE%8C%E5%85%A8%E4%B8%8D......
  • python可视化——matplotlib画图颜色控制
    个人认为这篇文章介绍的非常详细,值得推荐python可视化——matplotlib画图颜色控制_CD_Don的博客-CSDN博客......
  • 【Unity3D】Unity 脚本 ③ ( C# 脚本的执行入口函数 | 获取当前游戏物体及物体名称 |
    文章目录​​一、C#脚本的执行入口函数​​​​二、获取当前游戏物体及物体名称​​​​三、获取游戏物体的Transform组件数据​​​​四、UnityEngine命名空间简介......