Ubuntu16.04LTS安装docker

查看系统版本信息
1
cat /etc/issue

Ubuntu 16.04.2 LTS \n \l

在ubuntu上简单的安装方法
1
2
sudo apt-get update
sudo apt-get install docker

搞定!(这种安装方法的缺点是安装的docker版本不是最新的版本)
下面介绍推荐的安装docker方式

注意(Docker需要64的操作系统,此外你的kernel内核至少要在3.10版本之上)
官方的参考文档: https://docs.docker.com/engine/installation/linux/ubuntulinux/

1
2
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates
导入gpg密钥
1
2
3
sudo apt-key adv \
—keyserver hkp://ha.pool.sks-keyservers.net:80 \
—recv-keys 58118E89F3A912897C070ADBF76221572C52609D

添加docker源,其它版本添加请参照官方文档,官方只提供LTS版本的源
找到对应版本的源地址,16.04对应到源地址为:

1
deb https://apt.dockerproject.org/repo ubuntu-xenial main 
执行命令添加源
1
echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list
更新源
1
sudo apt-get update
查看可安装列表(这步可以略过)
1
apt-cache policy docker-engine
安装 默认安装最近的版本
1
sudo apt-get install docker-engine
启动
1
sudo service docker start
运行hello world
1
sudo docker run hello-world
结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.
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 Hub account:
https://hub.docker.com
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
如果出现网络问题无法下载,就先从网易蜂巢的镜像市场到hello-world运行

docker run hub.c.163.com/library/hello-world

或者先pull后run
1
docker pull hub.c.163.com/library/hello-world:latest
以非管理员权限运行(每次运行要加sudo也不是事,对吧)
创建docker用户组(默认已经创建)
1
sudo groupadd docker
将当前用户加到这个组里

(不用改$USER这几个字,$USER这个环境变量就是指当前用户名)

1
sudo usermod -aG docker $USER
再尝试运行
1
docker run hello-world
如果运行出现

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

官方文档提示你设置环境变量,其实没什么用,从网上看到很多结果都是无意中突然好了~

事实上是因为加入用户组这个东西必须注销或重启才能生效,你注销或者重启一下马上就好了

常用命令
1
2
3
4
docker version #查看版本
docker images #查看镜像列表
docker images -a #查看镜像列表
docker rmi -f [镜像id] #删除指定镜像