简体   繁体   English

docker-compose.yml 传递 arg 以从文件内容构建

[英]docker-compose.yml passing arg to build from file contents

I would like to read contents of a file specified by an environment variable and pass it to docker-compose as build arg.我想读取由环境变量指定的文件的内容,并将其作为构建参数传递给 docker-compose。
So then in my Dockerfile I can do:那么在我的Dockerfile 中,我可以执行以下操作:

ARG MY_FILE
RUN echo "$MY_FILE" > /my-file

This works perfectly:这完美地工作:

docker-compose -f ./docker-compose.yml build --build-arg MY_FILE="$(cat $PATH_TO_MY_FILE)"

However, if I try to do this in docker-compose.yml like so:但是,如果我尝试在docker-compose.yml 中执行此操作,如下所示:

    build:
      context: .
      args:
        - MY_FILE="$(cat $PATH_TO_MY_FILE)"

it fails with this error:它因此错误而失败:

ERROR: Invalid interpolation format for "build" option in service "my-service": "MY_FILE="$(cat $PATH_TO_MY_FILE)""

Any idea how do I have to construct this string to have the same effect?知道我必须如何构造这个字符串才能产生相同的效果吗? I tried $$ etc, but doesn't seem to work...我试过 $$ 等,但似乎不起作用......

Thanks for your help :)谢谢你的帮助 :)

Docker compose doesn't support this, so you have to use a workaround only. Docker compose 不支持此功能,因此您只能使用一种解决方法。 Which would either mean pre-processing the compose file or generate the command you ran by reading the yaml and interpolating by generating the command in bash这意味着预处理撰写文件或通过读取 yaml 并通过在 bash 中生成命令进行插值来生成您运行的命令

You can use something like yq and parse the parameters from docker-compose.yml and generate your command.您可以使用yq东西并解析docker-compose.yml的参数并生成您的命令。 But honestly what you are doing right now is simple and effective.但老实说,你现在正在做的事情简单而有效。

In docker service 3, you can do that now.在 docker service 3 中,您现在可以这样做。

web:
  image: xxxx
  env_file:
    - web-variables.env

If you have specified a Compose file with docker-compose -f FILE , paths in env_file are relative to the directory that file is in.如果您使用 docker docker-compose -f FILE指定了 Compose 文件,则 env_file 中的路径相对于该文件所在的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM