ubuntu16.04安装docker

首先注意看官方的参考文档: https://docs.docker.com/engine/installation/linux/ubuntulinux/

原有的一种安装方式:html

curl -sSL https://get.docker.com/ | sudo sh
如今貌似不提供了。。

因为官方文档变化频繁,若是安装方式与本文有差别以官方文档为准linux

查看内核版本,docker对内核版本有要求,ubuntu16.04确定符合,这一步能够略过docker

$ uname -r
结果
4.4.0-47-generic

更新源,确保APT以https的方式工做,而且安装了CA证书
$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates

导入gpg密钥
$ sudo apt-key adv \
               --keyserver hkp://ha.pool.sks-keyservers.net:80 \
               --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

添加docker源,其它版本添加请参照官方文档,官方只提供LTS版本的源
找到对应版本的源地址,16.04对应到源地址为
deb https://apt.dockerproject.org/repo ubuntu-xenial main


执行命令添加源
$ echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list

更新源
$ sudo apt-get update



查看可安装列表(这步能够略过)
$ apt-cache policy docker-engine

结果
docker-engine:
  已安装:(无)
  候选: 1.12.3-0~xenial
  版本列表:
     1.12.3-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.12.2-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.12.1-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.12.0-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.11.2-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.11.1-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.11.0-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages


安装 默认安装最近的版本
$ sudo apt-get install docker-engine

启动
$ sudo service docker start


运行hello world
$ sudo docker run hello-world

结果ubuntu

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.
 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 Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

若是出现网络问题没法下载,就先从网易蜂巢的镜像市场到hello-world运行bash

docker run hub.c.163.com/library/hello-world
或者先pull后run

docker pull hub.c.163.com/library/hello-world:latest



以非管理员权限运行(每次运行要加sudo也不是事,对吧)
建立docker用户组(默认已经建立)
$ sudo groupadd docker

将当前用户加到这个组里
(不用改$USER这几个字,$USER这个环境变量就是指当前用户名)
$ sudo usermod -aG docker $USER

再尝试运行
$ docker run hello-world

若是运行出现网络

Cannot connect to the Docker daemon. Is the docker daemon running on this host?
官方文档提示你设置环境变量,其实没什么用,从网上看到不少结果都是无心中忽然好了。。。

事实上是由于加入用户组这个东西必须注销或重启才能生效,你注销或者重启一下立刻就行了。
app


附:经常使用命令curl

docker version #查看版本  
docker images #查看镜像列表  
docker images -a  #查看镜像列表  
docker rmi -f [镜像id]  #删除指定镜像