您的位置:首页 > 运维架构 > Docker

ubuntu16.04安装docker

2016-11-26 20:48 567 查看
首先注意看官方的参考文档:https://docs.docker.com/engine/installation/linux/ubuntulinux/
原有的一种安装方式:

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

由于官方文档变化频繁,如果安装方式与本文有差异以官方文档为准

查看内核版本,docker对内核版本有要求,ubuntu16.04肯定符合,这一步可以略过

$ 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

结果

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运行

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?
官方文档提示你设置环境变量,其实没什么用,从网上看到很多结果都是无意中突然好了。。。

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

附:常用命令

docker version #查看版本
docker images #查看镜像列表
docker images -a  #查看镜像列表
docker rmi -f [镜像id]  #删除指定镜像
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  docker ubuntu