Docker安装

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

bash

yum install docker

(2)安装高版本

Centos

bash

yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache

Ubuntu

bash

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.

使用如下命令

bash

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

bash

yum list docker-ce --showduplicates | sort -r

CentOS8 默认使用 podman 代替 docker ,所以需要 containerd.io

安装containerd.io

bash

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

bash

sudo apt-cache madison docker-ce|awk '{print $3}'

安装指定版本

Centos

bash

yum install docker-ce-<VERSION_STRING>
yum install docker-ce-19.03.15

Ubuntu

bash

sudo apt -y install docker-ce=<VERSION_STRING>
sudo apt -y install docker-ce=5:19.03.15~3-0~ubuntu-focal

修改加速器,添加私有仓库,修改docker默认存储位置

bash

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

# 按需要添加
  "dns": ["114.114.114.114", "223.5.5.5"]
  "features": {
      "buildkit": false
     }

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

bash

systemctl start docker
systemctl enable docker

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

bash

sudo usermod -a -G docker $USER

bash

systemctl stop docker

bash

yum -y remove docker \
docker-client \
docker-common

bash

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

bash

yum install docker-ce-19.03.9-3.el7

使用之前的数据目录,指定之前的储存目录,我们之前Docker的储存目录是 /data/docker

修改docker默认存储位置,配置镜像加速器,添加私有仓库地址

bash

systemctl enable --now docker

以前Docker版本为1.13.1,,在对其版本升级到18.06.1之后,启动旧版本创建的容器时遇到这个错误:

查资料得知:因为「当您从不兼容的版本升级docker并且升级后无法启动docker容器时会出现这种情况」,解决办法如下:

bash

grep -rl 'docker-runc' /data/docker/containers/ | xargs sed -i 's/docker-runc/runc/g'
systemctl restart docker

/data/docker/containers/为docker存储目录

由于某些项目需要用到中文字体

Dockerfile中添加

dockerfile

COPY simsun.ttc /usr/share/fonts/simsun.ttc

执行以下命令查看系统中的中文字体

bash

fc-list :lang=zh

二进制

bash

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安装

bash

#curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py
pip3 install docker-compose

相关内容