如何将Env/Build Arg从Docker-Compose脚本传递到Dockerfile入口处命令?

发布于 2025-02-08 04:53:31 字数 1033 浏览 2 评论 0原文

我想将env/build var传递给我的dockerfile,以便在其入口处使用,并认为我可以从docker-compose文件中这样做,因此

  web:
    restart: "no"
    build:
      context: ../my-project
      args:
        CURRENT_ENV: "development"

在我的dockerfile中,我已经定义了这个定义

ARG CURRENT_ENV
ENTRYPOINT /bin/sh -c "rm -f /app/tmp/pids/*.pid && [[ $CURRENT_ENV = 'dev' ]] && /usr/local/rbenv/versions/`cat .ruby-version`/gemsets/my-project/bin/foreman start -f Procfile || /usr/local/rbenv/versions/`cat .ruby-version`/gemsets/my-project/bin/foreman start -f Procfile.hot; tail -f /dev/null"

,但是当我使用“ docker-compose”启动容器时在“ ='dev'”部分之前,入口点似乎没有拾取变量……

myco-deploy-web-1    | /bin/sh: -c: line 0: `rm -f /app/tmp/pids/*.pid && [[  = 'dev' ]] && /usr/local/rbenv/versions/2.4.5/gemsets/my-project/bin/foreman start -f Procfile || /usr/local/rbenv/versions/2.4.5/gemsets/my-project/bin/foreman start -f Procfile.hot; tail -f /dev/null'

什么是将构建arg/env var传递给我的入口处命令的正确方法?

I want to pass an env/build var to my Dockerfile for use in its entryxoint and thought I could do it from the docker-compose file like so

  web:
    restart: "no"
    build:
      context: ../my-project
      args:
        CURRENT_ENV: "development"

In my Dockerfile, I have this defined

ARG CURRENT_ENV
ENTRYPOINT /bin/sh -c "rm -f /app/tmp/pids/*.pid && [[ $CURRENT_ENV = 'dev' ]] && /usr/local/rbenv/versions/`cat .ruby-version`/gemsets/my-project/bin/foreman start -f Procfile || /usr/local/rbenv/versions/`cat .ruby-version`/gemsets/my-project/bin/foreman start -f Procfile.hot; tail -f /dev/null"

However when I start my containers using “docker-compose up”, it doesn’t appear the entry point is picking up the variable as it has a empty string before the “= ‘dev’” section …

myco-deploy-web-1    | /bin/sh: -c: line 0: `rm -f /app/tmp/pids/*.pid && [[  = 'dev' ]] && /usr/local/rbenv/versions/2.4.5/gemsets/my-project/bin/foreman start -f Procfile || /usr/local/rbenv/versions/2.4.5/gemsets/my-project/bin/foreman start -f Procfile.hot; tail -f /dev/null'

What’s the proper way to pass the build arg/env var to my ENTRYPOINT command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2025-02-15 04:53:31

入口点需要变量为环境变量,而不是构建arg。

你可以做到这一点

ARG CURRENT_ENV
ENV CURRENT_ENV $CURRENT_ENV
ENTRYPOINT /bin/sh -c "rm -f /app/tmp/pids/*.pid && [[ $CURRENT_ENV = 'dev' ]] && /usr/local/rbenv/versions/`cat .ruby-version`/gemsets/my-project/bin/foreman start -f Procfile || /usr/local/rbenv/versions/`cat .ruby-version`/gemsets/my-project/bin/foreman start -f Procfile.hot; tail -f /dev/null"

,它应该起作用

The ENTRYPOINT needs the variable to be an environment variable rather than a build arg.

You can do this

ARG CURRENT_ENV
ENV CURRENT_ENV $CURRENT_ENV
ENTRYPOINT /bin/sh -c "rm -f /app/tmp/pids/*.pid && [[ $CURRENT_ENV = 'dev' ]] && /usr/local/rbenv/versions/`cat .ruby-version`/gemsets/my-project/bin/foreman start -f Procfile || /usr/local/rbenv/versions/`cat .ruby-version`/gemsets/my-project/bin/foreman start -f Procfile.hot; tail -f /dev/null"

and it should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文