CentOS 安装Docker

参考源

https://www.bilibili.com/video/BV187411y7hF?spm_id_from=333.999.0.0

版本

本文章基于 CentOS 7.6


这里使用 yum 方式安装 Docker

简介

yum 是 Linux 的在线安装方式,是比较方便的。

由于 yum 是在线安装,需要从网络获取数据,所以得先保证系统是能联网的,否则一定会安装失败。

这里使用安装 Docker 作为示例。

安装

官网安装参考手册

https://docs.docker.com/install/linux/docker-ce/centos/

命令

yum install 程序名

参数

-y:所有的 yes / no 都选择 yes,确定安装时都选择 yes 时可以使用,可以省去一些选择步骤。

系统版本检测

本次示例基于 CentOS 7 进行演示,所以需要先确认系统的版本。

cat /etc/redhat-release

1、卸载以前的 Docker

为避免版本之间的冲突,这里建议先卸载以前的版本再进行安装。

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

由于这里之前没有安装过 Docker,所以显示没有匹配到。

2、安装运行环境

Docker 是用 Go 语言开发的。

Go 语言基于 CC++,所以需要先安装 CC++ 环境。

安装 C 环境

yum -y install gcc

安装 C++ 环境

yum -y install gcc-c++

3、环境准备

根据 Docker 官网的建议,需要再安装一些环境。

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

4、设置镜像仓库

这里官网建议我们安装国外的镜像仓库。

由于对国外的网络限制,国内安装的话会非常慢,并很有可能失败。

推荐国内用户安装阿里云镜像。

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

5、更新 yum 软件包索引

这里官网建议我们更新 yum 软件包索引:

yum makecache fast

6、安装 Docker

做完前面的准备工作,就可以开始安装 Docker 了

yum -y install docker-ce docker-ce-cli containerd.io

这里的 docker-ce 指的是 Docker 的社区版。

如果是需要企业版的话就改为 docker-ee ,不过企业版是收费的,而且功能也略有不同。

官方更推荐社区版,所以这里我们安装docker-ce

7、启动 Docker

Docker 安装完成后,就可以运行如下命令启动。

systemctl start docker

测试

启动 Docker 后,我们可以运行如下命令对 Docker 进行测试。

查看 Docker 进程

[root@sail ~]# ps -ef|grep docker
root     18805     1  0 16:54 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root     19233 16612  0 16:59 pts/0    00:00:00 grep --color=auto docker

由此可以看出,Docker 是启动成功了的。

查看版本信息

[root@sail ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.11
 API version:       1.41
 Go version:        go1.16.9
 Git commit:        dea9396
 Built:             Thu Nov 18 00:38:53 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.11
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.9
  Git commit:       847da18
  Built:            Thu Nov 18 00:37:17 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

可以看出 Docker 的版本为 20.10.11

运行 Hello World 镜像

Hello World 镜像是 Docker 官方为测试 Docker 环境和运行情况所设定的一个程序。

docker run hello-world
[root@sail ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
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/

由以下结果可以看出,我们的 Docker 工作环境是搭建完毕了的。

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

查看镜像

在执行启动 Hello World 镜像的命令时,如果本地没有该镜像,会先从镜像库中拉取该镜像再启动。

使用 docker images 查看目前的 Docker 的镜像情况:

[root@sail ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB

这里我们可以看到刚拉取的 hello-world 镜像,以及它的相关信息。

阿里云镜像加速

如果是阿里云的服务器,还可以使用阿里云的镜像加速使得 Docker 使用起来更加快速和高效。

打开阿里云服务器列表,选择如图所示的容器镜像服务

选择如图所示的镜像加速器,然后选择对应的服务器系统。

在服务器上依次执行上图所示的 4 条命令即可实现阿里云的镜像加速。

至此,yum 方式安装 Docker 完毕。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天航星

感谢你的鼓励和认可

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值