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