我在php项目中有一个Dockerfile,我需要在执行过程中传递用户名和密码来下载库。
用户和密码必须隐藏在生产环境或本地.env文件中。目前,我只是尝试使用本地选项,而用户名和密码为空。
我使用了"${USER}"
和${USER}
,但不仅登录失败,而且当我打印变量时,它们变为空。另外,我还尝试将变量进行硬编码,并且可以正常工作,因此问题是这些变量无法从.env文件中检索。
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数据并且可以使用它。
答案 0 :(得分:2)
您可以使用args
来满足您的要求。
这里有一个注意事项:您不应该在USER
中使用.env
作为关键字,因为它将被bash的默认环境USER
覆盖,这将使您的dockerfile无法获取正确的值。
以下是一个完全可行的最小示例,仅供参考:
docker / Server / Dockerfile:
FROM php:7.3-cli-alpine3.10
ARG USER
ARG PASSWORD
RUN echo ${USER}
RUN echo ${PASSWORD}
.env(注意:您必须使用USR,而不是USER):
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
答案 1 :(得分:1)
这里的问题是您的ENV
将只能从运行阶段而不是build
被访问。
例如,建议您使用内部版本args
build:
context: .
args:
buildno: 1
gitcommithash: cdc3b19