一、上期回顾

每天半小时,轻松学Docker第二篇:Linux二八法则基础知识

二、Docker概念

Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化。容器完全使用沙箱机制,相互之间不会有任何接口。

一个完整的Docker有以下几个部分组成:

  • Docker Client客户端
  • Docker Daemon守护进程
  • Docker Image镜像
  • Docker Container容器

Docker使用客户端-服务器(C/S)架构模式,使用远程API来管理和创建Docker容器。Docker daemon作为服务端接受来自客户的请求,并处理这些请求(创建、运行、分发容器)。 客户端和服务端既可以运行在一个机器上,也可通过Socket或者RESTful API来进行通信。Docker daemon一般在宿主主机后台运行,等待接收来自客户端的消息。Docker客户端则为用户提供一系列可执行命令,用户用这些命令实现跟Docker daemon交互。由Docker daemon服务根据收到命令进行包括构运行容器Containers、建镜像images以及从镜像仓库Regsitry先拉到镜像images到本地Docker Host之后再运行容器Containers。

三、Docker部署

如碰到bash: sudo: command not found,执行apt-get install -y sudo安装一下即可

1、创建中科大的Debian 12源文件

1.1、创建中科大的源文件

sudo vim /etc/apt/sources.list.d/ustc-debian.list
# 中国科技大学 Debian 12 镜像源
deb https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.ustc.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
deb https://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware

2、添加Docker官方源

2.1、安装官方依赖

sudo apt update
sudo apt install -y ca-certificates curl gnupg lsb-release

2.2、添加Docker官方GPG密钥

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

2.3、创建Docker源文件

sudo vim /etc/apt/sources.list.d/docker-ustc.list
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm stable

3、更新并安装Docker

3.1、更新软件包索引

sudo apt update

3.2、查看Docker版本

apt-cache madison docker-ce

root@debase:/etc/apt/sources.list.d# apt-cache madison docker-ce
 docker-ce | 5:28.4.0-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.3.3-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.3.2-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.3.1-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.3.0-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.2.2-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.2.1-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.2.0-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.1.1-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.1.0-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.0.4-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.0.3-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.0.2-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.0.1-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:28.0.0-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.5.1-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.5.0-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.4.1-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.4.0-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.3.1-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.3.0-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.2.1-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.2.0-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.1.2-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.1.1-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.1.0-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.0.3-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.0.2-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages
 docker-ce | 5:27.0.1-1~debian.12~bookworm | https://mirrors.ustc.edu.cn/docker-ce/linux/debian bookworm/stable amd64 Packages


3.3、安装指定版本Docker

sudo apt install -y \
  docker-ce=5:27.4.1-1~debian.12~bookworm \
  docker-ce-cli=5:27.4.1-1~debian.12~bookworm \
  containerd.io \
  docker-buildx-plugin \
  docker-compose-plugin

4、启动和验证Docker

4.1、查看状态和版本

root@debase:/etc/apt/sources.list.d# docker --version
Docker version 27.4.1, build b9d17ea
root@debase:/etc/apt/sources.list.d# docker compose version
Docker Compose version v2.39.4
root@debase:/etc/apt/sources.list.d# systemctl status docker.service
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; preset: enabled)
     Active: active (running) since Tue 2025-09-30 14:36:05 CST; 2min 17s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 6068 (dockerd)
      Tasks: 9
     Memory: 26.1M
        CPU: 573ms
     CGroup: /system.slice/docker.service
             └─6068 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Sep 30 14:36:04 debase systemd[1]: Starting docker.service - Docker Application Container Engine...
Sep 30 14:36:04 debase dockerd[6068]: time="2025-09-30T14:36:04.557798333+08:00" level=info msg="Starting up"
Sep 30 14:36:04 debase dockerd[6068]: time="2025-09-30T14:36:04.558496250+08:00" level=info msg="OTEL tracing is not configured, using no-op tracer provider"
Sep 30 14:36:04 debase dockerd[6068]: time="2025-09-30T14:36:04.695305174+08:00" level=info msg="Loading containers: start."
Sep 30 14:36:05 debase dockerd[6068]: time="2025-09-30T14:36:05.317111835+08:00" level=info msg="Loading containers: done."
Sep 30 14:36:05 debase dockerd[6068]: time="2025-09-30T14:36:05.339517306+08:00" level=info msg="Docker daemon" commit=c710b88 containerd-snapshotter=false storage-driver=overlay2 version=27.4.1
Sep 30 14:36:05 debase dockerd[6068]: time="2025-09-30T14:36:05.339828001+08:00" level=info msg="Daemon has completed initialization"
Sep 30 14:36:05 debase systemd[1]: Started docker.service - Docker Application Container Engine.
Sep 30 14:36:05 debase dockerd[6068]: time="2025-09-30T14:36:05.385865905+08:00" level=info msg="API listen on /run/docker.sock"
root@debase:/etc/apt/sources.list.d# 

