将 shell 中指定的 ENV 变量从 docker-compose 传递到 Dockerfile

Pass ENV variables specified in shell from docker-compose to Dockerfile

我想在构建 Docker 图像时设置 DEPLOY_ENV 环境变量,但该值似乎没有进入 Docker 文件。这是我的构建命令:

docker-compose.yml

version: '3'
services:
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    environment:
      - DEPLOY_ENV=${DEPLOY_ENV:-development}
    ports:
      - "3000:3000"
    volumes:
      - ".:/app"

当我 运行 DEPLOY_ENV=staging docker-compose config 时,我可以看到它按预期替换了 DEPLOY_ENV 变量:

services:
  web:
    build:
      context: /home/user/app
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b
      '0.0.0.0'"
    environment:
      DEPLOY_ENV: staging
    ports:
    - published: 3000
      target: 3000
    volumes:
    - /home/user/app:/app:rw
version: '3'

但是,我不知道如何在我的 Docker 文件中填充该值。这是我用来测试的 Docker 文件:

FROM ruby:2.7.2

SHELL ["/bin/bash", "-c"]

ENV RAILS_ENV=${DEPLOY_ENV}
ENV NODE_ENV=${DEPLOY_ENV}
ENV APP_HOME=/app

LABEL app=app
LABEL environment=${RAILS_ENV}

RUN echo ${DEPLOY_ENV}

当我运行docker-compose build时,它不会显示DEPLOY_ENV的值:

Building web
[+] Building 1.4s (6/6) FINISHED                                                                                                                                                            
 => [internal] load build definition from Dockerfile                                                                                                                                   0.1s
 => => transferring dockerfile: 1.55kB                                                                                                                                                 0.0s
 => [internal] load .dockerignore                                                                                                                                                      0.1s
 => => transferring context: 35B                                                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/ruby:2.7.2                                                                                                                          1.0s
 => [1/2] FROM docker.io/library/ruby:2.7.2@sha256:1dd0106849233fcd913b7c4608078fa1a5sd5e3ce1af2a55e4d726b0d8868e2f                                                                    0.0s
 => CACHED [2/2] RUN echo ${DEPLOY_ENV}                                                                                                                                                0.0s
 => exporting to image                                                                                                                                                                 0.1s
 => => exporting layers                                                                                                                                                                0.0s
 => => writing image sha256:27e159270471d3078fff8eb4eads7e4e586b345bfa13d3bdb3ec317266678549                                                                                           0.0s
 => => naming to docker.io/library/app_web

我计划在构建图像时设置秘密值,并希望避免对这些值进行硬编码或使用构建参数,因为它们可能会暴露。

我错过了什么?

您可以在 docker compose 中使用 args 原语,它会在您的 Dockerfile

中传递构建变量
args:
  DEPLOY_ENV_ARG: ${DEPLOY_ENV:-development}
environment:
  - DEPLOY_ENV=${DEPLOY_ENV:-development}

在你的 Dockerfile 中

ARG DEPLOY_ENV_ARG
ENV RAILS_ENV=${DEPLOY_ENV_ARG}