简介:本篇文章介绍如何在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.-.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:
. The Docker client contacted the Docker daemon.
. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
. 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.-.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:
. The Docker client contacted the Docker daemon.
. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
. 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:
. The Docker client contacted the Docker daemon.
. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
. 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 :18.09.-.el7
docker-ce-cli.x86_64 :18.09.-.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

CentOS下安装Docker的更多相关文章

  1. docker学习之路-centos下安装docker

    前言 我要在云服务器上做一个asp.net core的webapi应用,使用docker来部署应用,中间用到的任何组件包括nginx和sqlserver 2017都是用docker来装载运行,所以,这 ...

  2. Centos下安装Docker,并配置国内docker源

    Centos下安装Docker 替换为国内的yum源(可选): mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.re ...

  3. 在RedHat/CentOS下安装Docker(不升级内核)

    由于内核版本问题,最初仅Ubuntu可以较好的支持Docker.不过,由于RedHat系列OS(REHL.CentOS)是目前主流的Linux服务器操作系统,所以令RedHat系列OS支持Docker ...

  4. Linux 64位 CentOS下安装 Docker 容器,启动、停止

    一.Docker简介 Docker 提供了一个可以运行你的应用程序的封套(envelope),或者说容器,是轻量级的“容器引擎+映像仓库”,在LXC(linux轻量级容器)的基础上构建,可以运行任何应 ...

  5. CentOS下安装Docker CE教程

    一.安装前准备 1.硬件要求 CentOS 7(64-bit),内核版本不能低于3.10: CentOS 6.5(64-bit或更新的版本),内核版本为 2.6.32-431 或者更高版本,一般不建议 ...

  6. 1. centos下安装docker

    CentOS Docker 安装 Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 前提条件 目前,CentOS 仅发 ...

  7. centos下安装docker最新版教程

    1.通过yum安装需要root或者能sudo的权限 yum包更新到最新$ sudo yum update 添加Docker yum源$ sudo tee /etc/yum.repos.d/docker ...

  8. Linux(CentOS)下安装docker

    Linux(CentOS)安装Docker 查看当前内核版本 [docker@localhost ~]$ uname -r 确保yum包更新到最新 [docker@localhost ~]$ sudo ...

  9. Centos下安装Docker集群管理工具Shipyard

    一. Docker Shipyard是什么 ? shipyard是一个开源的docker管理平台,其特性主要包括: 支持镜像管理.容器管理. 支持控制台命令 容器资源消耗监控 支持集群swarm,可以 ...

随机推荐

  1. ETL数据仓库

    http://blog.csdn.net/leicool_518/article/category/3058299 http://bbs.csdn.net/topics/390349305

  2. PHP 文件处理(综合)

    1.获取远程文件大小:php获得远程文件大小的函数php获得远程文件大小的函数文件的大小函数为:filesize()文件是否存在的函数为:file_exits();但是这两个函数只针对本地那么:远程文 ...

  3. string.match(RegExp) 与 RegExp.exec(string) 深入详解

    string.match(RegExp) 与 RegExp.exec(string) 相同点与不同点对比解析: 1. 这两个方法,如果匹配成功,返回一个数组,匹配失败,返回null. 2. 当RegE ...

  4. web应用安全权威指南(文摘)

    第1章 什么是web应用的安全隐患 第3章 Web安全基础,HTTP,会话管理,同源策略 content_length 字节数 content_type mime类型 百分号编码 referer :请 ...

  5. pom.xml的继承、聚合与依赖

    原文地址:https://my.oschina.net/zh119893/blog/232896 6.1     简介 pom.xml文件是Maven进行工作的主要配置文件.在这个文件中我们可以配置M ...

  6. 我的border能自定义四角之border-radius : 左上角,右上角,左下角,右下角。

    1 边框:border: 1px solid #0081df; 2 想要单独加上四个圆角: border-bottom-left-radius: 5px; border-top-left-radius ...

  7. Ansible 笔记 (1) - 安装和配置

    本文参考 <Ansible 自动化运维和最佳实践>,这两天刚读这本书,写写总结.主控机环境是 centos 7,被控机均是 centos 6.8 . 确保 python 版本大于 2.6 ...

  8. IO 之 InputStream 和 Reader

    package java.io; // Representing an input stream of bytes. public abstract class InputStream impleme ...

  9. java 文件中 定义一个字符串,它的默认编码是什么?

    .java 文件的编码就是 String 字符串的编码 File 文件的编码就是 文件内容的编码 request 的设置的编码就是inputstream 的编码 jvm 的默认编码(the defau ...

  10. TestNG(一)——起步

    你可以从 http://testng.org/doc/documentation-main.html 获取信息. 一.简介 TestNG是test next generation的缩写,即下一代测试技 ...