简体   繁体   中英

Docker-compose: Set a variable in env file and use it in Dockerfile

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

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

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.

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

The environment are passed to the running container and not to the buildfile. For the you need to pass args in the build section

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM