美文网首页
如何在 Ubuntu 下配置与使用Docker,入门篇

如何在 Ubuntu 下配置与使用Docker,入门篇

作者: 鱿鱼鉄板燒 | 来源:发表于2017-09-24 20:53 被阅读0次

自己接触 Docker 不长,但是确实给我这种外行配置web工具带来方便。比如配置 Jenkins,Maven服务器,或是Gitlab CI runner。

每次过一阵要维护下docker的时候不免忘记了一些配置的步骤,总结做的不够,一阵回忆影响效率。决定记录下来写成文章帮助自己也帮助他人。

具体Docker是什么就不必多说,可以直接进入主题。

本文 Docker 的配置是在 Ubuntu server 版本下进行测试的。有些地方平台不同方法也有些不同。可以现在本地机器通过VMWare等软件安装一台虚拟机进行测试。

楼主已经准备好了一台平常使用的VMWare下的Ubuntu Server 16.04版本的虚拟机。

如何在Ubuntu下配置 Docker ?进入教程,本文直接记录是我的直接操作步骤,包会哈

安装 Docker CE

安装Docker包:

$ apt-get install apt-transport-https ca-certificates curl software-properties-common

安装GPG秘钥

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

设置Docker仓库源使用稳定版本

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

或是直接编辑 /etc/apt/source.list 追加如下:

deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable

更新 apt 索引信息

$ sudo apt-get update

安装 docker ce

$ sudo apt-get install docker-ce

测试安装情况

$ sudo docker -v
Docker version 17.06.2-ce, build cec0b72
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

目前我们看到列表没有任何清单,因为还没有跑过任何容器。
如果在执行sudo docker ps 提示:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

还需要执行如下命令:

$ systemctl start docker

启动docker服务。

小窍门

运行docker的时候都要使用 sudo 前缀有些时候有点麻烦,只要经过以下配置是可以省掉的。

解决每次执行 docker 的时候需要 sudo 的问题。

# 如果还没有docker group就添加一个:
$ sudo groupadd docker
# 将用户加入该group内。然后退出并重新登录即可生效。
$ sudo gpasswd -a ${USER} docker
# 重启docker
$ sudo service docker restart

然后可以试一试,是不是不用啦!:)

运行Docker镜像

学习任何一门编程语言都会从 helloworld 程序入门。

$ docker run hello-world

我X遇到问题了,貌似拉不了镜像卡住了。既然国外服务器用不了,那配置下国内代理吧。
百度了一波,大家照着做就好:

$ sudo vim /etc/docker/daemon.json
# 写入以下内容设置代理。
{
    "registry-mirrors": [
    "https://a73cc22x.mirror.aliyuncs.com",
    "https://hub-mirror.c.163.com",
    "http://c0d14726.m.daocloud.io",
    "https://registry.aliyuncs.com",
    "https://registry.docker-cn.com"
    ]
}

重启docker服务

$ sudo service docker restart

再次执行

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
5b0f327be733: Pull complete
Digest: sha256:1f19634d26995c320618d94e6f29c09c6589d5df3c063287a00e6de8458f8242
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 ID:
 https://cloud.docker.com/

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

可以通过docker ps 查看

$ docker ps -al
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
e4231be3f4ca        hello-world         "/hello"            10 minutes ago      Exited (0) 10 minutes ago                       modest_pasteur

Docker卸载

卸载Docker包:

$ sudo apt-get purge docker-engine

卸载Docker包及其以来不再需要使用下面的命令:

$ sudo apt-get autoremove --purge docker-engine

总结

差不多新手阶段会遇到的问题和解决方案也都罗列出来了。希望对想了解docker并想入门使用的人有所帮助。写的不对的地方也及时帮我纠正咱们一同进步。

参考资料

  1. Docker官网
  2. Oh My ZSH! 高逼格终端利器
  3. Docker 中国官方镜像加速
  4. Docker Hub 找到你想要的镜像 Docker's images

相关文章

网友评论

      本文标题:如何在 Ubuntu 下配置与使用Docker,入门篇

      本文链接:https://www.haomeiwen.com/subject/andlextx.html