前几天看到新闻说 ubuntu 以后要回归默认 gnome,于是就在虚拟机下安装了新版的 ubuntu-gnome17.04。顺手就总结下在纯净的 ubuntu 下安装 docker 的过程。
apt 源安装 docker
自从 ubuntu 不知道哪个版本开始,就在 apt 源里自带 docker 了,不过不叫 docker,名字叫 docker.io。(注意:docker 这个名字也对应着一个包,但不是我们要装的东西~)
所以如果想要默认安装,请直接运行sudo apt-get install docker.io -y
就可以了。
配置国内加速镜像
docker 安装完成后,我们关键的还是需要配置一个国内的镜像源,不然每次拉取(pull)镜像的过程是相当漫长的。对此我们可以选择 aliyun 或者 daocloud 等国内公司。这里以 aliyun 为例子:
你需要在 aliyun 上注册一个账号,然后访问https://cr.console.aliyun.com/#/accelerator
地址,上面会有你的加速地址,以替换下述命令中的网址。
1 | # setting aliyun source |
测试 docker 命令
我们可以使用sudo docker run hello-world
来测试下 docker 的安装情况,若一切正常,会显示
这时 docker 的安装就完成了,但如果你是一个像我一样的懒虫,这些可能还不够, 你发现在使用 docker 的时候需要不断的输入 sudo,并提供密码之类的, 那是否有办法可以不输入 sudo,以当前用户来完成 docker 的操作呢?
答案是肯定的~
免 sudo 运行 docker 命令
经过一番搜索,我们发现了其实运行 docker 指令就是和 docker 的进程进行通信, 而通信过程中使用的是 socket 文件,查看该 socket 文件,我们发现该 socket 文件的权限有点特别:
它是属于root 用户和docker 组的,这个 docker 组是什么呢?
查找了下文档,我们发现docker 官方文档里就有这样一段话,并解决了我们的问题:
The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The docker daemon always runs as the root user.
If you don’t want to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
因此,参照文档中的做法,运行如下命令:
1 | sudo groupadd docker |
然后注销用户,再次登录后即可(我是直接重启~ 虚拟机丢那自己动图方便)
至此,我们就完成了 docker 在 ubuntu-gnome17.04 上的安装。