Docker环境安装

删除旧的docker

1
root@ubuntu2204:~# for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

一、使用仓库安装docker

安装依赖环境

1
2
root@ubuntu2204:~# apt-get update
root@ubuntu2204:~# apt-get install ca-certificates curl gnupg

添加docker-ce软件源

注:Ubuntu apt仓库中默认有较新的版本,不添加docker源也可安装。

1
2
3
4
5
6
7
8
root@ubuntu2204:~# install -m 0755 -d /etc/apt/keyrings
root@ubuntu2204:~# curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu//gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
root@ubuntu2204:~# sudo chmod a+r /etc/apt/keyrings/docker.gpg
root@ubuntu2204:~# echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
root@ubuntu2204:~# apt update

查看docker版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@ubuntu2204:~# apt-cache madison docker-ce
docker-ce | 5:27.3.1-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:27.3.0-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:27.2.1-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:27.2.0-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:27.1.2-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:27.1.1-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:27.1.0-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:27.0.3-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:27.0.2-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:27.0.1-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:26.1.4-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:26.1.3-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:26.1.2-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:26.1.1-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
docker-ce | 5:26.1.0-1~ubuntu.22.04~jammy | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy/stable amd64 Packages
.....

安装特定docker-ce版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@ubuntu2204:~# apt-get install docker-ce=5:27.2.1-1~ubuntu.22.04~jammy docker-ce-cli=5:27.2.1-1~ubuntu.22.04~jammy containerd.io docker-buildx-plugin docker-compose-plugin

root@ubuntu2204:~# docker info
Client: Docker Engine - Community
Version: 27.2.1
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.17.1
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.29.7
Path: /usr/libexec/docker/cli-plugins/docker-compose

二、使用二进制包安装docker

可以清华大学镜像站下载Docker二进制源码包:

1
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/static/stable/x86_64/

安装iptables

1
root@ubuntu2204:~# apt install iptables

下载源码

1
2
3
4
5
6
7
8
9
10
11
root@ubuntu2204:~#  wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/static/stable/x86_64/docker-27.2.1.tgz
--2024-09-28 13:14:30-- https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/static/stable/x86_64/docker-27.2.1.tgz
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.15.130, 2402:f000:1:400::2
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.15.130|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 75336894 (72M) [application/octet-stream]
Saving to: ‘docker-27.2.1.tgz’

docker-27.2.1.tgz 100%[======================================================================>] 71.85M 32.6MB/s in 2.2s

2024-09-28 13:14:32 (32.6 MB/s) - ‘docker-27.2.1.tgz’ saved [75336894/75336894]

解压并复制到/usr/bin目录

1
2
3
root@ubuntu2204:~#  tar xf docker-27.2.1.tgz 
root@ubuntu2204:~# cd docker/
root@ubuntu2204:~# cp * /usr/bin/

添加service文件

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
root@ubuntu2204:~#  cat > /lib/systemd/system/docker.service << EOF
[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 -H unix://var/run/docker.sock
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
EOF

启动服务

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
root@ubuntu2204:~# systemctl daemon-reload
root@ubuntu2204:~# systemctl enable --now docker
root@ubuntu2204:~# systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2024-09-28 13:22:02 UTC; 5min ago
Docs: https://docs.docker.com
Main PID: 12403 (dockerd)
Tasks: 20 (limit: 4515)
Memory: 21.6M
CPU: 1.737s
CGroup: /system.slice/docker.service
├─12403 /usr/bin/dockerd -H unix://var/run/docker.sock
└─12412 containerd --config /var/run/docker/containerd/containerd.toml


root@ubuntu2204:~# docker info
Client:
Version: 27.2.1
Context: default
Debug Mode: false

Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 27.2.1
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 472731909fa34bd7bc9c087e4c27943f9835f111
runc version: v1.1.14-0-g2c9f560
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: builtin
cgroupns
Kernel Version: 5.15.0-102-generic
Operating System: Ubuntu 22.04.3 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.786GiB
Name: ubuntu2204
ID: 989b18be-d47f-450b-9ab2-e349432d646a
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine


Docker环境安装
https://www.xcjyc.top/2024/09/28/Docker环境安装/
作者
XCJYC
发布于
2024年9月28日
许可协议