200字
docker的安装和删除
2026-02-09
2026-02-09

docker的安装和优化

docker版本说明

官方网址: https://www.docker.com/

Docker版本号之前一直是0.X版本或1.X版本,从2013年3月13日发布第一个版本0.1.1-1开始一直到2017年02月08日发布1.13.1版

从2017年3月1号开始改为每个季度发布一次稳定版,其版本号规则也统一变更为YY.MM.xx,第一个版本为17.03.0, 例如17.09表示是2017年9月份发布的Docker之前没有区分版本,但是2017年推出(将docker更名为)新的项目Moby,Moby项目属于Docker项目的全新上游,Docker将是一个隶属于的Moby的子产品,而且之后的版本之后开始区分为CE(Docker Community Edition,社区版本)和 EE(Docker Enterprise Edition,企业收费版),CE社区版本和EE企业版本都是每个季度发布一个新版本,但是EE版本提供后期安全维护1年,而CE版本是4个月,以下为官方原文:

Docker CE and EE are released quarterly, and CE also has a monthly “Edge” option. Each Docker EE release is supported and maintained for one year and receives security and critical bugfixes during that period. We are also improving Docker CE maintainability by maintaining each quarterly CE release for 4 months. That gets Docker CE users a new 1-month window to update from one version to the
next.
Docker CE和EE每季度发布一次,CE也有每月一次的“Edge”选项。每个Docker EE版本都支持和维护一年,并在此期间接受安全和关键错误修复。我们还通过将每个季度的CE发布保持4个月来提高Docker CE的可维护性。这为Docker CE用户提供了一个新的1个月窗口,可以从一个版本更新到下一个版本。

如果要布署到 kubernetes上,需要查看相关kubernetes对docker版本要求的说明。

安装docker

ubuntu安装docker

#安装基础依赖
[root@nginx1 ~]#apt install ca-certificates curl gnupg
#导入GPG密钥 (现代更倾向于放在/etc/apt/keyrings/ 目录下(不存在需手动创建))
[root@nginx1 ~]#curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
#添加软件源
[root@nginx1 ~]#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
#安装核心组件
[root@nginx1 ~]#apt install docker-ce docker-ce-cli containerd.io

安装指定版本的DOCKER-CE:

#查找DOCKER-CE的版本:
[root@nginx1 ~]#apt-cache madison docker-ce
或者
[root@nginx1 ~]#apt list -a docker-ce

#安装指定版本的docker-ce
[root@nginx1 ~]#apt-install docker-ce=$DOCKER_VER \
            		docker-ce-cli=$DOCKER_VER \
            		containerd.io

#安装完指定版本后 如果你执行完全系统更新`apt upgrade`Docker可能会顺便升级到最新版本 需要锁定一下版本
[root@nginx1 ~]#apt-mark hold docker-ce docker-ce-cli containerd.io
#后续想升级了也可解开
#apt-mark unhold

测试安装是否健康

[root@nginx1 ~]#docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
17eec7bbc9d7: Pull complete 
ea52d2000f90: Download complete 
Digest: sha256:05813aedc15fb7b4d732e1be879d3252c1c9c25d885824f6295cab4538cb85cd
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

或者

[root@nginx1 ~]#docker info
Client: Docker Engine - Community
 Version:    29.2.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.31.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v5.0.2
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 1
 Server Version: 29.2.1
 Storage Driver: overlayfs
  driver-type: io.containerd.snapshotter.v1
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 CDI spec directories:
  /etc/cdi
  /var/run/cdi
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: dea7da592f5d1d2b7755e3a161be07f43fad8f75
 runc version: v1.3.4-0-gd6d73eb8
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.8.0-100-generic
 Operating System: Ubuntu 24.04.3 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 1.922GiB
 Name: nginx1
 ID: c7804bcf-d136-4e98-b679-18b5d07d8058
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  ::1/128
  127.0.0.0/8
 Live Restore Enabled: false
 Firewall Backend: iptables

测试完一切后的设置

#将当前用户添加到docker的用户组 免去了一直sudo或者切换root的麻烦事儿
[root@nginx1 ~]#usermod -aG docker $USER
#原地生效 如果不想执行可以注销再登录(重启终端)
[root@nginx1 ~]#newgrp docker

docker优化配置

注意:这种只对新建的容器有效,之前的容器不生效

[root@nginx1 ~]#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.m.daocloud.io"
  ],
  "data-root": "/date/docker",		#指定docker的根目录
  "exec-opts": ["native.cgroupdriver=systemd"],  #强制docker使用systemd管理资源
  "log-driver": "json-file",	#指定日志的驱动程序记录在本地JSON文件中
  "log-opts": {				#日志滚动策略
    "max-size": "100m",		#限制单个文件最大100M
    "max-file": "3"			#限制只保留3个日志文件
  },
  "max-concurrent-downloads": 10,	#设置最大并发下载数
  "max-concurrent-uploads": 5,		#设置最大并发上传数
  "live-restore": true,				#活体恢复功能 正常情况下重启docker容器会一起down掉 开启此项之后不会中断容器的运行
  "metrics-addr": "127.0.0.1:9323",	#暴露Docker的内部监控指标接口 可以改成内网网段供Prometheus
  "experimental": false				#关闭试验性功能 生产环境不是玩闹
}
   
[root@nginx1 ~]#systemctl daemon-reload && systemctl restart docker
[root@nginx1 ~]#systemctl enable docker

检验是否成功

[root@nginx1 ~]#docker info | grep -i "Cgroup Driver"
#如果失败了 用下面的命令查看日志
[root@nginx1 ~]#journalctl -u docker.service -n 20 --no-pager

删除docker

[root@nginx1 ~]#apt purge docker-ce		#这个办法会把/etc/docker/daemon.json 等配置文件删除 如果你不想删除请改成remove
[root@nginx1 ~]#rm -rf /var/lib/docker

评论