中间件一键部署脚本:普通用户版 Docker 安装

普通用户安装docker,必须拥有sudo权限

Docker 的核心功能是通过 Linux 内核特性(如 namespacescgroupsoverlayfs 等)实现容器隔离。这些操作需要直接与内核交互,而普通用户默认无权执行这些操作:

  • 网络管理:创建虚拟网卡、配置 iptables 规则等。

  • 存储管理:挂载文件系统(如 overlay2)、管理卷(volumes)。

  • 进程隔离:通过 namespaces 隔离进程、网络、文件系统等。

  • 资源限制:通过 cgroups 限制 CPU、内存等资源。

这些操作需要 root 权限,因为它们直接影响宿主机的全局状态。

 

运行脚本

sudo -E bash deployment_docker.sh

 

使用docker

 

deployment_docker.sh内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/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"

  

posted @   瘦阿瘦  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示