我有这个脚本:
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,而不是mypersonaltag。我已经尝试了像下面这样的所有其他可能的方法,看看标签参数。这就是抛出错误。
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'>发布于 2019-12-18 20:10:47
image, build_log = client.images.build(path=dockerfile_path, dockerfile='Dockerfile', tag=localtag, rm=True)将变量传递给函数存在问题,请删除localtag两边的引号
如果你以这种方式传递tag={localtag}变量,那么你就是在告诉python:“嘿,取一个变量,把它放在set里,然后把这个集合传递给function”。您可能会尝试使用f"{localtag}",它使用fstring结构(需要python3.6)。
--edit--
请告诉我们,您如何调用您的脚本?那么错误是什么呢?
https://stackoverflow.com/questions/59390522
复制相似问题