一、环境准备

Docker CE支持以下版本的Ubuntu操作系统:

  • Artful 17.10 (Docker CE 17.11 Edge +)
  • Xenial 16.04 (LTS)
  • Trusty 14.04 (LTS)

Docker CE可以安装在64位的x86平台或ARM平台上。Ubuntu发行版中,LTS(Long-Term-Support)长期支持版本,会获得5年的升级维护支持,这样的版本会更稳定,因此在生产环境中推荐使用LTS版本,本次安装的环境为Ubuntu 16.04

二、安装步骤

2.1 使用APT方式安装

卸载旧版本:

sudo apt-get remove docker \
    docker-engine \
    docker.io

添加CA证书:

sudo apt-get update

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

添加GPG证书:

# 使用国内源
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# 官方源
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

添加软件源到source.list

# 国内源
sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"


# 官方源
# sudo add-apt-repository \
#   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
#   $(lsb_release -cs) \
#   stable"

更新软件包缓存并安装docker:

sudo apt-get update
sudo apt-get install docker-ce

2.2 使用脚本安装

在测试或开发环境中Docker官方为了简化安装流程,提供了一套便捷的安装脚本,Ubuntu系统上可以使用这套脚本安装:

curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun

三、优化工作

3.1 建立docker组

默认情况下,docker命令会使用Unix socket和Docker通讯,而系统中只有root用户和docker组的用户才可以访问Docker引擎的Unix socket,所以有必要把当前用户添加到docker组中:

sudo groupadd docker # 使用apt方式安装的默认已经创建了docker组
sudo usermod -aG docker $USER

3.2 镜像加速器

官方加速器:把下面的内容复制到/etc/docker/daemon.json文件即可。

{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}

阿里云加速器:开通后会有和上面类似加速地址以及配置方法,也是复制到/etc/docker/daemon.json

校验是否安装成功:

> docker info
# 包含以下信息表示设置成功
Registry Mirrors:
 https://******.mirror.aliyuncs.com/

四、启动docker

# 启动docker
sudo systemctl start docker

# 关闭docker
sudo systemctl stop docker

# 添加到开机启动
sudo systemctl enable docker

启动docker:

> docker run hello-world
# 出现以下信息表示成功

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/engine/userguide/
最后修改:2018 年 05 月 23 日
如果觉得我的文章对你有用,请随意赞赏