【Docker】7.Docker安装

📅 2026/8/1 22:23:11 👁️ 阅读次数 📝 编程学习
【Docker】7.Docker安装

文章目录

  • 1. 各版本平台支持情况
    • 1.1 Server 版本
    • 1.2 桌面版本
  • 2. Server 版本安装
    • 2.1 Ubuntu 安装
      • 2.1.1 安装依赖
      • 2.1.2 安装 docker
      • 2.1.3 Docker 镜像源修改
      • 2.1.4 Docker 目录修改
    • 2.2 CentOS 安装
      • 2.2.1 安装依赖
      • 2.2.2 安装 Docker
      • 2.2.3 Docker 镜像源修改
      • 2.2.4 Docker 目录修改

1. 各版本平台支持情况

1.1 Server 版本


1.2 桌面版本


2. Server 版本安装

2.1 Ubuntu 安装

2.1.1 安装依赖

操作系统版本

Ubuntu Kinetic22.10Ubuntu Jammy22.04(LTS)Ubuntu Focal20.04(LTS)Ubuntu Bionic18.04(LTS)

CPU 支持

ARM 和 X86_64

2.1.2 安装 docker

确定CPU,可以看到我们的是X86_64,是支持的,如果是arm一般会显示aarch64

root@yudukai:/# uname -aLinux yudukai5.15.0-60-generic#66-Ubuntu SMP Fri Jan 20 14:29:49 UTC 2023 x86_64 x86_64 x86_64 GNU/Linuxroot@yudukai:/#

确定操作系统版本,本次我们使用的是Ubuntu 22.04

root@yudukai:/# cat /etc/*release*DISTRIB_ID=UbuntuDISTRIB_RELEASE=22.04DISTRIB_CODENAME=jammyDISTRIB_DESCRIPTION="Ubuntu 22.04.3 LTS"PRETTY_NAME="Ubuntu 22.04.3 LTS"NAME="Ubuntu"VERSION_ID="22.04"VERSION="22.04.3 LTS (Jammy Jellyfish)"VERSION_CODENAME=jammyID=ubuntuID_LIKE=debianHOME_URL="https://www.ubuntu.com/"SUPPORT_URL="https://help.ubuntu.com/"BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"UBUNTU_CODENAME=jammy root@yudukai:/#

卸载旧版本,如果是新购买的云服务器是没有的,比如输入docker并没有这个命令,就不需要卸载

我刚好之前装了,就删了重下:

root@yudukai:/# sudo apt-get remove docker docker-engine docker.io containerd runcReading package lists... Done Building dependency tree... Done Reading state information... Done Package'docker-engine'is not installed, so not removed Package'docker'is not installed, so not removed Package'containerd'is not installed, so not removed Package'runc'is not installed, so not removed Package'docker.io'is not installed, so not removed0upgraded,0newly installed,0to remove and326not upgraded. root@yudukai:/#

卸载历史版本:

# 1. 卸载 Docker 软件包及其配置sudoapt-getpurge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras-y# 2. 删除 Docker 数据目录(会清除所有镜像、容器和卷,请谨慎操作)sudorm-rf/var/lib/dockersudorm-rf/var/lib/containerd# 3. 删除自定义数据目录(根据注释,这是老师环境特有的,如果您的系统没有这个目录,执行也不会报错)sudorm-rf/data/var/lib/docker# 4. 删除 Docker 配置文件sudorm-rf/etc/docker/daemon.json

验证删除干净:

root@yudukai:/# docker --version-bash: /usr/bin/docker: No suchfileor directory root@yudukai:/# systemctl status dockerUnit docker.service could not be found. root@yudukai:/# ls /var/lib/dockerls: cannot access'/var/lib/docker':No suchfileor directory root@yudukai:/# ls /var/lib/containerdls: cannot access'/var/lib/containerd':No suchfileor directory root@yudukai:/# ip addr show docker03: docker0:<NO-CARRIER,BROADCAST,MULTICAST,UP>mtu1500qdisc noqueue state DOWN group default link/ether 4a:0f:be:09:8e:ac brd ff:ff:ff:ff:ff:ff inet172.17.0.1/16 brd172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::480f:beff:fe09:8eac/64 scopelinkvalid_lft forever preferred_lft forever root@yudukai:/#

