1
2
3
4
5
作者:李晓辉

微信联系:lxh_chat

联系邮箱: 939958092@qq.com

最近是不是经常听人提到 Docker?什么“容器化”、“DevOps”、“一键部署”……听起来高大上,其实说白了,它就是个帮我们打包应用、快速部署的利器。

我刚接触的时候也有点懵,光是“怎么装”就查了一堆资料,结果不是版本不兼容就是装完跑不动。😭 所以我决定整理一篇超级简单、一步步教你安装 Docker 的指南,让你也能轻松上手,顺利跑出人生第一个容器!

这篇文章不搞复杂术语,咱就从最基本的环境准备讲起,一步步装好 Docker,然后运行一个小容器,看它乖乖动起来!

准备好了?那就开干吧!

先决条件

在正式开整之前,先来检查下“地基”是不是结实。你得先有台能跑 Docker 的机器嘛(废话 🤣)。

Docker 这家伙虽然很强,但也不是谁都能跑得动。以下这些系统它支持得贼好:

  • Ubuntu

  • Debian

  • CentOS / RHEL(适合企业用)

  • Fedora(走在技术前沿的发烧友专属)

我们本次采用RockyLinux 9.4作为运行平台,RockyLinux是基于RHEL平台的

安装方法

你可以先配置好 Docker 的官方软件源,然后直接从里面安装和升级。这是最推荐的方式,简单省事,后续版本更新也方便,这也是我们本次采用的方法。

1
2
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo

当然,如果你是在那种无法联网的环境(比如内网、隔离系统)下安装 Docker,也可以手动下载 RPM 包,一个个装上去,后面升级也得你自己动手管理。这种方式虽然稍麻烦,但在“全靠自己不靠网”的场景下非常实用。

软件包可以从这里下载:

1
https://download.docker.com/linux/rhel/

既然我们采用官方软件源的方法,我们就从仓库里安装吧~

1
2
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo

在国内访问速度慢,如果慢,可以用下面的方法加速

1
sed -i 's+https://download.docker.com+https://mirror.nju.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
1
sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

启动服务

1
sudo systemctl enable --now docker

测试docker安装结果

如果安装成功,我们应该可以运行一个容器才对,不过在中国,由于国情问题,无法直接从docker官方下载镜像,就像你无法访问Google一样的原因,所以我run后面指定镜像的时候,指定了我自己的加速器,如果你也希望能在中国不限速的下载容器镜像,可以点此获取容器镜像加速器用户名和密码

先登录一下容器镜像加速器

1
2
3
4
5
6
7
8
9
[root@ceph ~]# docker login registry.myk8s.cn/library/hello-world
Username: XXXX
Password:

WARNING! Your credentials are stored unencrypted in '/root/.docker/config.json'.
Configure a credential helper to remove this warning. See
https://docs.docker.com/go/credential-store/

Login Succeeded

然后运行一个容器

1
docker run registry.myk8s.cn/library/hello-world

成功看到这段“Hello from Docker!”输出,说明你已经顺利把 Docker 搞定啦,快去朋友圈炫耀一下吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Unable to find image 'registry.myk8s.cn/library/hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete
Digest: sha256:c41088499908a59aae84b0a49c70e86f4731e588a737f1637e73c8c09d995654
Status: Downloaded newer image for registry.myk8s.cn/library/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 成功安装了,你已经迈出了容器化之路的第一步。下一篇,我会带你构建自己的第一个镜像,打造属于自己的“小容器宇宙”~🚀