Python Docker
项目描述
python-docker
一个纯Python实现,可以在没有docker的情况下构建docker镜像,并提供一个Python API来与docker仓库交互。
库使用示例
无需docker下载docker镜像!
from python_docker.registry import Registry
registry = Registry()
image = registry.pull_image('frolvlad/alpine-glibc', 'latest')
从文件系统修改docker镜像
from python_docker.base import Image
from python_docker.registry import Registry
registry = Registry()
image = registry.pull_image('continuumio/miniconda3', 'latest')
image.remove_layer()
image.name = 'this-is-a-test'
image.add_layer_path('./')
image.add_layer_contents({
'/this/is/a/test1': b'this is test 1',
'/this/is/a/test2': b'this is test 2'
})
image.layers[0].config['Env'].append('FOO=BAR')
# write docker image to filesystem
image.write_filename('example-docker-image.tar')
# run docker image (does require docker)
image.run(['cat /this/is/a/test1'])
上面的示例展示了如何在从仓库中拉取docker镜像后更新docker镜像。另外,在pull_image
方法中有一个lazy
选项。这允许您在不实际下载所有层的情况下修改docker镜像。当需要向一个几GB大的GPU镜像添加一个小层时,这是一个非常重要的功能。
from python_docker.base import Image
from python_docker.registry import Registry
registry = Registry()
image = registry.pull_image('continuumio/miniconda3', 'latest', lazy=True)
# do the same actions as the example above
# difference is that the layers are lazily downloaded
# only when needed in the `image.write_filename`
# and `image.run`.
registry.push_image(image)
# push_image does not require downloading the layers
开发
依赖关系
安装开发环境
conda env create -f environment-dev.yaml
测试
docker-compose up -d
pytest
这是如何工作的?
事实证明,docker镜像只是文件的tar集合。有几个规范版本。对于v1.0
,规范定义在这里:规范定义。我们不是写下规范,而是看看单个docker镜像。
docker pull ubuntu:latest
docker save ubuntu:latest -o /tmp/ubuntu.tar
列出docker镜像的目录结构。注意它是一个layer.tar
的集合,这是一个文件系统的tar归档。还有几个json文件。VERSION
文件目前总是1.0
。
tar -tvf /tmp/ubuntu.tar
Dockerhub意外地以v1
- v1.2
兼容的格式导出docker镜像。我们只看看对v1
重要的文件。仓库告诉层作为当前名称/标签的层头使用。
tar -xf /tmp/ubuntu.tar $filename
cat $filename | python -m json.tool
对于每一层,有三个文件:VERSION
、layer.tar
和 json
。
tar -xf /tmp/ubuntu.tar $filename
cat $filename
tar -xf /tmp/ubuntu.tar $filename
cat $filename | python -m json.tool
查看层元数据。
{
"id": "93935bf1450219e4351893e546b97b4584083b01d19daeba56cab906fc75fc1c",
"created": "1969-12-31T19:00:00-05:00",
"container_config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": null,
"Image": "",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"os": "linux"
}
查看层文件系统。
tar -xf /tmp/ubuntu.tar $filename
tar -tvf $filename | head
参考文献
- Docker 仓库 API 规范
- Docker 镜像规范
项目详情
关闭
python-docker-0.2.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 72ac524c57b752a64ef42ebd43949859ffe3261179edc97b642e44e6558fb593 |
|
MD5 | e4521b97c71cb19488b7ed4b06394f14 |
|
BLAKE2b-256 | cd7e2d80b89886fcc27338f07deeabd8b414e85e97a584180b5925e2fb17fdf1 |