在 Dockerfile 中使用 env 变量

我在php项目中有一个Dockerfile,我需要在执行期间传递用户和密码来下载库。

用户和密码必须隐藏在生产中或本地 .env 文件中。目前,我只是尝试本地选项,用户和密码为空。

我用过 和 ,但不仅登录失败,而且当我打印变量时,它们会变为空。另外,我尝试将变量硬编码,并且工作正常,因此问题是变量无法从.env文件中检索。"${USER}"${USER}

Docker-compose 按如下方式开始

version: '3'

services:
  server:
    build:
      context: .
      dockerfile: docker/Server/Dockerfile
    container_name: "server"
    ports:
      - 80:80
      - 8888:8888
    networks:
      - network
    env_file:
      - .env
    command: sh /start-workers.sh

和 Dockerfile:

FROM php:7.3-cli-alpine3.10
RUN apk add --update

#
# Dependencies
#
RUN apk add --no-cache --no-progress \
    libzip-dev zip php7-openssl pkgconfig \
    php-pear php7-dev openssl-dev bash \
    build-base composer

#
# Enable PHP extensions
#
RUN docker-php-ext-install bcmath sockets pcntl

#
# Server Dependencies
#
RUN echo '' | pecl install swoole \
    && echo "extension=swoole.so" >> /usr/local/etc/php/conf.d/swoole.ini

#
# installation
#
WORKDIR /var/www/service
COPY . .

RUN echo "START"

RUN echo "${USER}"

RUN echo "${PASSWORD}"

RUN echo "END"


RUN composer config http.libraries.com "${USER}" "${PASSWORD}" --global \
    && composer install -n -o --prefer-dist --no-dev --no-progress --no-suggest \
    && composer clear-cache \
    && mv docker/Server/start-workers.sh /
EXPOSE 80

.env 的开头和结尾如下所示:

APP_ENV=dev
APP_SECRET=666666666666666
.
.
.
USER=user
PASSWORD=password

目前,如果我按如下方式执行输出docker-compose up --build

Step 10/15 : RUN echo "START"
 ---> Running in 329b1707c2ab
START
Removing intermediate container 329b1707c2ab
 ---> 362b915ef616
Step 11/15 : RUN echo "${USER}"
 ---> Running in e052e7ee686a

Removing intermediate container e052e7ee686a
 ---> 3c9cfd43a4df
Step 12/15 : RUN echo "${PASSWORD}"
 ---> Running in b568e7b8d9b4

Removing intermediate container b568e7b8d9b4
 ---> 26a727ba6842
Step 13/15 : RUN echo "END"
 ---> Running in 726898b3eb42
END

我希望打印用户和密码,这样我就知道我正在接收 .env 数据,并且可以使用它。


答案 1

您可以使用来满足您的要求。args

这里有一个注意事项:你不应该使用in作为关键字,因为它将被bash的默认环境覆盖,这将使你的dockerfile无法获得正确的值。USER.envUSER

一个完整的可行的最小示例,如下所示,仅供参考:

docker/Server/Dockerfile:

FROM php:7.3-cli-alpine3.10

ARG USER
ARG PASSWORD

RUN echo ${USER}
RUN echo ${PASSWORD}

.env(注意:你必须使用USR,而不是这里的用户):

USR=user
PASSWORD=password

docker-compose.yaml:

version: '3'

services:
  server:
    build:
      context: .
      dockerfile: docker/Server/Dockerfile
      args:
         - USER=${USR}
         - PASSWORD=${PASSWORD}

执行:

$ docker-compose build --no-cache
Building server
Step 1/5 : FROM php:7.3-cli-alpine3.10
 ---> 84d7ac5a44d4
Step 2/5 : ARG USER
 ---> Running in 86b35f6903e2
Removing intermediate container 86b35f6903e2
 ---> ee6a0e84c76a
Step 3/5 : ARG PASSWORD
 ---> Running in 92480327a820
Removing intermediate container 92480327a820
 ---> 1f886e8f6fbb
Step 4/5 : RUN echo ${USER}
 ---> Running in 8c207c7e6080
user
Removing intermediate container 8c207c7e6080
 ---> cf97b2cc0317
Step 5/5 : RUN echo ${PASSWORD}
 ---> Running in 7cbdd909826d
password
Removing intermediate container 7cbdd909826d
 ---> 6ab7987e080a
Successfully built 6ab7987e080a
Successfully tagged 987_server:latest

答案 2

这里的问题,您的bem只能从运行阶段访问,而不是。ENVbuild

我建议你使用构建的例子:args

build:
  context: .
  args:
    buildno: 1
    gitcommithash: cdc3b19

推荐