python-3.x - 使用 python sdk 在 github 中使用远程 docker 文件构建 docker 映像
问题描述
我能够使用 python sdk 构建 docker 映像。如果 dockerfile 在我的本地机器上可用。
client = docker.from_env()
image, build_log = client.images.build(path = "./", tag=image_name,rm=True)
现在,我的 docker 文件将保存在 github 存储库中,我应该提取它们并构建图像。python sdk doc 说 build 方法接受路径或文件对象。
我能够使用 pyGithub (API3) 存储库从 github 读取 docker 文件的内容
g = Github(base_url=url, login_or_token=accessToken, verify=False)
dmc = g.get_organization(org_name)
repo = dmc.get_repo(repoName)
contents = repo.get_contents(dockerfile_name, "master")
我不确定如何将上述内容对象(ContentFile.ContentFile)转换为 python 文件对象,以便我可以使用它来构建如下图像
client = docker.from_env()
image, build_log = client.images.build(fileobj = contents_file_obj, tag=image_name,rm=True)
解决方案
下面的工作代码可用于从以下代码中提取 Python 文件 obj ContentFile.ContentFile
:
import io
from github import Github
import docker
# docker client
client = docker.from_env()
# github repo
g = Github(base_url=url, login_or_token=accessToken, verify=False)
dmc = g.get_organization(org_name)
repo = dmc.get_repo(repoName)
contents = repo.get_contents(dockerfile_name, "master")
# decoding contents of file and creating file obj out of it
decoded_content_str = contents.decoded_content.decode('utf-8')
contents_file_obj = io.StringIO(decoded_content_str)
# building image out of contents file obj
client = docker.from_env()
image, build_log = client.images.build(
fileobj=contents_file_obj,
tag=image_name,
rm=True)
在上面的代码中,(obj of class )的
方法返回文件的字节串内容。解码为字符串。
then用于创建 IO 对象(与 python 中的文件对象相同)decoded_content
contents
ContentFile.ContentFile
.decode('utf-8')
decoded_content_str
io.StringIO
推荐阅读
- c# - 如何在C#中删除链表中的结束值
- node.js - 捕获响应 awaitMessages 操作的用户
- python - 代码在函数外部工作正常,但一旦进入内部,它就不起作用
- javascript - PIXI.js 粒子聚集在角落里。想不通
- javascript - 如何获取两种不同格式的日期之间的时差?
- python - pandas - 有没有办法根据条件将值从一个数据框列复制到另一个?
- c# - 无法反序列化当前 JSON 对象(例如 {"name":"value"}
- ios - SwiftUI 中的多个 JSON 文件调用
- uicollectionview - 如何在与导航栏标题的前沿相同的集合视图单元格中配置间距?
- javascript - 如何在 Node.js 中同步处理用户输入