【发布时间】:2019-12-18 10:56:33
【问题描述】:
我有这个脚本:
import docker
import os
localtag = "mypersonaltag"
client = docker.from_env()
dockerfile_path = os.path.dirname(os.getcwd())
print(dockerfile_path)
fname = dockerfile_path + "/Dockerfile"
path = os.path.dirname(fname)
image, build_log = client.images.build(path=dockerfile_path, dockerfile='Dockerfile', tag='localtag', rm=True)
print(image)
如果我执行这个脚本,它会创建并构建本地 docker 镜像,标签名称为 localtag,但不是 我的个人标签。我已经尝试了所有其他可能的方式,如下所示,看看标签参数。它抛出错误。
image, build_log = client.images.build(path=dockerfile_path, dockerfile='Dockerfile', tag=localtag, rm=True)
或
image, build_log = client.images.build(path=dockerfile_path, dockerfile='Dockerfile', tag={localtag}, rm=True)
但奇怪的是,这个[image, build_log = client.images.build(path=dockerfile_path, dockerfile='Dockerfile', tag=localtag, rm=True)] 命令可以通过 python 编辑器完美运行,而使用 python 脚本会出错。不知道为什么?但是,只要我在标签周围加上引号,正在破坏的 python 脚本现在就可以正常工作了。
>>> tag = "test02"
>>> image, build_log = client.images.build(path=path, dockerfile='Dockerfile', tag=tag, rm=True)
>>> print(image)
<Image: 'local_tag:latest', 'localtag:latest', 'test01:latest', 'test02:latest'>
【问题讨论】:
标签: python docker docker-api