安装Docker

2018/8/11 posted in  docker

由于EOS的安装环境编译比较复杂,且涉及依赖比较多。最近发现其自带的Docker环境安装非常便利。要想玩起来,我们得先安装Docker。

但因为Docker在国内已被强。致使安装、镜像下载多为艰难,故整理相关资料,供大家参考。本文适用范围为,不能便利使用歪P嗯情况下的Docker安装。本文主要内容为安装Docker软件,配置镜像下载加速(我们主要使用网络上搜到的阿里云的支持),试跑hello-world

Docker主要依赖于Linux,对Windows的支持相对不好,但也可以安装。本文主要讲Linux和Mac下的Docker安装及相关的配置。

一、Linux安装

自搭虚拟机或者双系统配置好相关环境。本文主要以ubuntu14.04 LTS为例。

1.1 换阿里云源

本章节的内容参考这里

首先备份当前的源配置(必须

$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

编辑配置文件

$ sudo vi /etc/apt/sources.list

替换原文件的内容为下述内容(切记,修改内容前备份

deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

保存并退出。

1.2 使用阿里云提供的Docker源进行Docker安装

章节安装步骤参考这里,这个非常详尽,而且涵盖Cent OS等多个系统。

安装必要的一些系统工具

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

安装GPG证书(命令后面有个-),不要忽略了

$ curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

写入软件源信息

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

更新并安装 Docker-CE

$ sudo apt-get -y update
$ sudo apt-get -y install docker-ce

这样Docker就安装好了。

1.3 配置镜像下载加速源

章节内容参考这里。在阿里云申请一个账号,打开连接https://cr.console.aliyun.com/#/accelerator
拷贝您的专属加速器地址(每个人专属的,登陆需要密码),然后编辑下面文件

## 修改前先备份
$ sudo cp /etc/systemd/system/multi-user.target.wants/docker.service /etc/systemd/system/multi-user.target.wants/docker.service.bak
$ vi /etc/systemd/system/multi-user.target.wants/docker.service

可以看到下面这段内容

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
#下面这行是默认的,我注释了,添加了下面一行
#ExecStart=/usr/bin/dockerd -H fd://
ExecStart=/usr/bin/dockerd -H fd:// --registry-mirror=https://w7snjp9s.mirror.aliyuncs.com
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576

找到 ExecStart= 这一行,在这行最后添加加速器地址 –registry-mirror=<加速器地址>

如:ExecStart=/usr/bin/dockerd -H fd:// –registry-mirror=https://2h3po24q.mirror.aliyuncs.com

1.4 Hello World

因为我是虚拟机,直接重启了一下。看个人情况,重启Docker就好了。重启后运行

$ sudo docker run hello-world

理论上你应该看到下面这段内容

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
78445dd45222: Pull complete 
Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
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/

安装至此圆满结束。

二、Mac下的安装

Mac下的安装主要参考这个文章。原文没有说卸载后,命令找不到的一些坑。

过去你可以使用Docker Tools加上笨拙的虚拟机Virtualbox,是比较麻烦的。当然你可以去Docker.com去下载.dmg然后一键安装。问题在OSX上面,宿主机器和容器之间文件共享会变得非常慢。当然如果你只是小试身手,这个方式也没问题。 这里我们将着侧重在使用Homebrew + xhyve/hyperkit(native macOS hypervisor.framework)来安装设置好docker。

2.1 安装相关驱动及Docker

确保您安装好Homebrew,然后去terminal中执行

$ brew update
$ brew install docker docker-compose docker-machine xhyve docker-machine-driver-xhyve

这里解释下xhyve:

Why xhyve ?

So one of the painful points of using docker on OS X is that you need to run a virtualbox VM, which often suffers from performance issues. With xhyve, a OS X virtualization system, and docker-machine-xhyve you can now have docker use the native OS X hypervisor to run containers.

No more dealing with virtualbox shenanigans!

简单说轻量级的OSX虚拟化解决方案,让你摆脱恶心的VirtualBox设置和更新。

xhyve安装完成后,命令行提示需要增加权限

$ sudo chown root:wheel $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve
$ sudo chmod u+s $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve  

由于我们使用的是Docker,额外安装了一个xhyve对Docker的驱动,想了解的,可以查看docker-machine-driver-xhyve

2.2 获得阿里云加速地址

参考前面1.3章节,在阿里云申请一个账号,打开连接https://cr.console.aliyun.com/#/accelerator ,获得您的专属加速器地址,如:https://2h3po24q.mirror.aliyuncs.com

2.3 卸载原来默认的虚拟机(卸载前,务必确认没有资料)

卸载原来的默认的虚拟机,重配置环境(请务必确认,没有重要资料

# 卸载 Docker Toolbox:
$ sh -c "$(curl -fsSl https://raw.githubusercontent.com/docker/toolbox/master/osx/uninstall.sh)"
# 删除默认的缓存
$ sudo rm -rf ~/.docker

切记,在这里重启一下电脑,让删除生效,否则在下一步进行时,会提示Could not convert the UUID to MAC address

2.4 创建新的default虚拟机

使用xhyve创建虚拟机,使用之前获得的加速地址

$ docker-machine create default --driver xhyve --xhyve-experimental-nfs-share --engine-registry-mirror=https://2h3po24q.mirror.aliyuncs.com

创建时,因为前一步卸载了相关软件,可能提示Driver xhyve not found等。哪个软件不在,就再安装一遍就好了。直到最终成功创建。注意,如果default虚拟机创建了,那么在.docker下有残留,需要按2.3的章节清除并重启生效。

安装后的Docker的实际宿主机是在docker-machine虚拟出来的虚拟机上,要想连接,在安装成功后

$ docker-machine ssh default

查看目录

$ ls /

上面显示的将是你的虚拟机,也是你的Docker宿主机的目录。

2.5 Hello World

命令行运行

$ 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 ID:
https://cloud.docker.com/

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

安装圆满结束。

(全文完)

感谢您的支持

zan-code

处于某些特定的环境下,可以看到评论框,欢迎留言交流^_^。

友情链接: 区块链技术中文社区