4.2、验证功能

root@debase:/etc/apt/sources.list.d# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
root@debase:/etc/apt/sources.list.d# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

四、Docker代理配置

Docker引擎通过代理访问镜像仓库,可以配置Docker代理,使Docker引擎使用代理进行镜像image拉取。

1、添加代理配置文件

# 打开文件
sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf

# 文件内填入内容
[Service]
Environment="HTTP_PROXY=http://192.168.100.237:7890"
Environment="HTTPS_PROXY=http://192.168.100.237:7890"
Environment="NO_PROXY=localhost,127.0.0.1"

2、重新加载配置,重启Docker

root@debase:/etc/systemd/system# mkdir -p /etc/systemd/system/docker.service.d
root@debase:/etc/systemd/system# ll
total 40
drwxr-xr-x 10 root root 4096 Sep 30 14:47 ./
drwxr-xr-x  5 root root 4096 Sep 29 17:42 ../
lrwxrwxrwx  1 root root   45 Sep 29 17:42 dbus-org.freedesktop.timesync1.service -> /lib/systemd/system/systemd-timesyncd.service
drwxr-xr-x  2 root root 4096 Sep 30 14:47 docker.service.d/
drwxr-xr-x  2 root root 4096 Sep 29 17:28 getty.target.wants/
drwxr-xr-x  2 root root 4096 Sep 30 14:36 multi-user.target.wants/
drwxr-xr-x  2 root root 4096 Sep 29 17:29 network-online.target.wants/
drwxr-xr-x  2 root root 4096 Sep 29 17:43 open-vm-tools.service.requires/
drwxr-xr-x  2 root root 4096 Sep 30 14:36 sockets.target.wants/
lrwxrwxrwx  1 root root   31 Sep 29 17:42 sshd.service -> /lib/systemd/system/ssh.service
drwxr-xr-x  2 root root 4096 Sep 29 17:42 sysinit.target.wants/
drwxr-xr-x  2 root root 4096 Sep 30 10:01 timers.target.wants/
lrwxrwxrwx  1 root root   41 Sep 29 17:43 vmtoolsd.service -> /lib/systemd/system/open-vm-tools.service
root@debase:/etc/systemd/system#  vim /etc/systemd/system/docker.service.d/http-proxy.conf
root@debase:/etc/systemd/system# systemctl daemon-reload
root@debase:/etc/systemd/system# systemctl restart docker
root@debase:/etc/systemd/system# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

五、镜像加速设置

1、打开配置文件

# 打开文件
sudo vim vim /etc/docker/daemon.json

# 文件内填入内容
{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com",
    "https://docker.nju.edu.cn"
  ]
}

2、重新加载配置,重启Docker

root@debase:/etc/systemd/system# systemctl daemon-reload
root@debase:/etc/systemd/system# systemctl restart docker
root@debase:/etc/systemd/system# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

3、验证镜像加速

root@debase:/etc/systemd/system# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
17eec7bbc9d7: Pull complete 
Digest: sha256:54e66cc1dd1fcb1c3c58bd8017914dbed8701e2d8c74d9262e26bd9cc1642d31
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
root@debase:/etc/systemd/system# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    1b44b5a3e06a   7 weeks ago   10.1kB

六、总结与预告

1、镜像加速

默认情况下,Docker daemon服务会从Docker官网下载镜像image,但由于Docker官网位于中国大陆之外,网络通过质量极不稳定,为便于后续学习可以快速拉取镜像,需要学习Docker如何配置镜像加速。

2、下节课程预告

每天半小时,轻松学Docker第四篇:Docker镜像管理

Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