部署

Docker 安装

https://docs.docker.com/engine/install/

Ubuntu

https://docs.docker.com/engine/install/ubuntu/

# 更新仓库
apt-get update


# 安装相关依赖包
apt-get -y install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release


# 添加Docker官方的GPG密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg


# 写入软件源信息,从阿里云获取,这样速度更快
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu/ \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 更新仓库
apt-get update

# 查看可以安装的 docker-ce 和 docker-ce-cli 版本
apt-cache madison docker-ce docker-ce-cli

# 安装最新版本的 docker-ce 和 docker-ce-cli(不指定版本则默认安装的就是最新版本)
apt -y install \
docker-ce \
docker-ce-cli

# 或指定版本安装 docker-ce 和 docker-ce-cli
apt -y install \
docker-ce=5:20.10.7~3-0~ubuntu-focal \
docker-ce-cli=5:20.10.7~3-0~ubuntu-focal


# 通过运行镜像来验证 Docker 引擎是否已正确安装。
docker run hello-world

关闭 WARNING: No swap limit support

# docker info
...
WARNING: No swap limit support


# 修改grub配置文件,主要在以下行中添加swapaccount=1
# vim /etc/default/grub
...
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 swapaccount=1"
...


# 更新grub,此命令属于Ubuntu的命令,centos不支持
# update-grub
...
done


# 重启
# reboot

CentOS

  • 官方文档:https://docs.docker.com/engine/install/centos/

使用存储库安装

  • 官方文档:https://docs.docker.com/engine/install/centos/#install-using-the-repository

设置存储库

#设置存储库
[root@docker ~]# yum install -y yum-utils
[root@docker ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

安装 Docker 引擎

安装最新版
$ yum install docker-ce docker-ce-cli containerd.io
指定安装版本
#列出可选的版本
$ yum list docker-ce --showduplicates | sort -r

#安装指定版本
$ yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

#范例:安装指定版本
$ yum install docker-ce-20.10.10 docker-ce-cli-20.10.10 containerd.io
启动Docker
$ systemctl enable --now docker

基于阿里云安装

#创建仓库
# vim /etc/yum.repos.d/docker_aliyun.repo


#指定20.10.10版本进行安装
yum install -y docker-ce-20.10.10 docker-ce-cli-20.10.10 containerd.io


#配置镜像加速
# mkdir -p /etc/docker/
# vim /etc/docker/daemon.json
{
  "registry-mirrors": ["https://jqm0rnhf.mirror.aliyuncs.com"]
}

#启动docker服务
systemctl enable --now docker

docker_install.sh

#!/bin/bash

ubuntu_function() {
    local docker_version="5:20.10.24~3-0~ubuntu-focal"
    if command -v docker &>/dev/null; then
        echo "Docker is already installed."
    else
        apt-get update
        apt-get -y install apt-transport-https ca-certificates curl software-properties-common
        curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
        add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
        apt-get -y update
        apt-get -y install docker-ce=${docker_version}
	configure_docker_accelerator
    fi

}

centos_function() {
    echo "This is CentOS!"
}

other_function() {
    echo "This is another distribution!"
}

configure_docker_accelerator() {
    if [ ! -f /etc/docker/daemon.json ]; then
        mkdir -p /etc/docker
        touch /etc/docker/daemon.json
    fi

    echo '{
      "registry-mirrors": ["https://jqm0rnhf.mirror.aliyuncs.com"]
    }' | sudo tee /etc/docker/daemon.json > /dev/null

    systemctl restart docker

    echo "Docker image accelerator configured successfully."
}

if [ -f /etc/os-release ]; then
    source /etc/os-release
    case "$ID" in
        ubuntu)
            ubuntu_function
            ;;
        centos)
            centos_function
            ;;
        *)
            other_function
            ;;
    esac
else
    echo "Unable to determine the Linux distribution."
fi

Docker 镜像加速配置

通过阿里云加速

官方地址:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://jqm0rnhf.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

验证

[root@docker ~]# docker info
...
 Registry Mirrors:
  https://jqm0rnhf.mirror.aliyuncs.com/
...  

多途径加速

  • 定义多个镜像地址时会按照顺序依次寻找相关的镜像,因此要将速度最快的镜像地址放在前面。
