Docker-compose:在 env 文件中设置一个变量并在 Dockerfile 中使用它 [英] Docker-compose: Set a variable in env file and use it in Dockerfile

查看:50
本文介绍了Docker-compose:在 env 文件中设置一个变量并在 Dockerfile 中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Docker 和 Docker-compose 来构建一堆 nginx+php.

I'm using Docker and Docker-compose to build a stack of nginx+php.

我正在尝试在我的 .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]
date.timezone = "%s"
', $TIMEZONE > /usr/local/etc/php/conf.d/tzone.ini

容器内的时区设置不正确(在 php 容器 bash 内运行 php --info | grep timezone).如果我在 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

environment 被传递给正在运行的容器而不是构建文件.对于您需要在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