简介:本篇文章介绍如何在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. SQL Server判断数据库、表、存储过程、函数是否存在

    --判断数据库是否存在 if exists (select * from sys.databases where name = '数据库名') drop database [数据库名] --判断表是否 ...

  2. jQuery的表单选择器

    1.常规选择器选择表单标签 $(function () { // var a = $("input").eq(0).val() // alert(a) // // var b = ...

  3. Bind搭建DNS服务

    DNS域名解析服务(Domain Name System)是用于解析域名与IP地址对应关系的服务,功能上可以实现正向解析与反向解析: 正向解析:根据主机名(域名)查找对应的IP地址. 反向解析:根据I ...

  4. Criterion - 一个简单可扩展的 C 语言测试框架

    A dead-simple, yet extensible, C test framework. Philosophy Most test frameworks for C require a lot ...

  5. vbs执行系统命令

    首先说明一下,我的所有代码都是vbscript,jscript我没有研究过,不过我想也差不多. 关于最基础的语法比如变量的申明,分支,循环,函数的调用,等等这些我就不讲了,不懂得自己看一下. 1.我们 ...

  6. oracle忘记密码,修改密码

    1:输入命令: sqlplus /nolog ,进入oracle控制台,并输入 conn /as sysdba;以DBA角色进入.2:若修改某一个用户密码, 修改用户口令 格式为:alter user ...

  7. android模拟按键问题总结[使用IWindowManager.injectKeyEvent方法](转)

    http://blog.csdn.net/xudongdong99/article/details/8857173 Android上面TreeView效果 http://blog.csdn.net/g ...

  8. Gym 101201J Shopping (线段树+取模)

    题意:给定 n 个物品,然后有 m 个人买东西,他们有 x 元钱,然后从 l - r 这个区间内买东西,对于每个物品都尽可能多的买,问你最少剩下多少钱. 析:对于物品,尽可能多的买的意思就是对这个物品 ...

  9. dojoConfig包的配置(7/26号夜)

    主页代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

  10. linux每天一小步---alias命令详解

    1 命令功能     alias命令用来设置指令的别名,alias命令设置的别名只限于该次登陆操作,若要每次登入即自动设好别名,可在/etc/profile或自己的~/.bashrc中设定指令的别名. ...