Docker环境搭建(CentOS篇)
  分类: Linux   评论: 2 条

Docker环境搭建(CentOS篇)

in Linux with 2 comment

Docker 将程序与程序的运行环境打包在一起,从而避免了复杂的环境配置,只要机器安装了 Docker,即可无差别运行 Docker 镜像。由于生产环境多用 CentOS,今天就来说下 CentOS 下的 Docker 环境搭建。

卸载旧版本

使用下面命令查看当前 Docker 版本:

docker -v

截止到本文发布时,最新版本的 Docker 是 19.03.6,如果版本过低,可以使用下面的命令卸载:

sudo yum remove docker \
                docker-client \
                docker-client-latest \
                docker-common \
                docker-latest \
                docker-latest-logrotate \
                docker-logrotate \
                docker-engine

以下安装均针对社区版本,即 Community Edition,简称 CE。

使用镜像仓库安装

设置镜像仓库

1、安装软件包

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

2、设置稳定版仓库

生产环境,一定要使用稳定版。

sudo yum-config-manager \
  --add-repo \
  https://download.docker.com/linux/centos/docker-ce.repo

安装 Docker

1、安装最新版本

生产环境,不推荐直接安装最新版,防止出现兼容性问题。

sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

2、安装指定版本

生产环境一般都是使用固定版本号,并且在此版本上经过了充分的测试,推荐此方法安装。

列出所有版本

版本号由高到低排序。

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

显示结果如下:中间一列内容冒号和连字符之间的是版本号,例如第一行中,19.03.6 就是版本号。

docker-ce.x86_64            3:19.03.6-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.6-3.el7                    @docker-ce-stable
docker-ce.x86_64            3:19.03.5-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.2-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.1-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.0-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.9-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.8-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.7-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.6-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.5-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.4-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.3-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.2-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.1-3.el7                    docker-ce-stable
docker-ce.x86_64            3:18.09.0-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.3.ce-3.el7                   docker-ce-stable
docker-ce.x86_64            18.06.2.ce-3.el7                   docker-ce-stable
docker-ce.x86_64            18.06.1.ce-3.el7                   docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                   docker-ce-stable
安装指定版本

安装指定版本的命令:

yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-compose-plugin

要将上述命令中的 <VERSION_STRING> 替换为要安装的版本号,还是以 19.03.6 为例,完整的安装命令如下:

yum install docker-ce-19.03.6 docker-ce-cli-19.03.6 containerd.io docker-compose-plugin

使用软件包安装

不推荐这种安装方式,比较麻烦,除非是没办法使用仓库安装。

1、下载软件包

打开 https://download.docker.com/linux/centos/7/x86_64/stable/Packages/ ,下载要安装的版本。

还是以 19.03.6 为例,安装 docker-cedocker-ce-clicontainerd.io ,分别下载以下三个文件:

docker-cedocker-ce-cli 的版本是对应的,containerd 和其他两个版本号不对应。

2、安装软件包

进入到下载文件的目录,然后按顺序执行下面的命令即可:

sudo yum install containerd.io-1.2.10-3.2.el7.x86_64.rpm
sudo yum install docker-ce-cli-19.03.6-3.el7.x86_64.rpm
sudo yum install docker-ce-19.03.6-3.el7.x86_64.rpm

先安装 docker-ce 会提示 Requires: containerd.io >= 1.2.2-3Requires: docker-ce-cli,所以,先安装需要的模块即可。

启动 Docker

通过以上的步骤,Docker 就安装好了,但是还没有启动,通过以下命令启动 Docker:

systemctl start docker

如果要将 Docker 设置为开启自启动,执行下面命令即可:

systemctl enable docker

测试

执行下面命令测试 Docker 是否正确安装:

docker run hello-world

如果显示以下信息,说明安装成功。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
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

卸载软件包

sudo yum remove dcoker-ce \
           docker-ce-cli \
           containerd.io

删除 Docker 相关文件

会删除 Docker 的镜像,容器等。

sudo rm -rf /var/lib/docker

以上便是在 CentOS 下搭建 Docker 的全过程,其他环境搭建请参考:

更多说明请参考:「Docker 官方文档」。

欢迎关注我的公众号,及时获取最新文章推送。