linux CentOS7 64位系统 安装docker-ce

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhen_6137/article/details/85046896

1. docker 概述

目前,docker有2个版本:

1. Community Edition (CE) -- docker-ce  docker的社区版本非常适合希望开始使用Docker并尝试基于容器的应用程序的个人开发人员和小型团队。

2. Enterprise Edition   (EE)-- docker-ee  docker的企业版本是为企业开发和IT团队而设计的,这些团队构建、发布和运行大规模生产中的业务关键应用程序。

社区版本 和 企业版本在功能上的差异

作为学习docker,我们安装docker的社区版就行了。

docker-ce支持多个平台,可以在windows、Mac、Linux上运行。

 2. prerequisites(先决条件)

引用docker官网的一段话

To install Docker CE, you need a maintained version of CentOS 7. Archived versions aren’t supported or tested.

要安装Docker CE,您需要一个CentOS 7的发行版本。不支持或测试存档版本。

The centos-extras repository must be enabled. This repository is enabled by default, but if you have disabled it, you need to re-enable it.

必须启用centos-extras存储库。这个存储库在默认情况下是启用的,但是如果您禁用了它,则需要重新启用它。

The overlay2 storage driver is recommended.

建议使用overlay2存储驱动程序。

再次,Docker要求CentOS系统的内核版本不低于3.10,且为64位,所有首先需要验证CentOS版本是否支持Docker,

由于CentOS7 满足最低内核的要求,但由于内核版本比较低,部分功能(如overlay2存储层驱动)无法使用,并且部分功能可能不太稳定。

通过命令 uname -r (不懂 uname命令的,可以使用 man uname  或者 uname --help查看具体如何使用) 查看你当前linux系统内核版本。

还有其他命令:

  cat /etc/centos-release         #查看CentOS系统的版本

[root@zzf docker]# uname -r
3.10.0-957.1.3.el7.x86_64
[root@zzf docker]# 

3. docker-ce的安装

首先,卸载旧版本的docker, 我们叫docker或者docker-engine,如果他们已安装,那么要卸载他们及其有关的依赖

[root@zzf docker]# yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-selinux \
                      docker-engine-selinux \
                      docker-engine

卸载后,这个目录/var/lib/docker,包含以前下载的镜像,启动的容器,数据卷和网络,依然被保留。

安装Docker CE有2种方式,根据你的需求;

  • Most users set up Docker’s repositories and install from them, for ease of installation and upgrade tasks. This is the recommended approach.

  • Some users download the RPM package and install it manually and manage upgrades completely manually. This is useful in situations such as installing Docker on air-gapped systems with no access to the internet.

  • In testing and development environments, some users choose to use automated convenience scripts to install Docker.

 采用设置Docker repositories

备注:我使用root用户安装,如果你是其他用户,在命令前加上 sudo

1. 安装依赖包, yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data and lvm2are required by the devicemapper storage driver.

[root@zzf /]#  yum install -y yum-utils \
                  device-mapper-persistent-data \
                  lvm2

 2. 设置稳定的repository

由于国内网络问题,如果按照官网设置的库,根本就不会成功,所以建议使用国内源

#官方的源
$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

 国内库,

[root@zzf /]# sudo yum-config-manager \
                --add-repo \
                https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

以上命令会添加稳定版本的 Docker CE yum 源。从 Docker 17.06 开始,edge test 版本的 yum 源也会包含稳定版本的 Docker CE。

(本人没有使用)可选:启用边缘和测试存储库。这些存储库包含在docker.repo上面的文件中,但默认情况下是禁用的。可以将它们与稳定的存储库一起启用

# yum-config-manager --enable docker-ce-edge
# yum-config-manager --enable docker-ce-test

 禁用:

# yum-config-manager --disable docker-ce-edge
# yum-config-manager --disable docker-ce-test

 3.安装docker-ce

 安装最新版本的docker-ce,使用如下命令

[root@zzf /]# yum install docker-ce

安装指定版本的Docker CE,通过命令列出在库中可用的docker版本,通过如下命令,

列表是按版本号排序,从高版本到低版本

[root@zzf /]# yum list docker-ce --showduplicates | sort -r

已加载插件:fastestmirror
已安装的软件包
可安装的软件包
 * updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
 * extras: mirrors.aliyun.com
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 
docker-ce.x86_64            18.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            18.03.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.1.ce-1.el7.centos            @docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.3.ce-1.el7                   docker-ce-stable 
docker-ce.x86_64            17.03.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable 
 * base: mirrors.aliyun.com

The list returned depends on which repositories are enabled, and is specific to your version of CentOS (indicated by the .el7suffix in this example).

b. Install a specific version by its fully qualified package name, which is the package name (docker-ce) plus the version string (2nd column) up to the first hyphen, separated by a hyphen (-), for example, docker-ce-18.03.0.ce

# yum install docker-ce-<VERSION STRING>
[root@zzf /]# yum install docker-ce-17.12.1.ce

Docker is installed but not started. The docker group is created, but no users are added to the group.

docker被安装但是没有启动,docker组被创建,但是没有用户添加到该组。

4. 启动docker

[root@zzf /]# systemctl start docker

5. 验证docker被正确安装,运行hello-world镜像 或者查看docker 版本

# 执行该命令会下载一个测试镜像,并且在一个容器中运行该镜像,当该容器运行时,会打印一些信息,然后退出
[root@zzf /]# docker run hello-world

或者

# 打印docker 的版本信息
[root@zzf /]# docker -v

 运行docker run hello-world命令,显示如下 说明docker安装成功。

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

6. 将docker服务加入启动项,随系统启动而启动

[root@zzf /]# chkconfig docker on
注意:正在将请求转发到“systemctl enable docker.service”。
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

 7. 卸载Docker CE

 卸载docker相关包

# yum remove docker-ce

该命令不会自动删除主机上的图像,容器,卷或自定义配置文件。删除所有图像,容器和卷:

还必须手动删除任何已编辑的配置文件

# rm -rf /var/lib/docker

4. docker常用命令

1. 启动docker

# systemctl start docker.service

2. 查看docker启动状态

# systemctl status docker.service

3. 配置开机启动

# systemctl enable docker

4. 禁止开机启动

# systemtctl disable docker

3. docker pull

4. docker search

5. docker run

6. docker images

5. 镜像加速

鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,我们可以需要配置加速器来解决,我使用的是网易的镜像地址:http://hub-mirror.c.163.com。

新版的 Docker 使用 /etc/docker/daemon.json(Linux) 或者 %programdata%\docker\config\daemon.json(Windows) 来配置 Daemon。

请在该配置文件中加入(没有该文件的话,请先建一个):

{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}

参考:docker 官方网站:https://docs.docker.com/install/linux/docker-ce/centos/

其他博文:https://www.cnblogs.com/yu-hailong/p/7629120.html

                 https://blog.csdn.net/qq_35590198/article/details/79318160

猜你喜欢

转载自blog.csdn.net/zhen_6137/article/details/85046896