可以看到网卡没删干净

# 1. 停止并删除网卡sudoiplinkdelete docker0# 2. 再次验证ipaddr show docker0
root@yudukai:/# sudo ip link delete docker0root@yudukai:/# ip addr show docker0Device"docker0"does not exist. root@yudukai:/#

配置docker下载源

# 1. 安装 curl 工具sudoaptinstallcurl-y# 2. 创建 GPG 密钥目录并设置权限sudomkdir-m0755-p/etc/apt/keyrings# 3. 下载并添加 Docker 的官方 GPG 密钥curl-fsSLhttps://download.docker.com/linux/ubuntu/gpg|sudogpg--dearmor--yes-o/etc/apt/keyrings/docker.gpg# 4. 添加 Docker APT 仓库echo\"deb [arch=$(dpkg --print-architecture)signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \$(lsb_release-cs)stable"|\sudotee/etc/apt/sources.list.d/docker.list>/dev/null

安装

# 更新软件包列表# 执行此命令会从刚刚配置好的 Docker 官方源下载最新的软件包索引# 这一步是必须的,否则系统找不到 docker-ce 等软件包sudoapt-getupdate# 安装 Docker 核心组件及插件# docker-ce: Docker 社区版引擎(核心服务端)# docker-ce-cli: Docker 命令行工具(客户端,让我们能执行 docker 命令)# containerd.io: 容器运行时,负责管理容器的生命周期# docker-buildx-plugin: Docker 构建扩展,支持更强大的构建功能(如多平台构建)# docker-compose-plugin: Docker Compose v2 插件,支持使用 "docker compose" 命令# -y: 自动确认安装,无需手动输入 "y"sudoapt-getinstalldocker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin-y

自动启动配置:

#配置加载sudosystemctl daemon-reload#启动服务sudosystemctl startdocker#开启启动sudosystemctlenabledocker#查看服务状态sudosystemctl statusdocker

检查安装结果查看版本

root@yudukai:/# docker versionClient: Docker Engine - Community Version:29.6.2 API version:1.55Go version: go1.26.5 Git commit: dfc4efb Built: Thu Jul1616:12:232026OS/Arch: linux/amd64 Context: default Server: Docker Engine - Community Engine: Version:29.6.2 API version:1.55(minimum version1.40)Go version: go1.26.5 Git commit: 3d80467 Built: Thu Jul1616:12:232026OS/Arch: linux/amd64 Experimental:falsecontainerd: Version: v2.2.6 GitCommit: 11ce9d5f3c68c941867e82890e93e815c1304f1b runc: Version:1.3.6 GitCommit: v1.3.6-0-g491b69ba docker-init: Version:0.19.0 GitCommit: de40ad0 root@yudukai:/#

更详细查看docker信息:

root@yudukai:/# docker infoClient: Docker Engine - Community Version:29.6.2 Context: default Debug Mode:falsePlugins: buildx: Docker Buildx(Docker Inc.)Version: v0.36.0 Path: /usr/libexec/docker/cli-plugins/docker-buildx compose: Docker Compose(Docker Inc.)Version: v5.3.1 Path: /usr/libexec/docker/cli-plugins/docker-compose Server: Containers:0Running:0Paused:0Stopped:0Images:0Server Version:29.6.2 Storage Driver: overlayfs driver-type: io.containerd.snapshotter.v1 Logging Driver: json-file Cgroup Driver: systemd Cgroup Version:2Plugins: Volume:localNetwork: bridgehostipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-filelocalsplunk 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: 11ce9d5f3c68c941867e82890e93e815c1304f1b runc version: v1.3.6-0-g491b69ba init version: de40ad0 Security Options: apparmor seccomp Profile:builtincgroupns Kernel Version:5.15.0-60-generic Operating System: Ubuntu22.04.3 LTS OSType: linux Architecture: x86_64 CPUs:4Total Memory:7.763GiB Name: yudukai ID: 6874442b-fe2d-4a41-b0c4-4e943e19294e Docker Root Dir: /var/lib/docker Debug Mode:falseExperimental:falseInsecure Registries: ::1/128127.0.0.0/8 Live Restore Enabled:falseFirewall Backend: iptables EnableUserlandProxy:trueUserlandProxyPath: /usr/bin/docker-proxy root@yudukai:/#

