CentOS下安装Docker

简介:本篇文章介绍如何在CentOS系统下面安装docker系统。

官方文档:https://docs.docker.com/install/linux/docker-ce/centos/

Docker支持以下的CentOS版本:

  • CentOS 7 (64-bit)
  • CentOS 6.5 (64-bit) 或更高的版本

从 2017 年 3 月开始 docker 在原来的基础上分为两个分支版本: Docker CE 和 Docker EE。

Docker CE 即社区免费版,Docker EE 即企业版,强调安全,但需付费使用。

本文介绍 Docker CE 的安装使用。

一、前提条件

目前,CentOS 仅发行版本中的内核支持 Docker。

Docker 运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。

Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。

1.1 依赖性检查

Docker需要一个64位系统的Linux系统,内核的版本必须大于3.10。可以用下面的命令来检查是否满足docker的要求。

通过如下命令查看你当前的内核版本:

$ sudo uname -r
3.10.0-693.el7.x86_64

如果上述的依赖满足的话,还是推荐您全面地更新Linux系统,以保证内核相应的bug都得到修复。

通过如下命令来更新Linux系统:

$ sudo yum update

1.2 卸载旧版本

较旧版本的Dokcer被称为docker或docker-engine。如果已经安装这些,请卸载它们以及相关的依赖项。

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

 二、安装

目前CentOS系统下面安装docker可以有三种方式:

  • 使用curl获得docker的安装脚本进行安装:
    • 通过sh脚本安装
    • 下载rpm包安装
  • 使用yum包管理器来安装docker,推荐使用此方式进行安装

2.1 使用sh安装脚本安装

1.使用一个有sudo权限的账号登录CentOS系统。

2.更新现有的yum包。

$ sudo yum update

3.执行docker安装脚本。

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh

这个脚本会添加docker.repo源并安装Docker。

如果出现

+sh -c 'yum install -y -q docker-ce'

  那么执行:

yum install -y -q docker-ce

 如果出现

Delta RPMs disabled because /usr/bin/applydeltarpm not installed.

 那么执行

yum provides '*/applydeltarpm'
yum install deltarpm

4.启动docker服务。

$ sudo systemctl start docker

5.确认docker安装成功

$ sudo  docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
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/

2.2 下载rpm包安装

1.转到https://download.docker.com/linux/centos/7/x86_64/stable/Packages/并下载.rpm要安装的Docker版本的文件。

$ sudo wget -P /tmp/ https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-18.09.3-3.el7.x86_64.rpm 

-P 指定下载到/tmp/目录下

2.安装Docker CE。

$ sudo yum install /tmp/docker-ce-18.09.3-3.el7.x86_64.rpm

3.启动Docker

$ sudo systemctl start docker

4.确认docker是否安装成功

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
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/

5.升级Docker CE

  要升级Docker CE,请下载较新的软件包文件并重复以上的安装过程,但要注意的是,要使用yum -y upgrade不是使用yum -y install 并指向新文件。

2.3 使用yum包安装

1.使用有sudo权限的账号登录系统。

2.更新yum包。

$ sudo yum update

3.安装一些所需的包

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

4.添加docker源,并更新yum缓存

$ sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新yum缓存

$ sudo yum makecache fast

这个脚本会添加docker.repo源并安装Docker

5.安装Docker

$ sudo yum -y install docker-ce

6.启动docker服务

$ sudo systemctl start docker

7.确认docker是否安装成功

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
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

可以使用yum来删除docker。

1.列出docker包的具体的名字

$ sudo yum list  installed | grep docker
containerd.io.x86_64                 1.2.4-3.1.el7  docker
-ce.x86_64 3:18.09.3-3.el7 docker-ce-cli.x86_64 1:18.09.3-3.el7

2.删除docker

$ sudo yum -y remove containerd.io.x86_64 \
                     docker-ce.x86_64 \
                     docker-ce-cli.x86_64   

 

 备注:该命令只会删除docker运行环境,并不会删除镜像,容器,卷文件,以及用户创建的配置文件。

3.清楚镜像和容器文件

$ sudo rm -rf /var/lib/docker

4.手动查找并删除用户创建的配置文件。

 四、Docker安装的可选配置

4.1 以非root用户管理Docker

Docker守护程序绑定到Unix socket而不是TCP端口。默认情况下,Unix socke是root用户才有,而其他用户只能通过使用sudo使用它。Docker守护程序始终以root用户身份运行

如果您不想在docker命令前加上sudo,请创建一个名为docker的Unix组并向其添加用户。当Docker守护程序启动时,它会创建一个可由该docker成员访问的Unix socket

1.创建docker组

$ sudo groupadd docker

2.添加user到docker组

$ sudo usermod -aG docker $USER

请将$USER替换为系统中你的除root外的某个用户。

3.注销并重新登录,以便重新评估您的组成员身份。

如果在虚拟机上进行测试,则可能需要重新启动虚拟机才能使更改生效。

4.验证是否可以通过不使用sudo运行docker命令。

$ docker run hello-world

如果最初在添加用户到docker组之前,使用的是sudo的Dokcer CLI命令。你可能会出现如下错误,表明你的~/.docker目录由于sudo命令而创建的权限不正确。

WARNING: Error loading config file: /home/user/.docker/config.json -
stat /home/user/.docker/config.json: permission denied

要解决此问题,请删除~/.docker/目录(它会自动重新创建,但任何自定义设置都会丢失),或使用以下命令更改其所有权和权限:

$ sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
$ sudo chmod g+rwx "$HOME/.docker" -R

 4.2 配置在系统启动时启动Docker

$ sudo systemctl enable docker

如果要禁用此行为,请改用disable。

$ sudo systemctl disable docker

4.3 镜像加速

鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,我们可以需要配置加速器来解决。

Docker国内镜像:

  • 网易加速器:http://hub-mirror.c.163.com
  • 官方中国加速器:https://registry.docker-cn.com
  • ustc的镜像:https://docker.mirrors.ustc.edu.cn

也可使用私人镜像加速器地址,如使用阿里云的镜像加速器:登录阿里云->产品->搜索"容器镜像服务"->镜像库->镜像加速器。

新版的 Docker 使用 /etc/docker/daemon.json(Linux) 来配置 Daemon。

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

$ sudo cd /etc/docker/
$ sudo vi daemon.json
{
 "registry-mirrors": ["https://hub-mirror.c.163.com"]
} 
$
sudo systemctl daemon-reload
$
sudo systemctl restart docker

 

posted @ 2019-03-21 07:36  wztone  阅读(1995)  评论(0编辑  收藏  举报