docker logs

Docker logs complete tutorial | Learn all about docker logs command in 2022

As we are all aware the future of application deployment is containerization, and more and more application deployment is moving to docker. It is always good to have a complete understanding of the docker logs. In this blog, I will take you through the docker logs. So let’s get started.

What is docker logs?

Docker provides a command called logs, using which we can analyze the docker container logs. When a docker container starts, it will add all the activity in the logs file, and using the log command; we can fetch all the logs.

There are different ways users can access the docker logs, and the docker logs command is one of them.

docker logs example

With the docker logs command, we can access the entire container logs. The general syntax of the docker logs command is:

docker logs [OPTIONS] CONTAINER

You can fetch the docker logs documentation by typing:

docker logs --help

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
  -n, --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)

docker logs example

This session will understand how docker logs help us debug the issues while deploying an image.

Example 1: Nginx container logs

In this example, we will be deploying an Nginx docker container and trying to fetch the logs generated by the Nginx container.

Run an Nginx container using the below command:

docker run -d --name my-nginx -p 8080:80 nginx:latest
af8512b74be6f19171ed93822d82f06183670bd44a8e689d1f0157c71976cc88
➜  ~

Verify if the container is running:

docker ps
CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS         PORTS                                   NAMES
af8512b74be6   nginx:latest     "/docker-entrypoint.…"   8 seconds ago   Up 4 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   my-nginx

The Nginx container logs can be found by typing the below command:

docker logs my-nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/07/09 16:05:17 [notice] 1#1: using the "epoll" event method
2021/07/09 16:05:17 [notice] 1#1: nginx/1.21.0
2021/07/09 16:05:17 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/07/09 16:05:17 [notice] 1#1: OS: Linux 5.10.25-linuxkit
2021/07/09 16:05:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/07/09 16:05:17 [notice] 1#1: start worker processes
2021/07/09 16:05:17 [notice] 1#1: start worker process 31
2021/07/09 16:05:17 [notice] 1#1: start worker process 32

From the above logs, we can see that the Nginx container starts without any error.

Now go to localhost:8080 to fetch the docker container UI.

docker logs

let’s rerun the docker logs command and see if the Nginx container has logged any more activity:

docker logs my-nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/07/09 16:05:17 [notice] 1#1: using the "epoll" event method
2021/07/09 16:05:17 [notice] 1#1: nginx/1.21.0
2021/07/09 16:05:17 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/07/09 16:05:17 [notice] 1#1: OS: Linux 5.10.25-linuxkit
2021/07/09 16:05:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/07/09 16:05:17 [notice] 1#1: start worker processes
2021/07/09 16:05:17 [notice] 1#1: start worker process 31
2021/07/09 16:05:17 [notice] 1#1: start worker process 32
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
2021/07/09 16:10:00 [error] 32#32: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080", referrer: "http://localhost:8080/"
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"

Nginx container has logged logs for any get request via UI. This log comes in very handy if you wish to troubleshoot any error in container deployment.

Docker logs command

docker logs command

In this session, we will go through all the docker logs commands that the data ops engineer should know.

docker logs -f command

Docker logs -f command used to get the logs in real-time as they are generated. For the above Nginx, container let’s run the logs command and pass -f flag.

docker logs -f my-nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/07/09 16:05:17 [notice] 1#1: using the "epoll" event method
2021/07/09 16:05:17 [notice] 1#1: nginx/1.21.0
2021/07/09 16:05:17 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/07/09 16:05:17 [notice] 1#1: OS: Linux 5.10.25-linuxkit
2021/07/09 16:05:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/07/09 16:05:17 [notice] 1#1: start worker processes
2021/07/09 16:05:17 [notice] 1#1: start worker process 31
2021/07/09 16:05:17 [notice] 1#1: start worker process 32
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
2021/07/09 16:10:00 [error] 32#32: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080", referrer: "http://localhost:8080/"
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"

Goto localhost:8080 and try to access the Nginx URL and refresh the page multiple times. You will observe that for each gets the request; new logs will be displayed in docker logs -f command:

docker logs -f my-nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/07/09 16:05:17 [notice] 1#1: using the "epoll" event method
2021/07/09 16:05:17 [notice] 1#1: nginx/1.21.0
2021/07/09 16:05:17 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/07/09 16:05:17 [notice] 1#1: OS: Linux 5.10.25-linuxkit
2021/07/09 16:05:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/07/09 16:05:17 [notice] 1#1: start worker processes
2021/07/09 16:05:17 [notice] 1#1: start worker process 31
2021/07/09 16:05:17 [notice] 1#1: start worker process 32
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
2021/07/09 16:10:00 [error] 32#32: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080", referrer: "http://localhost:8080/"
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:17 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:18 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:19 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:20 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:24 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"

These logs will be very beneficial if you wish to know who visited your website and using which browser search engine.

docker logs | grep

Docker logs followed by grepping are another way to grep only the logs we are interested in.

Suppose, in the above example, you wish to get only the GET request to Nginx web UI. You can achieve this using the below command:

docker logs my-nginx 2>&1 | grep "Apple"
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:17 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:18 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:19 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:20 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:24 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"

you have to use 2>&1 because the docker container is logging to stderr, and piping the grep command will only work for stdout.

docker logs head

With docker logs followed by the head command, you can fetch only the top logs generated by the docker container.

