安装docker

一、创建docker安装脚本

touch 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 /etc/systemd/system/
echo '添加文件可执行权限'
chmod +x /etc/systemd/system/docker.service
echo '重新加载配置文件'
systemctl daemon-reload
echo '启动docker'
systemctl start docker
echo '设置开机自启'
systemctl enable docker.service
echo 'docker安装成功'
docker -v

二、创建docker-compose安装脚本

touch installCompose.sh
#!/bin/sh

set -e  # 遇到错误立即退出

# 检查参数
if [ $# -eq 0 ]; then
    echo "错误: 请提供docker-compose二进制文件名作为参数"
    echo "用法: ./installCompose.sh <docker-compose-binary-file>"
    exit 1
fi

COMPOSE_FILE=$1

# 检查文件是否存在
if [ ! -f "$COMPOSE_FILE" ]; then
    echo "错误: 文件 $COMPOSE_FILE 不存在"
    exit 1
fi

echo '步骤1: 检查文件类型'
file "$COMPOSE_FILE"

echo '步骤2: 将docker-compose文件复制到/usr/bin目录'
cp "$COMPOSE_FILE" /usr/bin/docker-compose

echo '步骤3: 添加文件可执行权限'
chmod +x /usr/bin/docker-compose

echo '步骤4: 验证docker-compose安装'
docker-compose --version

if [ $? -eq 0 ]; then
    echo '✅ docker-compose安装成功'
else
    echo '❌ docker-compose安装失败'
    exit 1
fi

三、创建docker.service脚本文件

touch docker.service
[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

四、创建uninstall.sh卸载脚本

touch uninstall.sh
#!/bin/sh
 
echo '停止docker'
systemctl stop docker
echo '删除docker.service'
rm -f /etc/systemd/system/docker.service
echo '删除docker文件'
rm -rf /usr/bin/docker*
echo '重新加载配置文件'
systemctl daemon-reload
echo '卸载成功'

五、上传安装包到脚本目录

六、为脚本赋予权限

chmod 777 install.sh
chmod 777 installCompose.sh
chmod 777 uninstall.sh

七、执行脚本文件

sh install.sh docker-28.0.4.tgz
sh installCompose.sh docker-compose-linux-x86_64

八、验证命令

docker -v
docker-compose -v

标签: none

添加新评论