Home > Net >  Set timestamp of container to apply in docker logs
Set timestamp of container to apply in docker logs

Time:12-25

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

CodePudding user response:

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

You can change the TZ to different timezone to see different log date time.

CodePudding user response:

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.

  • Related