Centos 7.6安装Docker

本文详细介绍了如何在腾讯云轻量服务器上的CentOS7.6系统中安装Docker,包括卸载旧版本、安装依赖、设置阿里云镜像仓库、安装Docker引擎、启动Docker、验证安装并执行hello-world测试。此外,还讲述了如何配置腾讯云的镜像加速源,确保高效地拉取Docker镜像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我是在腾讯云轻量服务器上安装的,参考的是docker的官网的教程,链接:Install Docker Engine on CentOS | Docker DocumentationInstructions for installing Docker Engine on CentOShttps://docs.docker.com/engine/install/centos/

其中需要注意的是不能使用官方的镜像源,命令中的sudo是以管理员运行命令。安装过程如下:

1. 卸载docker的旧版本

[root@VM-4-16-centos ~]# sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

 2. yum安装gcc相关

依次执行一下两条命令

[root@VM-4-16-centos ~]# yum -y install gcc

[root@VM-4-16-centos ~]# yum -y install gcc c++

3. 安装软件包和国内的镜像仓库

分别执行下面的两条命令

[root@VM-4-16-centos ~]# sudo yum install -y yum-utils

[root@VM-4-16-centos ~]# sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4. 下载阿里云仓库

执行这个步骤是为了处理后续安装docker-ce时有些包下不了的问题

[root@VM-4-16-centos ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

5. 安装docker引擎

[root@VM-4-16-centos ~]# sudo yum install docker-ce docker-ce-cli containerd.io

6. 启动docker

[root@VM-4-16-centos ~]# sudo systemctl start docker

7. 查看docker版本

[root@VM-4-16-centos ~]# docker version

得到的版本信息如下:

[root@VM-4-16-centos ~]# docker version
# 客户端版本 20.10.16
Client: Docker Engine - Community
 Version:           20.10.16
 API version:       1.41
 Go version:        go1.17.10
 Git commit:        aa7e414
 Built:             Thu May 12 09:19:45 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
# 服务端版本 20.10.16
Server: Docker Engine - Community
 Engine:
  Version:          20.10.16
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.10
  Git commit:       f756502
  Built:            Thu May 12 09:18:08 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.4
  GitCommit:        212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
 runc:
  Version:          1.1.1
  GitCommit:        v1.1.1-0-g52de29d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

8. 执行hello-world检查docker是否启动成功

[root@VM-4-16-centos ~]# sudo docker run hello-world

执行信息如下:

# 提示本地没有hello-world镜像
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete # 远端拉取镜像
Digest: sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17
Status: Downloaded newer image for hello-world:latest
# 打出这句话就说明docker已经安装成功
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/

hello-world执行成功,表示Linux Centos 7.6上安装并启动成功了。接下来我们得配置镜像加速源

9. 腾讯云配置镜像加速源

配置镜像加速源分三步

9.1 编辑daemon.json文件

[root@VM-4-16-centos ~]# vim /etc/docker/daemon.json

9.2 往daemon.json中添加配置

按i进入编辑状态,将下面的json粘贴到daemon.json中,并保存,按ESC,然后按 :wq 保存

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

9.3 重启docker

[root@VM-4-16-centos ~]# sudo systemctl restart docker

所有docker的准备工作就完成了!!!

### 安装Docker的具体步骤 #### 准备工作 确保系统处于最新状态,可以使用`yum update`命令来更新现有包到最新版本。 #### 卸载旧版Docker 如果之前已经安装过较早版本的Docker,则需要先卸载这些旧版本及其依赖项: ```bash sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine ``` #### 设置仓库 为了获取最新的稳定版Docker CE,需设置官方存储库。首先安装必要的依赖包: ```bash sudo yum install -y yum-utils ``` 接着添加Docker的官方源: ```bash sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo ``` #### 安装Docker Engine 现在可以从新配置好的仓库中安装Docker引擎: ```bash sudo yum install docker-ce docker-ce-cli containerd.io ``` 这一步骤完成后,默认情况下Docker服务不会自动启动,因此还需要手动启用并启动它: ```bash sudo systemctl enable docker sudo systemctl start docker ``` 此时应该已经在CentOS 7.6成功安装好了Docker[^1]。 #### 验证安装 可以通过运行hello-world容器来验证是否正确安装Docker: ```bash sudo docker run hello-world ``` 当看到一条欢迎消息时即表示安装无误。 #### 注意事项 - 在进行任何更改之前,请务必检查防火墙或SELinux的状态,并考虑将其暂时禁用以便顺利完成安装过程[^5]。 - 对于生产环境中使用的服务器,在实施上述操作前应当充分评估风险,并遵循最佳实践指南中的建议措施。 - 如果遇到兼容性问题,可能是因为默认安装的是较低版本(如1.13),这时可以根据需求按照特定指导来进行升级[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值