使用 docker-compose 设置容器时区 [英] Using docker-compose to set containers timezones

查看:255
本文介绍了使用 docker-compose 设置容器时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行一些 Dockerfile 的 docker-compose 文件来创建我的容器.我不想编辑我的 Dockerfile 来设置时区,因为我的团队成员可以随时更改它们,而且我有一个 docker-compose.override.yml 文件来更改本地环境.但是,我的一个容器(基于 Selenium 的容器)似乎没有拉取主机时区,这给我带来了问题.基于此,我想在我的所有容器上强制执行时区.现在在我的 Dockerfiles 中

I have a docker-compose file running a few Dockerfiles to create my containers. I don't want to edit my Dockerfiles to set timezones because they could change at any time by members of my team and I have a docker-compose.override.yml file to make local environment changes. However, one of my containers (a Selenium based one) seems to not pull host time zone and that causes problems for me. Based on that I want to enforce timezones on all my containers. In my Dockerfiles right now I do

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

一切正常.如何在 docker-compose 语法中复制相同的命令?

And everything works fine. How do I replicate the same command in docker-compose syntax?

推荐答案

version "2"

services:
  serviceA:
    ...
    environment:
      TZ: "America/Denver"
    command: >
      sh -c "ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && 
      echo $TZ > /etc/timezone &&
      exec my-main-application"

这个问题并没有要求它,但我刚刚添加了 exec my-main-application 来显示如何指定主进程.exec 在这里很重要,以确保 my-main-application 接收 Ctrl-C (SIGINT/SIGKILL).

The question didn't ask for it but I've just added exec my-main-application to show how the main process would be specified. exec is important here to make sure that my-main-application receives Ctrl-C (SIGINT/SIGKILL).

这篇关于使用 docker-compose 设置容器时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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