How to find Docker logs?

If you are a system administrator and responsible for building and managing containerized applications, docker logging is one of the most important for you. Docker logs help you to debug and troubleshoot issues faster.  By default, docker logs stored in the /var/lib/docker/containers/<container_id> directory on a docker host where the container is running. It uses a json-file driver to store logs for each container

In this tutorial, we will show you how to find and display docker logs in Linux.

Requirements

  • A Linux system with Docker installed.
  • A root password is configured in your system.

Basic Syntax

The basic syntax of docker logs is shown below:

docker logs [OPTION]

A brief explanation of each option is shown below:

-f : This option is used to follow the Docker container logs.

–tail : This option is used to display the last number of log lines you specify.

-t : This option is used to display the timestamps of the log lines.

–details : This option is used to display the extra information about the log lines.

How to View Docker Logs

When you run any container in detached mode, you can not see any logs in the console. In this case, you can use the docker logs command to view the container logs.

If you want to view the docker container logs, you will need to list all running containers on your docker host.

You can list them with the following command:

docker ps

You should see the running container with container id in the following output:

docker ps

CONTAINER ID  IMAGE      COMMAND                 CREATED       STATUS       PORTS                     NAMES
ba43241e3ce3  nginx  "/docker-entrypoint.…"   5 hours ago   Up 5 hours   0.0.0.0:8080->     80/tcp frosty_bassi

Now, run the following command to view the container logs:

docker logs ba43241e3ce3

You should see the following output:

91.234.62.16 - - [10/Jul/2020:11:36:26 +0000] "POST /GponForm/diag_Form?images/
HTTP/1.1"
404 153 "-" "Hello, World" "-"

91.234.62.16 - - [10/Jul/2020:11:36:26 +0000] "h+/tmp/gpon8080&ipv=0"
400 157 "-" "-" "-"

128.14.209.154 - - [10/Jul/2020:12:01:15 +0000] "GET / HTTP/1.1" 200 612 "-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
 Chrome/60.0.3112.113 Safari/537.36"
"-"

2020/07/10 12:01:15 [error] 28#28: *13 "/usr/share/nginx/html/webfig/index.html"
is not found (2: No such file or directory), client: 128.14.209.154, server: localhost,
request: "GET /webfig/ HTTP/1.1", host: "104.245.36.46:8080"

128.14.209.154 - - [10/Jul/2020:12:01:15 +0000] "GET /webfig/ HTTP/1.1" 404 555 "-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/60.0.3112.113 Safari/537.36"
"-"

To see the last 5 lines of nginx container logs, run the following command:

docker logs --tail 5 ba43241e3ce3

You should see only 5 lines as shown below:

91.234.62.16 - - [10/Jul/2020:11:36:26 +0000] "POST /GponForm/diag_Form?images/
HTTP/1.1"
404 153 "-" "Hello, World" "-"

91.234.62.16 - - [10/Jul/2020:11:36:26 +0000] "h+/tmp/gpon8080&ipv=0"
400 157 "-" "-" "-"

128.14.209.154 - - [10/Jul/2020:12:01:15 +0000] "GET / HTTP/1.1" 200 612 "-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/60.0.3112.113 Safari/537.36"
"-"

2020/07/10 12:01:15 [error] 28#28: *13 "/usr/share/nginx/html/webfig/index.html"
is not found (2: No such file or directory), client: 128.14.209.154, server: localhost,
request: "GET /webfig/ HTTP/1.1", host: "104.245.36.46:8080"

128.14.209.154 - - [10/Jul/2020:12:01:15 +0000] "GET /webfig/ HTTP/1.1" 404 555 "-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
 Chrome/60.0.3112.113 Safari/537.36"
"-"

To see the container logs continuously, run the following command:

docker logs --follow ba43241e3ce3

You can also see the docker logs file for Nginx container located at /var/lib/docker/containers/ directory.

First, list all files inside Nginx contaner with the following command:

ls -l /var/lib/docker/containers/ba43241e3ce3951d8599ce87450c64ea
944c45e484922dbccbb22231a3ab244a/

You should see the following output:

-rw-r----- 1 root root 5198 Jul 10 08:01 ba43241e3ce3951d8599ce87450c64ea
944c45e484922dbccbb22231a3ab244a-json.log
drwx------ 2 root root 4096 Jul 10 03:39 checkpoints
-rw------- 1 root root 2841 Jul 10 03:39 config.v2.json
-rw-r--r-- 1 root root 1512 Jul 10 03:39 hostconfig.json
-rw-r--r-- 1 root root   13 Jul 10 03:39 hostname
-rw-r--r-- 1 root root  174 Jul 10 03:39 hosts
drwx------ 2 root root 4096 Jul 10 03:39 mounts
-rw-r--r-- 1 root root  616 Jul 10 03:39 resolv.conf
-rw-r--r-- 1 root root   71 Jul 10 03:39 resolv.conf.hash

Now, see the nginx container log with the following command:

tail -f /var/lib/docker/containers/ba43241e3ce3951d8599ce87450c64ea944c45e484922dbccb
b22231a3ab244a/ba43241e3ce3951d8599ce87450c64ea944c45e484922dbccbb22231a3ab244a-json.log

Conclusion

In the above guide, you learned what docker logs is and how to find docker logs with the command line. I hope this will helps you to troubleshoot any issues of containerized applications.



from Linux Hint https://ift.tt/2WXloKx

Post a Comment

0 Comments