skip to Main Content

I want to see logs of container with timestamps but timezone of the logs are not set from ENV

version: '3.8'
services:
  api:
    build: .
    ports:
      - "3000:3000"
    environment:
      - TZ=Asia/Tehran

But after building the container usingdocker-compose up -build and running the command below to see logs of container, I see the timestamp is not set properly for Asia/Tehran:

docker-compose logs -ft api

2

Answers


  1. The base image that you used is important. Here’s a quick out-of-the-box example:

    docker run --init -d --rm -e TZ=Asia/Tehran centos bash -c 'while true; do echo "$(date) INFO log message."; sleep 1; done'
    docker logs -f <container>  # you will see the log date time uses container's TZ instead of the host
    

    enter image description here

    You can change the TZ to different timezone to see different log date time. If you include "-t" in the logs command, you will see the host date/time prepend to your log message.

    Login or Signup to reply.
  2. I don’t believe this is possible. The output of the timestamp in the logs when you run docker logs -t or docker-compose logs -t comes from the docker client which is forwarding the logs from the docker engine. Looking at the code for including the timestamp:

            if config.Timestamps {
                logLine = append([]byte(msg.Timestamp.Format(jsonmessage.RFC3339NanoFixed)+" "), logLine...)
            }
    

    The msg.Timestamp field is not passed through time.Local() so it should always be treated as UTC, no matter the timezone of the host running the docker engine, or the client calling the docker API.

    The timezone of the container doesn’t apply here unless you add the timestamp to your logs of your application itself and skip passing the -t option.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search