Docker系列(一):Linux环境下安装Docker

star2017 1年前 ⋅ 484 阅读

Docker 的构想是要实现Build,ship and Run Any App, Anywhere,即通过对应用的封装(Packaging)、分发(Distribution),部署(Deployment),运行(Runtime)生命周期进行管理,达到应用组件 一次封装,到处运行 的目的。

这里的应用组件,可以是一个Web应用,一个编译环境,也可以是一套数据库平台服务,甚至是一个操作系统或集群。

Docker目前只能运行在 64 位操作系统上,并且Linux内核不低于3.10版本。现在主流的Linux操作系统都已支持Docker。如RHEL 6.5/CentOS6.5往上的操作系统,Ubuntu 14.04往上的操作系统,都已经以软件源中默认带有Docker

Docker在各个操作系统的安装,优先参考官方文档-CN, 官方文档-EN

CentOS

准备

# 看当前的内核版本,要求不低于**3.10**。
uname -r
cat /proc/version

# 使用 yum 工具更新系统到最新。
yum update

# 卸载旧的 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

# 安装需要的软件包, yum-util 提供 yum-config-manager 功能,另外两个是 devicemapper 驱动依赖
yum install -y yum-utils device-mapper-persistent-data lvm2

# 设置 yum 源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

执行完后,会在 /etc/yum.repos.d创建一个Docker源的配置文件。

安装

# 查看所有仓库中所有 docker 版本,并选择特定版本安装
yum list docker-ce --showduplicates | sort -r

# 安装Docker,默认是安装最新的版本
yum install docker-ce
yum install docker-ce-<VERSION STRING>  # 例如:sudo yum install docker-ce-18.03.0.ce

# 启动 Docker 服务,并加入开机启动
systemctl start docker
systemctl enable docker

区镜像源

添加国内的镜像源地址,加速器镜像下载。

  1. 国内镜像源地址

  2. 配置国内镜像源地址

    打开 /etc/docker/daemon.json文件(没有时新建该文件),添加如下内容。也添设置其它加速站,如阿里、DaoCloud

    {"registry-mirrors":["https://registry.docker-cn.com"]}
    
  3. 重启 Docker 服务,使配置生效

    systemctl restart docker
    
  4. 查看当前注册的镜像源列表,验证配置是否生效

    docker info
    
    # 显示如下信息
    Experimental: false
     Insecure Registries:
      127.0.0.0/8
    Registry Mirrors:
      https://registry.docker-cn.com/
      http://hub-mirror.c.163.com/
      https://docker.mirrors.ustc.edu.cn/
    Live Restore Enabled: false
    

Ubuntu

准备

# 删除旧版本
apt-get remove docker docker-engine docker.io

# 更新系统
apt-get update

# 支持https
apt-get install apt-transport-https ca-certificates curl software-properties-common

# 添加官方GPG KEY
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 确认密钥:9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
apt-key fingerprint 0EBFCD88

# 添加稳定的 docker 仓库
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

安装

# 安装 docker-ce
apt-get install docker-ce

# 获取 docker 版本列表
apt-cache madison docker-ce

# 安装指定版本,例如:docker-ce=18.03.0~ce-0~ubuntu
apt-get install docker-ce=<VERSION>

验证

  1. 查看版本号
    切换到root账号,输入docker version, 有ClientServer两部分,说明安装和启动成功。

    docker version
    
  2. 运行最简的项目:hello-world

    docker run hello-world
    
    # 如果没有该镜像,则会自动下载。运行内容显示:This message shows that your installation appears to be working correctly. 表示安装成功。
    

卸载

  1. CentOS-卸载 docker-ce

    yum remove docker-ce
    rm -rf /var/lib/docker
    
  2. Ubuntu-卸载 docker-ce

    apt-get purge docker-ce
    rm -rf /var/lib/docker
    

相关参考

  1. Docker 镜像加速器
  2. Get Docker CE for CentOS
  3. Centos7上安装docker
  4. Docker 资源汇总
更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: