Docker深入理解:用Python实现一个简单的Docker Mocker 1.起步

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ucsheep/article/details/86363208

在Github上面发现了一个很有意思的项目,100%Python实现的Docker模型,这个小项目叫Mocker。
以下是原作者相关链接:
Mocker Github 地址
作者讲解视频 YouTube 地址

为了后续深入地理解,我先按照作者的说明,跑通这个程序。当然,这仅仅是一个简易的Docker模型。在操作的过程中,会遇到一些问题。在这之前,需要说明一下实验的环境。以下是Mocker作者测试成功的环境。

1. CentOS7 和 Ubuntu 14 及以上(不支持Windows)
2. Python 2.7

针对上述情况,我使用的环境为 Ubuntu 16 和 Python 2.7。

1.获取源码

从github获取到源码,并提取到本地。如下:

ucsheep@ucsheep-B250M-D2V:~$ mv 下载/mocker-master /home/ucsheep/mocker
ucsheep@ucsheep-B250M-D2V:~$ cd /home/ucsheep/mocker/
ucsheep@ucsheep-B250M-D2V:~/mocker$ ls
LICENSE  mocker  mocker.py  README.md  requirements.txt  scripts  setup.py
ucsheep@ucsheep-B250M-D2V:~/mocker$ 

如图,我们已经将源码放置在 /home/ucsheep/mocker 目录下。

2.建立一个隔离环境

我们要建立一个隔离的Python虚拟运行环境,需要首先安装virtualenv

sudo apt-get install virtualenv

或者

pip install virtualenv

都可以。
接着:

ucsheep@ucsheep-B250M-D2V:~/mocker$ virtualenv .
New python executable in /home/ucsheep/mocker/bin/python
Installing setuptools, pip, wheel...
done.
ucsheep@ucsheep-B250M-D2V:~/mocker$ source bin/activate
(mocker) ucsheep@ucsheep-B250M-D2V:~/mocker$

如上,成功。

3.安装Mocker所有依赖

(mocker) ucsheep@ucsheep-B250M-D2V:~/mocker$ pip install -r requirements.txt 

到这一步,整个安装过程就成功了。我们接下来就是测试一下基本的pull/images/run这三个命令了

4.测试 pull/images/run 命令

首先看一下当前的镜像列表

(mocker) ucsheep@ucsheep-B250M-D2V:~/mocker$ python mocker.py images
+------+---------+------+------+
| name | version | size | file |
+------+---------+------+------+
(mocker) ucsheep@ucsheep-B250M-D2V:~/mocker$

是空的,我们pull一个nginx 1.11镜像

(mocker) ucsheep@ucsheep-B250M-D2V:~/mocker$ python mocker.py pull nginx 1.11
Fetching manifest for nginx:1.11...
Fetching layer sha256:451b9524cb063772e4429c5cdf98081e4d1fd04d0073ab83a628e4db06bd1737..
- var
- var/log
- var/log/nginx
- var/log/nginx/access.log
- var/log/nginx/error.log
...
...
...

此时,Mocker是从DockerHub拉取镜像,接着,看看我们此时的images列表

(mocker) ucsheep@ucsheep-B250M-D2V:~/mocker$ python mocker.py images
+---------------+---------+---------+--------------------+
| name          | version | size    | file               |
+---------------+---------+---------+--------------------+
| library/nginx | 1.11    | 68.4MiB | library_nginx.json |
+---------------+---------+---------+--------------------+

已经多了一个nginx镜像,在看一下我们本地文件有什么变化

(mocker) ucsheep@ucsheep-B250M-D2V:~/mocker$ ls
bin      library_nginx       local      README.md         setup.py
include  library_nginx.json  mocker     requirements.txt
lib      LICENSE             mocker.py  scripts

多了 library_nginxlibrary_nginx.json
镜像的一些基础信息存储在 library_nginx.json,而镜像的layer抖存在 library_nginx
有的时候会报 *layer不存在的错误,原因在于,Mocker查找本地images的方式是遍历当前目录下的所有json文件,默认认为 一个.json 文件就代表一个镜像,然后根据此json文件名去找对应的layer目录。将无关的json文件删掉,会停止报错。

接下来,我们run一下

(mocker) ucsheep@ucsheep-B250M-D2V:~/mocker$ python mocker.py run library/nginx
Creating cgroups sub-directories for user root
Hierarchies availables: ['hugetlb', 'perf_event', 'blkio', 'devices', 'memory', 'cpuacct', 'cpu', 'cpuset', 'freezer', 'systemd']
cgroups sub-directories created for user root
Creating cgroups sub-directories for user root
Hierarchies availables: ['hugetlb', 'perf_event', 'blkio', 'devices', 'memory', 'cpuacct', 'cpu', 'cpuset', 'freezer', 'systemd']
cgroups sub-directories created for user root
Running "/hello"
Creating cgroups sub-directories for user root
Hierarchies availables: ['hugetlb', 'perf_event', 'blkio', 'devices', 'memory', 'cpuacct', 'cpu', 'cpuset', 'freezer', 'systemd']
cgroups sub-directories created for user root
Setting ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

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/

None
None
Finalizing
done

猜你喜欢

转载自blog.csdn.net/ucsheep/article/details/86363208