Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Where does Docker save the log file?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about where Docker saves log files, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Debugging most Linux programs usually involves checking log files, which can be a complex process. However, when running in a containerized environment under Docker, you need to use more specific tools to debug applications in production.

Where are the logs stored?

The simple answer is that Docker stores container logs in its primary storage location / var/lib/docker/. Each container has a log specific to its ID (full ID, rather than the shortened ID that usually appears), which you can access like this:

/ var/lib/docker/containers/ID/ID-json.log

This is where they are stored, but because they are in JSON format, they are not easy to read, and having to use a full container ID is annoying. Docker provides a built-in command to view them:

Docker logs-f e4bd48ef3103

Here, the-f flag will keep the prompt open and "follow" any new entries in the file. You can also use-tail the file, or use-timestamps to display the log time, or use-until and-- since to filter by time.

If you use Docker Compose, you can easily view all logs using the log command:

Docker-compose logs

However, one thing you will notice is STDOUT and STDERR, which are useful for many things, but only display the console output from the entry point specified by "CMD" in the Docker file. Many applications have their own dedicated logging systems, which are usually logged to / var/log/nginx/access.log. Such logs can still be accessed from the host side through Docker.

View logs from an application in the container

Depending on the container, this may not be necessary. For example, the default NGINX container is set to send its Docker logs to STDOUT to simplify log checking. It does this using a symbolic link from / dev/stdout to the log file, and you can set something similar for the container.

RUN ln-sf / dev/stdout / var/log/nginx/access.log & & ln-sf / dev/stderr / var/log/nginx/error.log

However, if you want to view specific files in the container, you can do so. The exec-it command provided by Docker allows you to run any command in any running Docker process. Using it, you can track log files within the Docker container:

Docker exec-it e4bd48ef3103 tail-f log.txt

Because this allows you to run any command, you can use journalctl or any other debugging strategy you want, as long as you use docker exec-it. / bin/bash if you want to jump in and look around, you can even run.

A more permanent solution for hosting services is to use Docker volume mounts. You can bind a similar directory / var/log/nginx to a volume visible to the host. First, create a new volume:

Docker volume create nginx-logs

And run the container-mount using the following command:

Docker run-d\-- name devtest\-- mount source=nginx-logs,target=/var/log/nginx\ nginx:latest

If you use Docker Compose, the process can be automated:

Version: "3 services: web: image: nginx:latest ports: -" 80:80 "volumes:-nginx-logs:/var/log/nginx/volumes: nginx-logs:

In this way, any log aggregation service on the host can directly ingest log files.

View the Docker daemon log

If you want to view specific logs for the entire Docker service on the server, rather than any specific containerized applications, you need to view the journalctl logs:

Sudo journalctl-fu docker.service

This is where it is stored on most systems, but it is different on some systems:

Amazon Linux: / var/log/docker

CentOS/RHEL: / var/log/messages | grep docker

Apple: ~ / Library/Containers/com.docker.docker/Data/log/vm/dockerd.log

Windows: AppData\ Roaming\ Docker\ log\ vm\ dockerd.log

The above is where Docker saves the log files, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report