9

I need to set the default timezone in a Dockerfile. I have two containers (nginx and php7-fpm).

When I enter the PHP container's bash and run php --info | grep timezone I get:

Default timezone => UTC

date.timezone => no value => no value

My dockerfiles are the following:

nginx/Dockerfile:

FROM debian:jessie

RUN apt-get update && apt-get install -y nginx

ADD nginx.conf /etc/nginx/
ADD site.conf /etc/nginx/sites-available/

RUN ln -s /etc/nginx/sites-available/site.conf /etc/nginx/sites-enabled/site
RUN rm /etc/nginx/sites-enabled/default

RUN echo "upstream php-upstream { server php:9000; }" > /etc/nginx/conf.d/upstream.conf

RUN usermod -u 1000 www-data

CMD ["nginx"]

EXPOSE 80
EXPOSE 443

php-fpm/Dockerfile:

FROM php:7.0-fpm

RUN apt-get update && apt-get install -y git unzip

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer --version

RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/Madrid /etc/localtime
RUN "date"

RUN docker-php-ext-install pdo pdo_mysql

RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
RUN echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_connect_back=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.idekey=\"PHPSTORM\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_port=9001" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

WORKDIR /var/www/site

I've tried to use the answers of similar questions with no results.

Note: I'm using docker-compose build and docker-compose up -d to run the complete stack, which is exactly this one.

Community
  • 1
  • 1
CarlosAS
  • 654
  • 2
  • 10
  • 31

1 Answers1

32

There are two type of Time zone settings. One is a system level. That you can set using /etc/localtime

See the Dockerfile steps below

ENV TZ=America/Los_Angeles
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

PS: Taken from https://serverfault.com/questions/683605/docker-container-time-timezone-will-not-reflect-changes

Also you can refer to another article Using docker-compose to set containers timezones

Next is the PHP/APP level setting. For that you can create a ini file. Which can be done by adding below line to Dockerfile

RUN printf '[PHP]\ndate.timezone = "US/Central"\n' > /usr/local/etc/php/conf.d/tzone.ini
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • You got a typo in your second code block. But anyway, I already tried the first one, which is not what I need, and the second one doesn't seem to work :( – CarlosAS Aug 09 '17 at 10:47
  • What is the issue? For me it works `root@e3d4bc020357:/var/www/html# php -i | grep zone Additional .ini files parsed => /usr/local/etc/php/conf.d/tzone.ini "Olson" Timezone Database Version => 2017.2 Timezone Database => internal Default timezone => US/Central date.timezone => US/Central => US/Central` – Tarun Lalwani Aug 09 '17 at 10:51
  • Not for me... I also commented my previous timezone lines, but something else must be messing it up. – CarlosAS Aug 09 '17 at 11:05
  • Post your updated Dockerfile. Let me use your exact one – Tarun Lalwani Aug 09 '17 at 11:21
  • 1
    My bad the extension needs to `.ini` and not `.conf`. I tried with conf first and then ini and by mistake posted the solution with conf – Tarun Lalwani Aug 09 '17 at 11:40
  • Got it, before install nginx you need to set the time zone first. Thanks a lot – Emdadul Sawon Sep 04 '20 at 07:50