中间件一键部署脚本:普通用户版 Docker 安装
普通用户安装docker,必须拥有sudo权限
Docker 的核心功能是通过 Linux 内核特性(如 namespaces、cgroups、overlayfs 等)实现容器隔离。这些操作需要直接与内核交互,而普通用户默认无权执行这些操作:
-
网络管理:创建虚拟网卡、配置 iptables 规则等。
-
存储管理:挂载文件系统(如
overlay2)、管理卷(volumes)。 -
进程隔离:通过
namespaces隔离进程、网络、文件系统等。 -
资源限制:通过
cgroups限制 CPU、内存等资源。
这些操作需要 root 权限,因为它们直接影响宿主机的全局状态。
运行脚本
sudo -E bash deployment_docker.sh

使用docker

deployment_docker.sh内容
#!/bin/bash
tar_name=$(ls docker*)
if [[ -z "${tar_name}" ]]; then
echo "未找到 docker 二进制安装包"
exit 1
fi
tar -xvzf ${tar_name} -C $(pwd)
cd docker* && mkdir data containerd-data containerd-state
currentdir=$(pwd)
# 设置 PATH 变量
echo "[*] 配置环境变量..."
echo "export PATH=$currentdir:\$PATH" >> ~/.bashrc
export PATH=$currentdir:$PATH
# 启动 dockerd(用户模式)
echo "[*] 启动 containerd(普通用户模式)..."
cat >$currentdir/containerd-config.toml <<EOF
# Copyright 2018-2022 Docker Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
disabled_plugins = ["cri"]
root = "$currentdir/containerd-data"
state = "$currentdir/containerd-state"
#root = "/var/lib/containerd"
#state = "/run/containerd"
#subreaper = true
#oom_score = 0
#[grpc]
# address = "/run/containerd/containerd.sock"
# uid = 0
# gid = 0
[grpc]
address = "$currentdir/containerd.sock"
#[debug]
# address = "/run/containerd/debug.sock"
# uid = 0
# gid = 0
# level = "info"
EOF
$currentdir/containerd --config $currentdir/containerd-config.toml &
# 启动 dockerd(用户模式)
echo "[*] 启动 Docker(普通用户模式)..."
cat >$currentdir/daemon.json <<EOF
{
"hosts": ["unix://$currentdir/docker.sock"],
"data-root": "$currentdir/data",
"exec-opts": ["native.cgroupdriver=cgroupfs"],
"log-level": "warn",
"log-opts": {
"max-size": "100m",
"max-file": "3"
}
}
EOF
$currentdir/dockerd --containerd $currentdir/containerd.sock --config-file $currentdir/daemon.json &
# 等待 Docker 启动
sleep 5
echo "export DOCKER_HOST=unix://$currentdir/docker.sock" >> ~/.bashrc
$currentdir/docker version
echo "[✅] Docker 安装完成!"
echo "[👉] 请执行以下命令使用 Docker:"
echo "执行:sudo -E docker info"
浙公网安备 33010602011771号