Linux安装Docker

本文主要介绍如何在在Linux上部署Docker,主要步骤参见官网:
CentOS7:https://docs.docker.com/install/linux/docker-ce/centos/
CentOS8:https://www.linuxtechi.com/install-docker-ce-centos-8-rhel-8/
Ubuntu:https://docs.docker.com/engine/install/ubuntu/
阿里云:https://developer.aliyun.com/mirror/docker-ce,不过阿里云的教程里,有点问题

1. CentOS

1.1 配置Repo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
tee /etc/yum.repos.d/docker-ce.repo <<-'EOF'
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
exclude=docker-ce docker-ce-cli containerd.io
EOF

/usr/bin/crb enable;setenforce 0;
dnf clean all;dnf makecache -y;dnf update -y;
dnf groupinstall -y 'development tools';
dnf install -y --disableexcludes=docker-ce-stable docker-ce docker-ce-cli containerd.io;
systemctl enable --now docker;

注意代理和日志,这里使用的是163的代理,但是最好是你自己nexus私服。

1
2
3
4
5
6
7
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://hub-mirror.c.163.com"],
"log-driver": "json-file",
"log-opts": {"max-size": "10m", "max-file": "3"}
}
EOF

1.2.清理docker日志

找日志路径

1
docker inspect 017979c6f3bc | grep LogPath

1.3.安装Docker Compose

1
2
3
4
5
$ sudo dnf install curl -y
# 你需要替换掉下面的1.25.4为你的目标版本
$ curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
$ docker-compose --version

2. Ubuntu

1
2
3
4
5
6
7
8
9
10
11
# step 1: 安装必要的一些系统工具
sudo apt update
sudo apt -y install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release

# step 2: 安装GPG证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io

3.完成

如果你遇到了问题,可以参考阿里云的说明:https://developer.aliyun.com/article/29941

3.1.代理方式一-修改Docker镜像源

这里填写的是网易的镜像,但是我建议你搭建自己的nexus服务器,然后这里填写你自己的nexus的docker地址,参考nexus配置的一些实践

1
2
3
4
5
6
$ vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://hub-mirror.c.163.com"]
}
$ sudo systemctl daemon-reload
$ sudo service docker restart

3.2.代理方式一-http代理

因为docker通过systemd启动,所以bash里的http_proxy无效

1
2
3
4
5
6
$ mkdir -p /etc/systemd/system/docker.service.d
$ vim /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:7890"
Environment="HTTPS_PROXY=https://proxy.example.com:7890"
$ systemctl daemon-reload;systemctl restart docker

如果你打算自己基于ssr做一个公共http代理,可以参见:privoxy和ssr为内网提供http代理加速

4.2 启动Docker

1
2
$ sudo systemctl start docker
$ sudo systemctl enable docker

4.3 查看版本

1
$ docker --version

4.4 验证安装

sudo docker run hello-world

这是一个mini的镜像。
至此,你已经安装完毕。