Reviewing Docker Logs

Перевод: Работаем с логами контейнеров Docker

Docker Docker

Many of you know that it’s possible to access Docker container logs using “docker logs” command. But fewer people know that it’s possible to follow logs stream for new messages (like tail -f), and even fewer yet are aware that it’s possible to specify timestamps of the period you want to review – showing only specific logs during that period.

Show Docker Container Logs

Easiest command is this:

# docker logs mycontainer1

This will show you all the available logs for the mycontainer1, showing most recent logs last.

Following Docker Container Logs

To follow Docker container for new logs that happen after you start the command, use -f option (f for follow) – like this:

# docker logs -f mycontainer1

Initially output won’t be very different from the first example, without -f. But you won’t get command line prompt and if you wait long enough, new log messages will start appearing.

Docker Logs Around Certain Time

This is a true gem – using these options allows you not only to focus on specific time period, but also to automatically extract only certain logs based on their timestamps.

NOTE: It’s possible to specify proper timestamps – meaning both date and time, but I find that just the date part is more than enough.

For timestamp based logs access, use the –since and –until options of the docker logs command.

Here’s an example of looking at a Java based container, as you can see only the messages from March 30th and until April 1st are shown:

root@s2:~ # docker logs -f confluence --since 2020-03-30 --until 2020-04-01
30-Mar-2020 07:52:27.292 INFO [http-nio-8090-exec-7] org.apache.coyote.http11.Http11Processor.service Error parsing HTTP request header
  Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
     java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
         at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:415)
         at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:292)
         at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
         at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
         at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
         at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
         at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
         at java.lang.Thread.run(Thread.java:748)
30-Mar-2020 09:22:28.099 INFO [http-nio-8090-exec-1] org.apache.coyote.http11.Http11Processor.service Error parsing HTTP request header
  Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
     java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
         at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:415)
         at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:292)
         at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
         at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
         at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
         at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
         at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
         at java.lang.Thread.run(Thread.java:748)
30-Mar-2020 23:57:24.900 INFO [http-nio-8090-exec-3] org.apache.coyote.http11.Http11Processor.service Error parsing HTTP request header
  Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
     java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
         at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:415)
         at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:292)
         at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
         at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
         at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
         at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
         at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
         at java.lang.Thread.run(Thread.java:748)

That’s it for today, have fun with Docker!

See Also




Contact Me

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!
Recent Articles
03 Sep 2024

macOS Sequoia 15.0

02 Sep 2024

Video: Has This Worked Before?

24 Mar 2024

Homelab: Mac Pro 2013 32GB

Recent Tweets