Docker-compose:在env文件中设置一个变量,并在Dockerfile中使用它 [英] Docker-compose: Set a variable in env file and use it in Dockerfile
问题描述
我正在使用Docker和Docker-compose构建一堆nginx + php。
I'm using Docker and Docker-compose to build a stack of nginx+php.
我正在尝试在<$ c $中设置时区c> .env 文件并在Dockerfile中使用它,但我可能会误解文档。
I'm trying to set the timezone in my .env
file and use it in a Dockerfile, but I might be missunderstanding something from the documentation.
.env
# Timezone
TIMEZONE=Europe/Madrid
docker-compose.yml
version '2'
services:
php:
build: php7-fpm
volumes:
- ${APP_PATH}:/var/www/app
- ./logs:/var/www/logs
environment:
TIMEZONE: ${TIMEZONE}
#[...more.stuff...]
php7-fpm / Dockerfile
FROM php:7.0-fpm
ARG TIMEZONE
#[...more.stuff...]
ENV TIMEZONE=${TIMEZONE}
RUN ln -snf /usr/share/zoneinfo/$TIMEZONE /etc/localtime && echo $TIMEZONE > /etc/timezone
RUN printf '[PHP]\ndate.timezone = "%s"\n', $TIMEZONE > /usr/local/etc/php/conf.d/tzone.ini
未设置时区正确地放在容器内(在php容器bash中运行 php --info | grep时区
)。如果我在Dockerfile中手动编写区域,则该区域有效。
The timezone is not set properly inside the container (running php --info | grep timezone
inside the php container bash). If I write the zone manually in the Dockerfile, it works.
推荐答案
您需要在docker compose中传递build参数
You need to pass the build argument in docker compose
version '2'
services:
php:
build:
dockerfile: php7-fpm
args:
TIMEZONE: ${TIMEZONE}
volumes:
- ${APP_PATH}:/var/www/app
- ./logs:/var/www/logs
环境
传递给正在运行的容器,而不传递给buildfile。对于您,您需要在 build
部分
The environment
are passed to the running container and not to the buildfile. For the you need to pass args
in the build
section
这篇关于Docker-compose:在env文件中设置一个变量,并在Dockerfile中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!