# mkdir -p /etc/docker

# tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": ["https://hub-mirror.c.163.com/","https://docker.mirrors.ustc.edu.cn/","https://reg-mirror.qiniu.com"]
}
EOF

# systemctl daemon-reload

# systemctl restart docker

验证

# docker info
...
 Registry Mirrors:
  https://jqm0rnhf.mirror.aliyuncs.com/
  https://hub-mirror.c.163.com/
  https://docker.mirrors.ustc.edu.cn/
  https://reg-mirror.qiniu.com/
...

其它Docker加速器

还有许多其他Docker镜像加速器可供选择,以下是一些较为流行的Docker镜像加速器:

  1. 阿里云的Docker加速器:https://<您的加速器ID>.mirror.aliyuncs.com,只需在此网址的末尾替换您的个人或团队的加速器ID。
  2. DaoCloud的Docker加速器:https://<您的用户名>.hub.daocloud.io。使用DaoCloud的Docker加速器,您需要先在DaoCloud上注册一个帐户,然后使用用户名登录。
  3. Azure的Docker加速器:https://<区域>.azurecr.io,其中<区域>是您的Azure区域(例如,eastus或westus)。
  4. AWS的Docker加速器:https://<数字>.dkr.ecr.<区域>.amazonaws.com,其中<数字>是您的AWS帐户ID,<区域>是您的AWS区域。

这些Docker镜像加速器都具有不同的性能和特点,您可以根据自己的需求选择最适合的Docker加速器。无论您选择哪个Docker镜像加速器,只需将其添加到Docker的配置文件中,即可在拉取镜像时自动使用加速器加快镜像的下载速度。

请注意,不同的Docker加速器可能由于负载不同而导致速度的差异。如果您遇到速度问题,可以尝试使用另一个Docker加速器,或者尝试使用多个Docker加速器以获得更快的下载速度。

安装本地 Docker 文档

官方帮助网址:https://docs.docker.com/

将文档下载到本机来实现访问

docker run -it -d -p 4000:4000 docs/docker.github.io:latest

#用浏览器访问本机的4000端口即可实现访问

Docker 相关文件

Unit File

/usr/lib/systemd/system/docker.service
/usr/lib/systemd/system/docker.socket

镜像下载保存路径

???

容器数据保存路径

[root@docker ~]# docker run -it alpine 
/ # touch test.txt
[root@docker ~]# find / -name test.txt
/var/lib/docker/overlay2/7915b47304f70cbb946e4b4e0209b3a349275bb54aedffebcd5822764e76fc26/diff/test.txt
/var/lib/docker/overlay2/7915b47304f70cbb946e4b4e0209b3a349275bb54aedffebcd5822764e76fc26/merged/test.txt
[root@docker ~]# docker images 
REPOSITORY              TAG       IMAGE ID       CREATED         SIZE
busybox                 latest    beae173ccac6   2 weeks ago     1.24MB
alpine                  latest    c059bfaa849c   8 weeks ago     5.59MB
hello-world             latest    feb5d9fea6a5   3 months ago    13.3kB
docs/docker.github.io   latest    32ed84d97e30   19 months ago   1GB
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND     CREATED         STATUS         PORTS     NAMES
418f07084909   alpine    "/bin/sh"   3 minutes ago   Up 3 minutes             serene_chebyshev

???

Docker 服务管理

默认docker是以sock形式来运行的,也可以设置为端口号形式运行

给docker添加标签

  • 加以区分
vim /lib/systemd/system/docker.service
#修改下面行,name=后面的字段可以自定义
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --label="name=docker0"
#重启生效
systemctl daemon-reload
systemctl restart docker

以端口方式运行docker

方法一

vim /lib/systemd/system/docker.service
#修改下面行,【】中为添加的字段,端口可以自定义
ExecStart=/usr/bin/dockerd -H fd:// 【-H tcp://0.0.0.0:9817】 --containerd=/run/containerd/containerd.sock --label="name=docker0"

#重启生效
systemctl daemon-reload
systemctl restart docker

远程访问 Docker

#远程访问docker方法一
curl http://10.0.0.100:9817/info

#远程访问docker方法二
docker -H tcp://10.0.0.100:9817 info