迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

Docker 部署Nginx 访问日志时间不正确的解决方式

作者:迹忆客 最近更新:2023/01/08 浏览次数:

在运营网站的过程中,始终没有太把日志当回事。虽然开启了日志记录,不过没有仔细的分析过日志。今天心血来潮看日志时发现记录的时间要比服务器的时间少8个小时。 8个小时,这是一个比较特殊的数字。我们知道 UTC 时区是标准的世界通用时区,而UTC+8 是比世界通用时间快8个小时的时区,而我们正好是在这个时区的。 我的网站是使用Docker+Nginx部署的,因此这里推断Docker使用的时区可能是UTC。而我们的服务器所在的时区是比其快8小时的。 为了验证我们的猜想,使用如下命令进入docker 容器进行查看

$ docker exec -it nginx bash

进入容器之后,执行date命令,显示如下结果:

docker 容器时区

很明显,docker容器的时区是UTC。知道了问题的所在,接下来就是校正docker容器的时区。网上目前基本上就三种方式:

  1. 共享主机的localtime
  2. 复制主机的localtime
  3. 创建自定义的dockerfile

我这里使用的是第一种方式,共享主机的localtime。我是使用的docker-compse。所以直接在配置文件中新增一行文件映射即可

version: '3'

services:
  web:
    ...
    volumes:
      ...
      - /etc/localtime:/etc/localtime
      ...
    ...

然后重新构建web镜像,并启动容器

$ docker-compose up web

这之后 nginx 再记录的日志的时间就和服务器同步了。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

How to Install Nginx on Ubuntu 20.04?

发布时间:2025/04/07 浏览次数:157 分类:OPERATING SYSTEM

Nginx is one of the most popular web servers in the world, responsible for hosting some of the largest and most trafficked sites on the Internet. It is a lightweight application software that can be used as a web server or a reverse proxy.

Install WordPress with Nginx on Ubuntu 18.04

发布时间:2025/04/07 浏览次数:86 分类:OPERATING SYSTEM

WordPress is one of the most popular open source content management systems (CMS) with a market share of up to 60% compared to other CMS like Drupal or Joomla. WordPress can be used to develop any type of website, be it a blog, a small busi

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类:Docker

In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t

Docker deploys nginx php application

发布时间:2025/03/26 浏览次数:131 分类:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

发布时间:2025/03/26 浏览次数:107 分类:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

发布时间:2025/03/26 浏览次数:97 分类:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

发布时间:2025/03/26 浏览次数:124 分类:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便