Linux 离线安装Docker(脚本自动安装)

[toc]

Linux 离线安装Docker(脚本自动安装) - 20211017

前言

关于服务器不能联网的情况,这样就没法用yum安装软件,docker也是如此;或者由于CentOS系统采用Yum 安装过程中很慢,不换镜像源的情况下

1. 准备docker离线包

docker官方离线包下载地址 - https://download.docker.com/linux/static/stable/x86_64/ 下载需要安装的docker版本,我此次下载的是 docker-20.10.9.tgz 版本

2. 准备docker.service 系统配置文件

docker.service

--- Linux都可以用-----
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target


----- Ubuntu 默认配置- apt install docker.io 安装 -------
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket
Wants=containerd.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target

3. 准备安装脚本和卸载脚本

安装脚本 install.sh

默认路径: /usr/lib/systemd/system/docker.service

手动安装建议也用这个路径,方便查看

vim install.sh

#!/bin/sh
echo '解压tar包...'
tar -xvf $1

echo '将docker目录移到/usr/bin目录下...'
cp docker/* /usr/bin/

echo '将docker.service 移到/etc/systemd/system/ 目录...'
cp docker.service /usr/lib/systemd/system/

echo '添加文件权限...'
chmod +x /usr/lib/systemd/system/docker.service

echo '重新加载配置文件...'
systemctl daemon-reload

echo '启动docker...'
systemctl start docker

echo '设置开机自启...'
systemctl enable docker.service

echo 'docker安装成功...'
docker -v

卸载脚本 uninstall.sh

vim uninstall.sh


#!/bin/sh

echo '删除docker.service...'
rm -f /usr/lib/systemd/system/docker.service

echo '删除docker文件...'
rm -rf /usr/bin/docker*

echo '重新加载配置文件'
systemctl daemon-reload

echo '卸载成功...'


4. 安装

4.1、此时目录为:(只需要关注docker-20.10.9.tgz、docker.service、install.sh、uninstall.sh即可)

4.2、执行脚本 sh install.sh docker-20.10.9.tgz

待脚本执行完毕后,执行docker -v

发现此时docker已安装成功,可以用docker --help

查看docker命令,从现在开始你就可以自己安装image和container了

4.3 如果你想卸载docker,此时执行脚本 sh uninstall.sh即可,执行过程如下:

此时输入docker -v,发现docker已经卸载

5、docker存储

重新安装Ddocker,镜像内容不会丢失,内容在:/var/lib/docker中

胡梦旭博客
请先登录后发表评论
  • latest comments
  • 总共0条评论