执行hello-world可以看到Hello from Docker,表面docker服务正常

sudodockerrun hello-world
root@yudukai:/# sudo docker run hello-worldUnable tofindimage'hello-world:latest'locally docker: Error response from daemon: failed to resolve reference"docker.io/library/hello-world:latest":failed todorequest: Head"https://registry-1.docker.io/v2/library/hello-world/manifests/latest":dial tcp199.16.156.75:443: i/otimeoutRun'docker run --help'formoreinformation root@yudukai:/#

上面这个,就是一个典型的**Docker镜像拉取超时问题**,主要原因是国内访问Docker Hub官方仓库网络不稳定。


2.1.3 Docker 镜像源修改

对于使用systemd的系统(Ubuntu 16.04+、Debian 8+、CentOS 7), 如果是腾讯云在配置文件/etc/docker/daemon.json中加入

{"registry-mirrors":["https://mirror.ccs.tencentyun.com"]}

如果是阿里云,因为只对企业开放海外代理,只能找私人的一些开放的源,目前可以使用的源如下:

{"registry-mirrors":["https://docker.m.daocloud.io","https://dockerhub.timeweb.cloud","https://huecker.io"]}

如果这样没有文件,就创建一下:

root@yudukai:/# cd /etc/docker/root@yudukai:/etc/docker# cat daemon.jsoncat: daemon.json: No suchfileor directory root@yudukai:/etc/docker#
sudotee/etc/docker/daemon.json>/dev/null<<EOF { "registry-mirrors": [ "https://docker.m.daocloud.io", "https://dockerhub.timeweb.cloud", "https://huecker.io" ] } EOF

重新启动dockerd

sudosystemctl daemon-reloadsudosystemctl restartdocker

继续实验:sudo docker run hello-world

root@yudukai:/etc/docker# sudo docker run hello-worldUnable tofindimage'hello-world:latest'locally latest: Pulling from library/hello-world d5e71e642bf5: Download complete 4f55086f7dd0: Pull complete Digest: sha256:c3cbe1cc1aa588a64951ac6286e0df7b27fe2e6324b1001c619bb358770c0178 Status: Downloaded newer imageforhello-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 imagewhichruns the executable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client,whichsent it to your terminal. To try somethingmoreambitious, you can run an Ubuntu container with: $dockerrun-itubuntubashShare images, automate workflows, andmorewith afreeDocker ID: https://hub.docker.com/ Formoreexamples and ideas, visit: https://docs.docker.com/get-started/ root@yudukai:/etc/docker#

2.1.4 Docker 目录修改

Docker默认的安装目录为/var/lib/docker,这里面会存放很多很多镜像,所以我们在安装的时候需要考虑这个目录的空间,有三种解决方案。

  1. /var/lib/docker挂载到一个大的磁盘,这种一般我们能控制挂载目录,像腾讯云这种云厂商在安装K8s的节点的时候提供了挂载选项,可以直接挂载这个目录过去
  2. 安装之前挂载一个大的磁盘,然后创建一个软链接到/var/lib/docker,这样就自动安装到我们空间比较大的磁盘了
  3. 安装了docker,然后发现忘了配置这个目录,我们需要修改docker的配置文件
#假定我们磁盘的大的目录为 /datamkdir-p/data/var/lib/docker# 编辑配置文件vi/etc/docker/daemon.json# 输入下面的 json{"data-root":"/data/var/lib/docker"}# 加载配置sudosystemctl daemon-reload# 重启 dockersudosystemctl restartdocker#查看 docker 状态sudosystemctl statusdocker

