基本安装步骤

Ubuntu Docker 安装

Docker 支持以下的 Ubuntu 版本:

    • Ubuntu Precise 12.04 (LTS)
    • Ubuntu Trusty 14.04 (LTS)
    • Ubuntu Wily 15.10
    • 其他更新的版本……

前提条件

Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的 Ubuntu 版本是否支持 Docker。

通过 uname -r 命令查看你当前的内核版本

runoob@runoob:~$ uname -r


使用脚本安装 Docker

1、获取最新版本的 Docker 安装包

runoob@runoob:~$ wget -qO- https://get.docker.com/ | sh

输入当前用户的密码后,就会下载脚本并且安装Docker及依赖包。

安装完成后有个提示:

	If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like: sudo usermod -aG docker runoob
Remember that you will have to log out and back in for this to take effect!

当要以非root用户可以直接运行docker时,需要执行 sudo usermod -aG docker runoob 命令,然后重新登陆,否则会有如下报错

2、启动docker 后台服务

runoob@runoob:~$ sudo service docker start

3、测试运行hello-world

runoob@runoob:~$ docker run hello-world
 
 

详情测试步骤

安装Docker只需3步,下载Docker, 安装Docker,检查Docker是否成功。

Docker目前支持主流的3种操作系统的Linux, Mac, Windows的环境,本文使用的Linux系统环境为:Linux Ubuntu 14.04.4 LTS 64bit。在Ubuntu中下载和安装Docker可以直接用apt-get搞定。

由于Docker在1.7.1以后的版本指定了自己的源,所以我们需要先在APT中配置Docker的源。

更新APT的源,安装https和ca证书的库,默认这2个库都已经装了。


~ sudo apt-get update
~ sudo apt-get install apt-transport-https ca-certificates

添加秘钥GPG到APT配置中。


~ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

增加Docker的源到/etc/apt/souces.list文件中,我的版本是14.04对应ubuntu-trusty。


~ sudo vi /etc/apt/sources.list # 增加到最后一行
deb https://apt.dockerproject.org/repo ubuntu-trusty main

接下来,就可以用可以用apt-get直接安装Docker了。


~ sudo apt-get update
~ sudo apt-get install docker-engine

安装完成,默认会启动Docker。


# 检查docker服务
~ service docker status
docker start/running, process 10013 # 检查docker进行
~ ps -aux|grep docker
root 10013 0.0 1.0 424948 40584 ? Ssl 22:29 0:00 /usr/bin/dockerd --raw-logs
root 10022 0.0 0.2 199680 10280 ? Ssl 22:29 0:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shimdocker-containerd-shim --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --runtime docker-runc # 检查docker版本
~ sudo docker version
Client:
Version: 1.12.1
API version: 1.24
Go version: go1.6.3
Git commit: 23cf638
Built: Thu Aug 18 05:22:43 2016
OS/Arch: linux/amd64 Server:
Version: 1.12.1
API version: 1.24
Go version: go1.6.3
Git commit: 23cf638
Built: Thu Aug 18 05:22:43 2016
OS/Arch: linux/amd64

检查Docker是否成功,运行hello-world。如果出现下面的信息,表示Docker引擎安装成功。


~ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest 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/

注意:我们在执行上面的命令的时候,经常会遇到一个错误。Cannot connect to the Docker daemon. Is the docker daemon running on this host?

比如,直接输入 docker run hello-world 命令。


~ docker run hello-world
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

这是由于权限的问题,docker默认和root权限绑定,如果不加sudo时则没有权限。

问题解决步骤

默认安装完 docker 后,每次执行 docker 都需要运行 sudo 命令,非常浪费时间影响效率。如果不跟 sudo,直接执行 docker images 命令会有如下问题:

FATA[0000] Get http:///var/run/docker.sock/v1.18/images/json: dial unix /var/run/docker.sock: permission denied. Are you trying to connect to a TLS-enabled daemon without TLS?

于是考虑如何免 sudo 使用 docker,经过查找资料,发现只要把用户加入 docker 用户组即可,具体用法如下。

2 免 sudo 使用 docker

  • 如果还没有 docker group 就添加一个:

    1. sudo groupadd docker
  • 将用户加入该 group 内。然后退出并重新登录就生效啦。

    1. sudo gpasswd -a ${USER} docker
  • 重启 docker 服务

    1. sudo service docker restart
  • 切换当前会话到新 group 或者重启 X 会话

    1. newgrp - docker
    2. OR
    3. pkill X

注意,最后一步是必须的,否则因为 groups 命令获取到的是缓存的组信息,刚添加的组信息未能生效,所以 docker images 执行时同样有错。

3 原因分析

  • 因为 /var/run/docker.sock 所属 docker 组具有 setuid 权限

    1. $ sudo ls -l /var/run/docker.sock
    2. srw-rw---- 1 root docker 0 May 1 21:35 /var/run/docker.sock

最新文章

  1. GO语言下载、安装、配置
  2. *按类的某一字段排序(Lv)
  3. Asp.Net MVC4入门指南(4):添加一个模型
  4. asp.net core csrf
  5. Linux资源站
  6. Python学习笔记-Day3-python函数
  7. OC加强-day01
  8. C# Winform程序本地化应用
  9. dump json 显示中文问题
  10. EasyUI DataGrid 中字段 formatter 格式化不起作用
  11. Cocos2d-x 3.2 大富翁游戏项目开发-第五部分 单机游戏-级别选择ScrollView
  12. 8.17HTML 标签
  13. JS 上传文件 Uploadify 网址及 v3.2.1 参数说明
  14. CentOS下安装Tomcat 8
  15. NodeJS笔记(一)-免安装设置
  16. ACM__队列
  17. BZOJ2208: [Jsoi2010]连通数(tarjan bitset floyd)
  18. 基于CRF的中文分词
  19. UI5-文档-4.12-Shell Control as Container
  20. POJ 2370

热门文章

  1. Android系统信息获取
  2. uva 10994 - Simple Addition(规律)
  3. C# SMTP邮件发送程序
  4. 修改Nginx与Apache配置参数解决http状态码:413上传文件大小限制问题
  5. ZeroClipboard及其原理介绍
  6. iOS arc和非arc 适用 宏
  7. 外媒关注:中国版Twitter新浪微博推出微米对抗微信
  8. 正确用DD测试磁盘读写速度
  9. 排序算法及其java实现
  10. a标签添加点击事件