docker logs my-nginx | head
2021/07/09 16:05:17 [notice] 1#1: using the "epoll" event method
2021/07/09 16:05:17 [notice] 1#1: nginx/1.21.0
2021/07/09 16:05:17 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/07/09 16:05:17 [notice] 1#1: OS: Linux 5.10.25-linuxkit
2021/07/09 16:05:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/07/09 16:05:17 [notice] 1#1: start worker processes
2021/07/09 16:05:17 [notice] 1#1: start worker process 31
2021/07/09 16:05:17 [notice] 1#1: start worker process 32
2021/07/09 16:10:00 [error] 32#32: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080", referrer: "http://localhost:8080/"
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"

By default, the head command will give you the top 10 rows.

the Above logs contain stderr as well, and if you wish to get rid of them, you have to use the below command:

docker logs my-nginx 2>&1 | head
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/07/09 16:05:17 [notice] 1#1: using the "epoll" event method
2021/07/09 16:05:17 [notice] 1#1: nginx/1.21.0

Similarly, you can get the specific number of rows by passing -n after the head command.

n= number of rows to be fetched.

docker logs my-nginx 2>&1| head -5
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf

docker logs tail

Like the docker logs head command, we can use the docker logs tail command to get the bottom n rows.

docker logs my-nginx 2>&1| tail
2021/07/09 16:05:17 [notice] 1#1: start worker process 31
2021/07/09 16:05:17 [notice] 1#1: start worker process 32
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
2021/07/09 16:10:00 [error] 32#32: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080", referrer: "http://localhost:8080/"
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:17 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:18 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:19 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:20 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:24 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
➜  ~

Suppose you wish to get only 2 button rows. Run the below command to do so.

docker logs my-nginx 2>&1| tail -2
172.17.0.1 - - [10/Jul/2021:06:32:20 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:24 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
➜  ~

docker logs since

The docker logs since is another excellent command to fetch the logs from a specific time until now. Let’s run this command in the my-Nginx container and verify the output.

docker logs --since 2021-07-10 my-nginx
172.17.0.1 - - [10/Jul/2021:06:32:17 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:18 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:19 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:20 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:24 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"

In the above command, we have fetched the logs from 07-July-2021

docker logs until

docker until command is opposite of since command. Using docker until command, we can get logs before a timestamp.

docker logs --until 2021-07-10 my-nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/07/09 16:05:17 [notice] 1#1: using the "epoll" event method
2021/07/09 16:05:17 [notice] 1#1: nginx/1.21.0
2021/07/09 16:05:17 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/07/09 16:05:17 [notice] 1#1: OS: Linux 5.10.25-linuxkit
2021/07/09 16:05:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/07/09 16:05:17 [notice] 1#1: start worker processes
2021/07/09 16:05:17 [notice] 1#1: start worker process 31
2021/07/09 16:05:17 [notice] 1#1: start worker process 32
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
2021/07/09 16:10:00 [error] 32#32: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080", referrer: "http://localhost:8080/"
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"

docker logs location

Docker logs command is an excellent command to get information about the docker containers logs. But in case if you are curious, how you can get information about the location of the log.

We can use the docker inspect command to get all information about the docker container, and you can –format command to get logPath location.

docker inspect --format={{.LogPath}} my-nginx
/var/lib/docker/containers/af8512b74be6f19171ed93822d82f06183670bd44a8e689d1f0157c71976cc88/af8512b74be6f19171ed93822d82f06183670bd44a8e689d1f0157c71976cc88-json.log

docker logs of an exited container

So far, we have seen how we can get the logs a running container, but how can you retrieve the logs of an exited container?

The answer to this question is quite simple. The same way you get the logs of a running container, you can get logs of a stopped container.

Let’s stop the above my-nginx container which we ran.

docker kill my-nginx
my-nginx

Let’s verify if the container is exited.

docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED        STATUS                       PORTS                                   NAMES
af8512b74be6   nginx:latest      "/docker-entrypoint.…"   16 hours ago   Exited (137) 8 seconds ago                                           my-nginx

Now let’s try to fetch this exited nginx container log.

docker logs af8512b74be6
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/07/09 16:05:17 [notice] 1#1: using the "epoll" event method
2021/07/09 16:05:17 [notice] 1#1: nginx/1.21.0
2021/07/09 16:05:17 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/07/09 16:05:17 [notice] 1#1: OS: Linux 5.10.25-linuxkit
2021/07/09 16:05:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/07/09 16:05:17 [notice] 1#1: start worker processes
2021/07/09 16:05:17 [notice] 1#1: start worker process 31
2021/07/09 16:05:17 [notice] 1#1: start worker process 32
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
2021/07/09 16:10:00 [error] 32#32: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080", referrer: "http://localhost:8080/"
172.17.0.1 - - [09/Jul/2021:16:10:00 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:17 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:18 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:19 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:20 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"
172.17.0.1 - - [10/Jul/2021:06:32:24 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" "-"

NOTE: You can fetch the container logs by container name or by container id.

docker logs clear

In case you wish to clear all the logs generated by the docker container. There are many ways to achieve that, but one of the easiest ways is to use the below command.

$(docker inspect --format='{{.LogPath}}' my-nginx_lst)

We get the docker logs file name in the above command and use> arrow to put black data in that file.

Conclusion

Finally, we have come to the end of this fantastic tutorial. Here we have learned all about the docker logs. We have started with the basic docker logs command, and later we have seen how we can filter out the specific logs.

I hope you enjoy this tutorial. Please do let me know in the comment box.

More to read?

Hello-world docker image

Docker httpd image

Dockerfile tutorial for begineer

Leave a Comment

Your email address will not be published.

Scroll to Top