【问题标题】:Build args not passing from docker-compoes.yml to Dockerfile构建参数不从 docker-compose.yml 传递到 Dockerfile
【发布时间】:2021-09-17 05:51:02
【问题描述】:

我的 docker-compose.yml:

version: "3.8"

services:
  web:
    build:
      context: .
      args:
        file_url: 'some-url'

我的 Dockerfile:

FROM ruby:2.7.4-bullseye

ARG file_url

RUN curl $file_url -L -o "file"

当我跑步时:

docker-compose up --build

我希望 Docker 会构建并启动我的容器。但相反,我得到了:

Step 4/12 : RUN curl $file_url -L -o "file"
 ---> Running in 59696c6274c7
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information

很明显,构建参数没有传递给 Dockerfile。我在 Stackoverflow 上阅读了很多类似的帖子,但不知道出了什么问题。

还有一件事。我实际上可以用docker-compose build web 构建容器就好了。只是在我运行docker-compose up --builddocker-compose up 时发生了错误。

【问题讨论】:

    标签: docker docker-compose


    【解决方案1】:

    变量未在您的 dockerfile 中正确传递。 在 dockerfile 中查看变量调用:https://docs.docker.com/engine/reference/builder/

    理想情况下,它包含在 ${variable_name} 中。 这行RUN curl $file_url -L -o "file需要整改。

    在您的 Dockerfile 中将 $file_url 更改为 ${file_url}

    【讨论】:

    • 我不认为这是原因。 Docker 文档说:Environment variables are notated in the Dockerfile either with $variable_name or ${variable_name}. They are treated equivalently and the brace syntax is typically used to address issues with variable names with no whitespace, like ${foo}_bar. 是的,我试过了。
    • 我想通了。问题不在于在 Dockerfile 中传递参数。即使在构建时输出为Step 4/12 : RUN curl $file_url -L -o "file",参数也会正确传递。我测试了它。问题在于传递 file_url 参数时的 curl 语法。例如,您可以通过传递 https://stackoverflow.com/ 作为 file_url 参数来测试它,并且不会出现错误。你想传递什么作为'some-url'? @丁宇
    • 感谢您的跟进。 file_url 只是一个占位符。我使用的真实 URL 是 https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb 但错误是一样的。此外,我实际上可以用docker-compose build web 构建容器就好了。就在我运行docker-compose up --builddocker-compose up 时发生了错误。
    • 我正在尝试重现您的错误,但我不能!也许有一些东西被缓存了。您可以尝试docker-compose build --no-cache && docker-compose up --force-recreate 以确保没有缓存。 (也尝试从“some-url”中删除``,因为我在使用它们时出错)
    猜你喜欢
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 2020-02-08
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    相关资源
    最近更新 更多