简介
本文介绍Docker Compose如何解决容器时区不正确导致时间不正确的问题。
方案1:添加environment参数
修改docker-compose.yml文件添加environment参数:
environment: - TZ=Asia/Shanghai
完整文件如下:
version: '3'
services:
nginx:
image: 'nginx:latest'
restart: always
container_name: nginx
ports:
- 80:80
- 8081:8081
- 443:443
environment:
TZ : Asia/Shanghai
volumes:
- /app/nginx/conf.d:/etc/nginx/conf.d
- /app/nginx/logs:/etc/nginx/logs
command: nginx -g 'daemon off;'
方案2:绑定时间文件
修改docker-compose.yml文件绑定事件参数:
volumes: - /etc/timezone:/etc/timezone - /etc/localtime:/etc/localtime
完整文件
version: '3'
services:
nginx:
image: nginx:latest
restart: always
container_name: nginx
ports:
- 80:80
- 8081:8081
- 443:443
environment:
TZ : Asia/Shanghai
volumes:
- /app/nginx/conf.d:/etc/nginx/conf.d
- /app/nginx/logs:/etc/nginx/logs
- /etc/timezone:/etc/timezone
- /etc/localtime:/etc/localtime
command: nginx -g 'daemon off;'

请先 !