注意
本文最后更新于 2023-07-12 ,文中内容可能已过时。
摘要
(1)直接安装(版本较低)
(2)安装高版本
Centos
1
2
3
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache
Ubuntu
1
2
3
4
5
6
7
8
9
10
11
12
13
sudo apt -y install apt-transport-https ca-certificates curl software-properties-common
# aliyun
sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $( lsb_release -cs) stable"
sudo apt -y update
# tencent
sudo curl -fsSL http://mirrors.tencent.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] http://mirrors.tencent.com/docker-ce/linux/ubuntu $( lsb_release -cs) stable"
# tsinghua
sudo curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $( lsb_release -cs) stable"
sudo apt -y update
出现以下提示:
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
原因如下:
apt-key is used to manage the list of keys used by apt to authenticate packages. Packages which have been authenticated using these keys are considered trusted.
Use of apt-key is deprecated, except for the use of apt-key del in maintainer scripts to remove existing keys from the main keyring. If such usage of apt-key is desired, the additional installation of the GNU Privacy Guard suite (packaged in gnupg) is required.
apt-key(8) will last be available in Debian 11 and Ubuntu 22.04.
使用如下命令
1
2
3
cd /etc/apt/trusted.gpg.d/
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor >docker.gpg
echo "deb [arch= $( dpkg --print-architecture) ] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu " $( . /etc/os-release && echo " $VERSION_CODENAME " ) " stable" > /etc/apt/sources.list.d/docker.list
列出Docker版本
Centos
1
yum list docker-ce --showduplicates | sort -r
CentOS8 默认使用 podman 代替 docker ,所以需要 containerd.io
安装containerd.io
1
yum -y install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/edge/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
Ubuntu
1
sudo apt-cache madison docker-ce| awk '{print $3}'
安装指定版本
Centos
1
2
yum install docker-ce-<VERSION_STRING>
yum install docker-ce-19.03.15
Ubuntu
1
2
sudo apt -y install docker-ce= <VERSION_STRING>
sudo apt -y install docker-ce= 5:19.03.15~3-0~ubuntu-focal
修改加速器,添加私有仓库,修改docker默认存储位置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"data-root": "/data/docker",
"experimental": true,
"insecure-registries": [
"192.168.1.60"
],
"log-driver": "json-file",
"log-opts": {
"max-file": "3",
"max-size": "5m"
},
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://ung2thfc.mirror.aliyuncs.com"
]
}
EOF
1
2
systemctl start docker
systemctl enable docker
为了避免每次命令都输入sudo,可以设置用户权限
1
sudo usermod -a -G docker $USER
1
2
3
yum -y remove docker \
docker-client \
docker-common
1
2
3
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache
1
yum install docker-ce-19.03.9-3.el7
使用之前的数据目录,指定之前的储存目录,我们之前Docker的储存目录是 /data/docker
修改docker默认存储位置,配置镜像加速器,添加私有仓库地址
1
systemctl enable --now docker
以前Docker版本为1.13.1,
,在对其版本升级到18.06.1
之后,启动旧版本创建的容器时遇到这个错误:
查资料得知:因为「当您从不兼容的版本升级docker并且升级后无法启动docker容器时会出现这种情况」,解决办法如下:
1
2
grep -rl 'docker-runc' /data/docker/containers/ | xargs sed -i 's/docker-runc/runc/g'
systemctl restart docker
/data/docker/containers/
为docker存储目录
由于某些项目需要用到中文字体
Dockerfile中添加
1
COPY simsun.ttc /usr/share/fonts/simsun.ttc
执行以下命令查看系统中的中文字体
二进制
1
2
3
4
curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-` uname -s` -` uname -m` -o /usr/local/bin/docker-compose
# 国内
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-` uname -s` -` uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
pip安装
1
2
#curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py
pip3 install docker-compose