Linux安装Docker

CentOS

bash

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

Ubuntu

bash

apt install -y ca-certificates curl gnupg lsb-release
install -m 0755 -d /etc/apt/keyrings
# 安装GPG证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/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

Ubuntu

bash

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

安装指定版本

CentOS

bash

yum install docker-ce-<VERSION_STRING>
yum install docker-ce-20.10.9-3.el8

Ubuntu

bash

apt -y install docker-ce=<VERSION_STRING>
apt -y install docker-ce=5:20.10.24~3-0~ubuntu-jammy

修改加速器,添加私有仓库,修改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://docker.1panel.live",
    "https://docker.rainbond.cc",
    "https://docker.m.daocloud.io",
    "https://docker.anyhub.us.kg",
    "https://dockerhub.icu",
    "https://docker.awsl9527.cn"
  ]
}
EOF

# 按需要添加
  "dns": [
    "223.5.5.5", "114.114.114.114"
  ]
  "features": {
      "buildkit": false
     }
     
# openEuler 以及 Kylin需要设置
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Hard": 1048576,
      "Soft": 1048576
    }
  }

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

bash

systemctl start docker
systemctl enable docker

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

bash

sudo usermod -a -G docker $USER

bash

systemctl stop docker

bash

apt -y remove docker \
docker-client \
docker-common

参考上面安装教程

使用之前的数据目录,指定之前的储存目录,我们之前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

相关内容