CentOS7 安装 Docker

1,950 阅读2分钟

一、安装前必读

在安装 Docker 之前,先说一下配置,我是Centos7 Linux 内核。 官方建议 3.10 以上。

注意:本文的命令使用的是 root 用户登录执行,不是 root 的话所有命令前面要加 sudo

1.查看当前的内核版本

uname -r

示例:

$ uname -r
3.10.0-1160.105.1.el7.x86_64

我这里是3.10 ,满足条件。

2.更新yum包:

注意:使用 root 权限更新 yum 包(生产环境中此步操作需慎重,看自己情况,学习的话随便搞)

yum -y update

这个命令不是必须执行的,看个人情况,后面出现不兼容的情况的话就必须update了

注意:

yum -y update:升级所有包同时也升级软件和系统内核;​ 
yum -y upgrade:只升级所有包,不升级软件和系统内核

3.卸载旧版本(如果之前安装过的话)

yum remove docker  docker-common docker-selinux docker-engine

示例:

$ yum remove docker  docker-common docker-selinux docker-engine
已加载插件:fastestmirror, langpacks
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
参数 docker 没有匹配
参数 docker-common 没有匹配
参数 docker-selinux 没有匹配
参数 docker-engine 没有匹配
不删除任何软件包

二、安装Docker

1.安装依赖软件包

yum-util 提供yum-config-manager功能,另两个是devicemapper驱动依赖**

yum install -y yum-utils device-mapper-persistent-data lvm2

2.设置 yum 源

设置一个yum源,下面两个都可用,建议使用阿里仓库。

yum-config-manager --add-repo http://download.docker.com/linux/centos/docker-ce.repo(中央仓库)

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo(阿里仓库)

3.选择Docker版本并安装

(1)查看可用版本有哪些

yum list docker-ce --showduplicates | sort -r

示例:

$ yum list docker-ce --showduplicates | sort -r
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
已加载插件:fastestmirror, langpacks
可安装的软件包
 * updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
 * extras: mirrors.aliyun.com
docker-ce.x86_64            3:25.0.3-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.2-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.1-1.el7                      docker-ce-stable
...  ...
docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable
 * base: mirrors.aliyun.com

(2)选择一个版本并安装

yum install docker-ce-版本号

yum -y install docker-ce-18.03.1.ce

示例:(安装25版)

yum -y install docker-ce-25.0.3

4.启动 Docker 并设置开机自启

systemctl start docker
systemctl enable docker

5.查看Docker版本

docker version

示例(如下输出表示安装成功)

$ docker version
Client: Docker Engine - Community
 Version:           25.0.3
 API version:       1.44
 Go version:        go1.21.6
 Git commit:        4debf41
 Built:             Tue Feb  6 21:17:10 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          25.0.3
  API version:      1.44 (minimum version 1.24)
  Go version:       go1.21.6
  Git commit:       f417435
  Built:            Tue Feb  6 21:16:08 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

6.测试 hello-world

使用hello world 镜像进行测试

1)拉取镜像

docker pull hello-world

2)运行镜像

docker run hello-world

如下表示运行成功:

image.png