【Docker】7.Docker安装
文章目录1. 各版本平台支持情况1.1 Server 版本1.2 桌面版本2. Server 版本安装2.1 Ubuntu 安装2.1.1 安装依赖2.1.2 安装 docker2.1.3 Docker 镜像源修改2.1.4 Docker 目录修改2.2 CentOS 安装2.2.1 安装依赖2.2.2 安装 Docker2.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_642.1.2 安装 docker确定CPU可以看到我们的是X86_64是支持的如果是arm一般会显示aarch64rootyudukai:/# 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/Linuxrootyudukai:/#确定操作系统版本本次我们使用的是Ubuntu 22.04rootyudukai:/# cat /etc/*release*DISTRIB_IDUbuntuDISTRIB_RELEASE22.04DISTRIB_CODENAMEjammyDISTRIB_DESCRIPTIONUbuntu 22.04.3 LTSPRETTY_NAMEUbuntu 22.04.3 LTSNAMEUbuntuVERSION_ID22.04VERSION22.04.3 LTS (Jammy Jellyfish)VERSION_CODENAMEjammyIDubuntuID_LIKEdebianHOME_URLhttps://www.ubuntu.com/SUPPORT_URLhttps://help.ubuntu.com/BUG_REPORT_URLhttps://bugs.launchpad.net/ubuntu/PRIVACY_POLICY_URLhttps://www.ubuntu.com/legal/terms-and-policies/privacy-policyUBUNTU_CODENAMEjammy rootyudukai:/#卸载旧版本如果是新购买的云服务器是没有的比如输入docker并没有这个命令就不需要卸载我刚好之前装了就删了重下rootyudukai:/# sudo apt-get remove docker docker-engine docker.io containerd runcReading package lists... Done Building dependency tree... Done Reading state information... Done Packagedocker-engineis not installed, so not removed Packagedockeris not installed, so not removed Packagecontainerdis not installed, so not removed Packageruncis not installed, so not removed Packagedocker.iois not installed, so not removed0upgraded,0newly installed,0to remove and326not upgraded. rootyudukai:/#卸载历史版本# 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验证删除干净rootyudukai:/# docker --version-bash: /usr/bin/docker: No suchfileor directory rootyudukai:/# systemctl status dockerUnit docker.service could not be found. rootyudukai:/# ls /var/lib/dockerls: cannot access/var/lib/docker:No suchfileor directory rootyudukai:/# ls /var/lib/containerdls: cannot access/var/lib/containerd:No suchfileor directory rootyudukai:/# ip addr show docker03: docker0:NO-CARRIER,BROADCAST,MULTICAST,UPmtu1500qdisc 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 rootyudukai:/#可以看到网卡没删干净# 1. 停止并删除网卡sudoiplinkdelete docker0# 2. 再次验证ipaddr show docker0rootyudukai:/# sudo ip link delete docker0rootyudukai:/# ip addr show docker0Devicedocker0does not exist. rootyudukai:/#配置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: 自动确认安装无需手动输入 ysudoapt-getinstalldocker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin-y自动启动配置#配置加载sudosystemctl daemon-reload#启动服务sudosystemctl startdocker#开启启动sudosystemctlenabledocker#查看服务状态sudosystemctl statusdocker检查安装结果查看版本rootyudukai:/# 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 rootyudukai:/#更详细查看docker信息rootyudukai:/# 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 rootyudukai:/#执行hello-world可以看到Hello from Docker表面docker服务正常sudodockerrun hello-worldrootyudukai:/# sudo docker run hello-worldUnable tofindimagehello-world:latestlocally docker: Error response from daemon: failed to resolve referencedocker.io/library/hello-world:latest:failed todorequest: Headhttps://registry-1.docker.io/v2/library/hello-world/manifests/latest:dial tcp199.16.156.75:443: i/otimeoutRundocker run --helpformoreinformation rootyudukai:/#上面这个就是一个典型的**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]}如果这样没有文件就创建一下rootyudukai:/# cd /etc/docker/rootyudukai:/etc/docker# cat daemon.jsoncat: daemon.json: No suchfileor directory rootyudukai:/etc/docker#sudotee/etc/docker/daemon.json/dev/nullEOF { registry-mirrors: [ https://docker.m.daocloud.io, https://dockerhub.timeweb.cloud, https://huecker.io ] } EOF重新启动dockerdsudosystemctl daemon-reloadsudosystemctl restartdocker继续实验sudo docker run hello-worldrootyudukai:/etc/docker# sudo docker run hello-worldUnable tofindimagehello-world:latestlocally 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 thehello-worldimage 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/ rootyudukai:/etc/docker#2.1.4 Docker 目录修改Docker默认的安装目录为/var/lib/docker这里面会存放很多很多镜像所以我们在安装的时候需要考虑这个目录的空间有三种解决方案。将/var/lib/docker挂载到一个大的磁盘这种一般我们能控制挂载目录像腾讯云这种云厂商在安装K8s的节点的时候提供了挂载选项可以直接挂载这个目录过去安装之前挂载一个大的磁盘然后创建一个软链接到/var/lib/docker这样就自动安装到我们空间比较大的磁盘了安装了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不确定自己要挂载的目录可以先用命令看一下rootyudukai:/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 rootyudukai:/var/lib/docker#因为我的这个磁盘空间足够我就不更改了。如果要更改的话更改的时候记得把docker服务先停掉改完了再开启。2.2 CentOS 安装2.2.1 安装依赖支持的操作系统CentOS7-- 停止更新已经不再支持 CentOS8(stream)CentOS9(stream)支持的CPUARM/X86_642.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配置仓库[rootcentos1 ~]# 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[rootcentos1 ~]# sudo yum install -y yum-utils[rootcentos1 ~]# sudo yum-config-manager --add-repohttps://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo[rootcentos1 ~]# 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检查安装结果查看版本[rootcentos1 ~]# 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信息[rootcentos1 ~]# 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服务正常[rootcentos1 ~]# sudo docker run hello-worldUnable tofindimagehello-world:latestlocally 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 thehello-worldimage 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这里面会存放很多很多镜像所以我们在安装的时候需要考虑这个目录的空间有三种解决方案。将/var/lib/docker挂载到一个大的磁盘这种一般我们能控制挂载目录像腾讯云这种云厂商在安装 K8s 的节点的时候提供了挂载选项可以直接挂载这个目录过去安装之前挂载一个大的磁盘然后创建一个软链接到/var/lib/docker这样就自动安装到我们空间比较大的磁盘了安装了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