Using docker-compose to set containers timezones

后端 未结 5 2064
别跟我提以往
别跟我提以往 2020-12-25 11:21

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 b

相关标签:
5条回答
  • 2020-12-25 11:31

    This is simple solution:

    environment:
      - TZ=America/Denver
    
    0 讨论(0)
  • 2020-12-25 11:36
    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).

    0 讨论(0)
  • 2020-12-25 11:43

    The easiest solution would be share volume in docker-compose.yml like this

    ipchanger:
      image: codertarasvaskiv/ipchanger:raspberry
      volumes:
        - "/etc/localtime:/etc/localtime:ro"
    
    • ro - means container can only read from /etc/localtime of host-machine.
    0 讨论(0)
  • 2020-12-25 11:44
     version: '2'
     services:
       ServiceA:
         image: image:
       - '/etc/localtime:/etc/localtime:ro'
    
    0 讨论(0)
  • 2020-12-25 11:50
    version: '3.6'
    services:
      mysql:
        image: mysql:5.6
        restart: always
        container_name: dev-mysql
        command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
        restart: always
        environment:
         - TZ=Asia/Shanghai
         - MYSQL_ROOT_PASSWORD=password # set the root password
        ports:
          - '3306:3306'
        volumes:
          - "/etc/localtime:/etc/localtime:ro"
          - "/etc/timezone:/etc/timezone:ro"
    
    

    This my docker-compose.yaml of mysql

    Reminder,you need recreate container,other than restart. if you change the yaml need to recreate

    docker-compose -f docker-compose.yaml stop
    docker-compose -f docker-compose.yaml rm
    docker-compose -f docker-compose.yaml start
    
    0 讨论(0)
提交回复
热议问题