Docker 安装

添加 Docker源

1
2
3
4
5
6
7
8
# 安装需要的软件包以使apt能够通过HTTPS使用仓库
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release

# 信任 Docker 的 GPG 公钥并添加仓库
install -m 0755 -d /etc/apt/keyrings
# 下载GPG
# 授权
sudo chmod a+r /etc/apt/keyrings/docker.gpg

添加 Docker 的官方 GPG

1
2
3
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 添加 Docker 官方安装路径:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
1
2
3
4
5
# 使用清化大学源
# 添加Docker官方的GPG密钥
curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 设置稳定版仓库
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
1
2
3
4
# 添加阿里云官方GPG密钥
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# 写入阿里云Docker仓库地址
sudo sh -c 'echo "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list'

更新 Ubuntu:

1
2
# 更新软件包索引
sudo apt-get update

安装 Docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 安装最新 Docker CE
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 或者指定 Docker 版本: 查询版本 apt-cache madison docker-ce
sudo apt install docker-ce=5:20.10.16~3-0~ubuntu-jammy docker-ce-cli=5:20.10.16~3-0~ubuntu-jammy containerd.io

# 查看 Docker 运行狀況
sudo systemctl status docker
# 验证是否成功安装了docker
docker --version

# 自动启动 Docker:
sudo systemctl start docker
sudo systemctl enable docker
1
2
3
4
sudo newgrp docker
sudo groupadd docker
sudo usermod -aG docker ${USER}
sudo systemctl restart docker

换源

2024年6月后,继续可以使用有效docker镜像仓库地址

1
2
3
4
5
6
7
8
9
10
11
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://dockerhub.icu"]
}
EOF

# 重启 docker
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl status docker

历史的镜像仓库地址:

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
# 修改daemon.json文件,
vim /etc/docker/daemon.json

# daemon.json内容如下:
{
"registry-mirrors": [
"https://dockerproxy.com",
"https://docker.m.daocloud.io",
"https://cr.console.aliyun.com",
"https://ccr.ccs.tencentyun.com",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com",
"https://docker.nju.edu.cn",
"https://docker.mirrors.sjtug.sjtu.edu.cn",
"https://github.com/ustclug/mirrorrequest",
"https://registry.docker-cn.com"
]
}

# 重载配置文件,并重启 docker
sudo systemctl daemon-reload
sudo systemctl restart docker

# 查看 Registry Mirrors 配置是否成功
sudo docker info

安裝 Docker-composer

Releases · docker/compose (github.com)下载最新 Docker Composer。

1
2
3
4
5
# 执行下列命令安装最新的 Docker Composer 当下的版本是 2.18.1
sudo curl -L "https://mirror.ghproxy.com/https://github.com/docker/compose/releases/download/v2.18.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 赋予可执行的权限
sudo chmod +x /usr/local/bin/docker-compose