【问题标题】:Configure timezone in dockerized Nginx + PHP-FPM在 dockerized Nginx + PHP-FPM 中配置时区
【发布时间】:2018-01-17 03:19:59
【问题描述】:

我需要在 Dockerfile 中设置默认时区。我有两个容器(nginx 和 php7-fpm)。

当我进入 PHP 容器的 bash 并运行 php --info | grep timezone 时,我得到:

默认时区 => UTC

date.timezone => 无价值 => 无价值

我的 dockerfile 如下:

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

我尝试使用类似问题的答案,但没有结果。

注意:我使用docker-compose build 和docker-compose up -d 来运行完整的堆栈,也就是this one。

【问题讨论】:

    标签: php docker nginx dockerfile


    【解决方案1】:

    有两种类型的时区设置。一是系统级。您可以使用/etc/localtime进行设置

    请参阅下面的 Dockerfile 步骤

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

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

    也可以参考另一篇文章Using docker-compose to set containers timezones

    接下来是 PHP/APP 级别的设置。为此,您可以创建一个 ini 文件。这可以通过在 Dockerfile 中添加以下行来完成

    RUN printf '[PHP]\ndate.timezone = "US/Central"\n' > /usr/local/etc/php/conf.d/tzone.ini
    

    【讨论】:

    • 您的第二个代码块中有错字。但无论如何,我已经尝试了第一个,这不是我需要的,第二个似乎不起作用:(
    • 有什么问题?对我来说它有效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
    • 不适合我...我还评论了我以前的时区行,但一定是其他事情搞砸了。
    • 发布您更新的 Dockerfile。让我用你的那个
    • 我的错误扩展需要.ini 而不是.conf。我先尝试使用conf,然后尝试使用ini,然后错误地使用conf发布了解决方案
    猜你喜欢
    • 2012-12-23
    • 2012-07-20
    • 1970-01-01
    • 2017-03-29
    • 2017-09-23
    • 2012-08-16
    • 2017-05-25
    • 2018-04-27
    • 1970-01-01
    相关资源
    最近更新 更多