在 Dockerfile 中使用 env 变量
2022-08-30 23:21:26
我在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 数据,并且可以使用它。