Docker安装

摘要

(1)直接安装(版本较低)

1
yum install docker

(2)安装高版本

Centos

1
2
3
yum install -y yum-utils
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 install -y ca-certificates curl gnupg lsb-release
# 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 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
4
5
6
install -m 0755 -d /etc/apt/keyrings
# 添加 Docker 官方 GPG 密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# 使用以下命令设置软件源
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt update

列出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
21
22
23
24
25
26
mkdir -p /etc/docker
ls /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
    "data-root": "/data/docker",
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "5m",
        "max-file": "3"
    },
    "experimental": true,
    "insecure-registries": [
        "192.168.1.10"
    ],
    "registry-mirrors": [
        "https://dockerproxy.com",
        "https://docker.mirrors.sjtug.sjtu.edu.cn",
        "https://docker.m.daocloud.io"
    ]
}
EOF

# 按需要添加
    "features": {
        "buildkit": false
     }

目前由于Docker Hub限制,导致使用镜像加速器后无法获取最新官方镜像。请暂时去掉加速器配置,直接连接Docker Hub获取。

1
2
systemctl start docker
systemctl enable docker

为了避免每次命令都输入sudo,可以设置用户权限

1
sudo usermod -a -G docker $USER
1
systemctl stop docker
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之后,启动旧版本创建的容器时遇到这个错误:

https://img.bwcxtech.com/img/20200928154200.png

查资料得知:因为「当您从不兼容的版本升级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
fc-list :lang=zh

二进制

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

相关内容