错误请求 ("error reading build args: json: cannot unmarshal number into Go value of type string")

Bad Request ("error reading build args: json: cannot unmarshal number into Go value of type string")

使用 docker python 库,


    UID = USER_ID = os.getuid()
    GROUP_ID = os.getgid()
    USER = getpass.getuser()
    HOME = "~"
    DIR = os.getcwd()
    DOCKER_GID = execute_shell_command('"$(stat -c "%g" /var/run/docker.sock)"', log_path)
    VERSION = "latest"
    BASE_DOCKER = "ubuntu:20.04"

    assert os.path.isfile(dockerfile_path)

    build_args = {
        "UID": UID,
        "DOCKER_GID": DOCKER_GID,
        "UNAME": USER,
        "VER": VERSION,
        "BASE_IMAGE": BASE_DOCKER,
    }  

client = client = docker.from_env()
client.images.build(fileobj=dockerfile, buildargs=build_args)

得到

Bad Request ("error reading build args: json: cannot unmarshal number into Go value of type string")

这不是 Google 可用的,因此为未来的读者发帖

已找到答案here

build_args 中的数字转换为字符串以避免此 undocumented 错误:

 build_args = {k: str(v) for k, v in {
        "UID": UID,
        "DOCKER_GID": DOCKER_GID,
        "UNAME": USER,
        "VER": VERSION,
        "BASE_IMAGE": BASE_DOCKER,
    }.items()}