Linux环境中安装docker

1. 卸载旧版本

如果你此前已经安装过docker engine,那么这步是必须的,请使用以下的命令删除历史安装:

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

如果yum包管理器回复未安装,那么这不会影响到之后的安装。
这里需要注意,卸载docker时,存储在 /var/lib/docker/目录下的镜像,容器,卷和网络并不会自动删除。

2. 安装方式选择

2.1. 设置存储库

安装需要的软件包并设置 Docker 的存储库,使用以下命令。

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

2.2. 安装Docker engine

安装 Docker engine、containerd和docker compose
要安装最新的版本,运行以下命令:

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

这个命令安装了Docker,但是并不会启动Docker。同时它还会创建Docker组,默认情况下并不会向该组添加任何用户。
如果你需要安装特定版本,这在很多时候都是被要求执行的。
首先需要列出存储库中的可用版本,使用以下命令:

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

显示:
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.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable

返回的列表中显示了软件包的版本,安装特定版本时需要指定包名称加上版本字符串。将 <VERSION_STRING>替换为所需的版本,执行以下命令:

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

2.3. 启动docker

sudo systemctl start docker
我们可用通过运行镜像来验证安装是否成功:

sudo docker run hello-world
此命令将下载测试镜像并在容器中运行,当容器运行,它会打印确认信息并退出。

显示如下:

[root@localhost ~]# docker run hello-world

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/

猜你喜欢

转载自blog.csdn.net/eettttttt/article/details/131820644