【问题标题】:docker-compose - how to pass env variable from .env to Dockerfile?docker-compose - 如何将 env 变量从 .env 传递到 Dockerfile?
【发布时间】:2021-12-13 02:01:24
【问题描述】:

我有一个看起来像这样的 docker-compose,部分是:

  nginx:
    container_name: ${NGINX_CONTAINER_NAME}
    ports:
      - ${NGINX_HTTP_PORT1}:${NGINX_HTTP_PORT2}
      - ${NGINX_HTTPS_PORT1}:${NGINX_HTTPS_PORT2}
    build: 
      dockerfile: Dockerfile
      args: 
       - NGINX_VERSION=${NGINX_VERSION}
    volumes:
      - ${NGINX_CONF_DIR:-./nginx}:/etc/nginx/conf.d
      - ${NGINX_LOG_DIR:-./logs/nginx}:/var/log/nginx
      - ${WORDPRESS_DATA_DIR:-./wordpress}:/var/www/html
    depends_on:
      - wordpress
    restart: always

如您所见,我正在尝试将一个名为 NGINX_VERSION 的变量传递到 Dockerfile 中。

这是 .env 的内容:

# nginx
NGINX_VERSION=1.21.3
NGINX_HTTP_PORT1=8085
NGINX_HTTP_PORT2=8085
NGINX_HTTPS_PORT1=443
NGINX_HTTPS_PORT2=443
NGINX_CONTAINER_NAME=dev_nginx

这就是工作 Dockerfile 的样子:

FROM nginx:1.21.3

RUN apt-get update \
  && apt-get -y install openssl \
  && apt-get -y install vim

RUN mkdir -p /etc/openssl/certs
RUN mkdir -p /etc/nginx/snippets
RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/openssl/nginx-selfsigned.key -out /etc/openssl/nginx-selfsigned.crt -subj "/C=US/ST=NY/L=NY/O=ACME/OU=CD/CN=WPDeveloper"

问题

当我将 Dockerfile 中的图像名称更改为如下所示时:

 FROM nginx:$NGINX_VERSION

或者这个:

 FROM nginx:${NGINX_VERSION}

我收到以下错误:

failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition: failed to parse stage name "nginx:": invalid reference format

当我将版本号硬编码在 Dockerfile 中时,一切正常。 另外,如果有帮助,我会通过运行以下命令来验证 env 中的值是否被读取:

  docker-compose -f .\docker-compose.yml config

在某种程度上,这就是输出的样子 - 请注意它找到了 NGINX VERSION 的正确值

nginx:
    build:
      dockerfile: Dockerfile
      args:
        NGINX_VERSION: 1.21.3
    container_name: dev_nginx
    depends_on:
      wordpress:
        condition: service_started
    networks:
      default: null
    ports:
    - mode: ingress
      target: 8085
      published: 8085
    - mode: ingress
      target: 443
      published: 443
      protocol: tcp
    restart: always
    volumes:
      source: ./nginx
      target: /etc/nginx/conf.d
      bind:
        create_host_path: true
    - type: bind
      source: ./logs/nginx
      target: /var/log/nginx
      bind:
        create_host_path: true
    - type: bind
      source: ./wordpress
      target: /var/www/html
      bind:
        create_host_path: true

任何提示将不胜感激。

【问题讨论】:

标签: docker docker-compose environment-variables


【解决方案1】:

如果您希望 docker-compose 文件从 .env 文件中读取变量,然后将其传递给 Dockerfile,您可以按照以下设置进行操作:

这是您的 .env 文件包含的内容:

# nginx
NGINX_VERSION=1.21.3

这可以是您的 Dockerfile:

ARG NGINX_VERSION
FROM nginx:${NGINX_VERSION}
...

最后这将是您的 docker-compose.yml 文件:

nginx:
  build:
    context: .
    dockerfile: Dockerfile
    args:
      NGINX_VERSION: ${NGINX_VERSION}

  ...

最后运行:

docker-compose up

确保您的 .env 文件与 Dockerfile 和 docker-compose.yml 文件位于同一目录中。

【讨论】:

    【解决方案2】:

    您在 docker-compose 文件中指定了args,但我在您的 Dockerfile 中没有看到相应的ARG?您还需要注意how ARG and FROM interact

    这是一个最小的示例,我能够重新创建您的错误,然后通过在 FROM 语句之前添加 ARG 来修复它

    version: "3.8"
    services:
      test:
        build:
          context: ./
          dockerfile: Dockerfile
          args:
            - ALPINE_VERSION=3.14
    
    ARG ALPINE_VERSION=3
    
    FROM alpine:${ALPINE_VERSION}
    
    ENTRYPOINT [ "sh" ]
    

    缺少ARG 时出错

    failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition: failed to parse stage name "alpine:": invalid reference format

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 2022-10-09
      • 1970-01-01
      • 2019-05-27
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      相关资源
      最近更新 更多