不确定自己要挂载的目录可以先用命令看一下:

root@yudukai:/var/lib/docker# df -hFilesystem Size Used Avail Use% Mounted on tmpfs 795M 78M 718M10% /run /dev/vda1 177G 23G 148G14% / tmpfs3.9G03.9G0% /dev/shm tmpfs5.0M05.0M0% /run/lock tmpfs 795M0795M0% /run/user/0 /dev/loop03.5M 192K2.8M7% /data/testmymount root@yudukai:/var/lib/docker#

因为我的这个磁盘空间足够,我就不更改了。

如果要更改的话,更改的时候记得把docker服务先停掉,改完了再开启。


2.2 CentOS 安装

2.2.1 安装依赖

支持的操作系统

CentOS7-- 停止更新已经不再支持 CentOS8(stream)CentOS9(stream)

支持的CPU

ARM/X86_64

2.2.2 安装 Docker

确认操作系统

cat/etc/*release*

确认CPU架构

uname-a

卸载旧版本

sudoyum removedocker\docker-client\docker-client-latest\docker-common\docker-latest\docker-latest-logrotate\docker-logrotate\docker-engine

卸载历史版本

#删除机器上的包sudoyum remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras#执行卸载sudorm-rf/var/lib/dockersudorm-rf/var/lib/containerd#这个是修改后的目录,根据实际情况设置sudorm-rf/data/var/lib/dockersudorm-rf/etc/docker/daemon.json

配置仓库

[root@centos1 ~]# ll /etc/yum.repos.d/total40-rw-r--r--.1root root1664Nov232020CentOS-Base.repo -rw-r--r--.1root root1309Nov232020CentOS-CR.repo -rw-r--r--.1root root649Nov232020CentOS-Debuginfo.repo -rw-r--r--.1root root314Nov232020CentOS-fasttrack.repo -rw-r--r--.1root root630Nov232020CentOS-Media.repo -rw-r--r--.1root root1331Nov232020CentOS-Sources.repo -rw-r--r--.1root root8515Nov232020CentOS-Vault.repo -rw-r--r--.1root root616Nov232020CentOS-x86_64-kernel.repo[root@centos1 ~]# sudo yum install -y yum-utils[root@centos1 ~]# sudo yum-config-manager --add-repohttps://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo[root@centos1 ~]# ll /etc/yum.repos.d/total44-rw-r--r--.1root root1664Nov232020CentOS-Base.repo -rw-r--r--.1root root1309Nov232020CentOS-CR.repo -rw-r--r--.1root root649Nov232020CentOS-Debuginfo.repo -rw-r--r--.1root root314Nov232020CentOS-fasttrack.repo -rw-r--r--.1root root630Nov232020CentOS-Media.repo -rw-r--r--.1root root1331Nov232020CentOS-Sources.repo -rw-r--r--.1root root8515Nov232020CentOS-Vault.repo -rw-r--r--.1root root616Nov232020CentOS-x86_64-kernel.repo -rw-r--r--.1root root1919Apr507:45 docker-ce.repo

安装最新版本

sudoyuminstall-ydocker-ce

启动docker

#配置加载sudosystemctl daemon-reload#启动服务sudosystemctl startdocker#开启启动sudosystemctlenabledocker#查看服务状态sudosystemctl statusdocker

检查安装结果查看版本

[root@centos1 ~]# docker versionClient: Docker Engine - Community Version:23.0.3 API version:1.42Go version: go1.19.7 Git commit: 3e7cbfd Built: Tue Apr422:04:182023OS/Arch: linux/amd64 Context: default Server: Docker Engine - Community Engine: Version:23.0.3 API version:1.42(minimum version1.12)Go version: go1.19.7 Git commit: 59118bf Built: Tue Apr422:02:012023OS/Arch: linux/amd64 Experimental:falsecontainerd: Version:1.6.20 GitCommit: 2806fc1057397dbaeefbea0e4e17bddfbd388f38 runc: Version:1.1.5 GitCommit: v1.1.5-0-gf19387a docker-init: Version:0.19.0 GitCommit: de40ad0

更详细查看docker信息

[root@centos1 ~]# docker infoClient: Context: default Debug Mode:falsePlugins: buildx: Docker Buildx(Docker Inc.)Version: v0.10.4 Path: /usr/libexec/docker/cli-plugins/docker-buildx compose: Docker Compose(Docker Inc.)Version: v2.17.2 Path: /usr/libexec/docker/cli-plugins/docker-compose Server: Containers:0Running:0Paused:0Stopped:0Images:0Server Version:23.0.3 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type:trueUsing metacopy:falseNative Overlay Diff:trueuserxattr:falseLogging Driver: json-file Cgroup Driver: cgroupfs Cgroup Version:1Plugins: Volume:localNetwork: bridgehostipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-filelocallogentries splunk syslog Swarm: inactive Runtimes: io.containerd.runc.v2 runc Default Runtime: runc Init Binary: docker-init containerd version: 2806fc1057397dbaeefbea0e4e17bddfbd388f38 runc version: v1.1.5-0-gf19387a init version: de40ad0 Security Options: seccomp Profile:builtinKernel Version:3.10.0-1160.71.1.el7.x86_64 Operating System: CentOS Linux7(Core)OSType: linux Architecture: x86_64 CPUs:2Total Memory:1.795GiB Name: centos1 ID: 0b3c79d5-957d-4d04-a856-ac15a2a09db2 Docker Root Dir: /var/lib/docker Debug Mode:falseRegistry: https://index.docker.io/v1/ Experimental:falseInsecure Registries:127.0.0.0/8 Live Restore Enabled:false

执行hello-world可以看到Hello from Docker,表面docker服务正常

[root@centos1 ~]# sudo docker run hello-worldUnable tofindimage'hello-world:latest'locally latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:ffb13da98453e0f04d33a6eee5bb8e46ee50d08ebe17735fc0779d0349e889e9 Status: Downloaded newer imageforhello-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 imagewhichruns the executable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client,whichsent it to your terminal. To try somethingmoreambitious, you can run an Ubuntu container with: $dockerrun-itubuntubashShare images, automate workflows, andmorewith afreeDocker ID: https://hub.docker.com/ Formoreexamples and ideas, visit: https://docs.docker.com/get-started/

2.2.3 Docker 镜像源修改

对于使用systemd的系统(Ubuntu 16.04+、Debian 8+、CentOS 7), 如果是腾讯云在配置文件/etc/docker/daemon.json中加入

{"registry-mirrors":["https://mirror.ccs.tencentyun.com"]}

如果是阿里云,因为只对企业开放海外代理,只能找私人的一些开放的源,目前可以使用的源如下,可能会失效

{"registry-mirrors":["https://docker.m.daocloud.io","https://dockerhub.timeweb.cloud","https://huecker.io"]}

2.2.4 Docker 目录修改

Docker默认的安装目录为/var/lib/docker,这里面会存放很多很多镜像,所以我们在安装的时候需要考虑这个目录的空间,有三种解决方案。

  1. /var/lib/docker挂载到一个大的磁盘,这种一般我们能控制挂载目录,像腾讯云这种云厂商在安装 K8s 的节点的时候提供了挂载选项,可以直接挂载这个目录过去
  2. 安装之前挂载一个大的磁盘,然后创建一个软链接到/var/lib/docker,这样就自动安装到我们空间比较大的磁盘了
  3. 安装了docker,然后发现忘了配置这个目录,我们需要修改docker的配置文件
#假定我们磁盘的大的目录为 /datamkdir-p/data/var/lib/docker# 编辑配置文件vi/etc/docker/daemon.json# 输入下面的 json{"data-root":"/data/var/lib/docker"}# 加载配置sudosystemctl daemon-reload# 重启 dockersudosystemctl restartdocker#查看 docker 状态sudosystemctl statusdocker