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

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

问题描述

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

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

一切正常。如何使用docker-compose语法复制同一命令?

解决方案

  version 2  

服务:
服务A:
...
环境:
TZ: America / Denver
命令:>
sh -c ln -snf / usr / share / zoneinfo / $ TZ / etc / localtime&&b; b $ b echo $ TZ> / etc / timezone&
exec my-main-application

编辑:该问题并没有要求,但我只是添加了 exec my-main-application 来显示如何指定主进程。 exec 在这里很重要,以确保 my-main-application 接收Ctrl-C(SIGINT / SIGKILL)。 / p>

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

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"

Edit: 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天全站免登陆