CentOS7安装docker服务(指定版本)

100 阅读2分钟
  1. 查看当前CentOS版本
[root@hadoop101 ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
  1. 卸载旧版本docker命令
yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine
  1. 更新最新系统
yum update -y
  1. 安装所需的软件包以允许yum使用HTTPS存储库
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
或
sudo yum -y install yum-utils
  1. 添加Docker的官方GPG密钥
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 或使用aliyun速度更快

sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  1. 安装Docker Engine-Community最新版本
sudo yum install -y docker-ce docker-ce-cli containerd.io
# 或
sudo yum -y install docker-ce docker-ce-cli containerd.io

# 如果要连同docker compose插件一起安装
yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 这里是安装的docker compose插件,使用的命令是“docker compose up -d”或者“docker compose down”

# compose也有第二种安装方式,如下
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 上面的变量替换完后名称是 “docker-compose-Linux-x86_64”
sudo chmod +x /usr/local/bin/docker-compose

docker-compose --version

# 注:上面的compose使用方式有些许区别,分别是“docker compose”和“docker-compose”
  1. 安装指定docker版本
# 列出可用版本
yum list docker-ce --showduplicates | sort -r

# 安装指定版本(使用以下命令安装所需的 Docker 版本(将 <VERSION_STRING> 替换为所需版本))
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
  1. 启动Docker服务 查看状态 开机启动服务
# 启动Docker服务
sudo systemctl start docker

# 设置Docker服务开机自启动
sudo systemctl enable docker

# 查看docker服务状态
sudo systemctl status docker
  1. 查看docker 版本信息
docker -v
  1. 修改docker镜像源
# 创建目录(如果已经存在则忽略此步骤)
sudo mkdir -p /etc/docker

# 向配置文件写入
sudo tee /etc/docker/daemon.json <<- 'EOF'
{
    "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://docker.imgdb.de",
    "https://docker-0.unsee.tech",
    "https://docker.hlmirror.com",
    "https://docker.1ms.run",
    "https://func.ink",
    "https://lispy.org",
    "https://docker.xiaogenban1993.com"
    ]
}
EOF

# 重启docker服务
sudo systemctl daemon-reload && sudo systemctl restart docker
  1. 验证docker
docker run hello-world

运行结果

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