Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tail a docker log from the current position in the log without seeing the whole file or waiting for it to traverse the whole file

Tags:

docker

logging

If you use the Coreutils tail command in Linux, you have a -f option that lets you follow a log file from the log's current position (it does not go to the very beginning of the file and display everything).

Is this functionality available in docker logs without waiting for it to traverse the whole log?

I have tried:

docker logs --since 1m somecontainer

and

docker logs -f --since 1m somecontainer

It appears that it actually traverses the entire log file (which can take a long time) and then starts echoing to the screen once it reaches the time frame you specify.

Is there a way to start tailing from the current point without waiting? Is my best option to always log out to some external file and tail that with the Coreutils tail command?

like image 942
Susan Avatar asked Aug 31 '18 16:08

Susan


People also ask

Can I Tail docker logs?

As you've seen, Docker provides multiple CLI options to display logs in different formats. For example, you can display logs for a specified point in time. In addition, the follow option is one of the most used Docker options because it allows developers to live tail logs for specific containers.

How do I fetch a container log?

You find these JSON log files in the /var/lib/docker/containers/ directory on a Linux Docker host. The <container_id> here is the id of the running container. If you're not sure which id is related to which container, you can run the docker ps command to list all running containers.

How do I view a docker log file?

Using Default Log File By default, Docker stores log files in a dedicated directory on the host using the json-file log driver. The log file directory is /var/lib/docker/containers/<container_id> on the host where the container is running.


3 Answers

Please read docker logs --help for help. Try below, starting from the last 10 lines. More details here.

docker logs -f --tail 10 container_name
like image 116
Light.G Avatar answered Nov 06 '22 10:11

Light.G


Alternatively, we can check the log by time (eg. since last 2mins) as:

docker logs --since=2m <container_id> // since last 2 minutes
docker logs --since=1h <container_id> // since last 1 hour
like image 33
rc.adhikari Avatar answered Nov 06 '22 10:11

rc.adhikari


use the --tail switch:

>  docker logs -f <container name> --tail 10

this will show the log starting from the last 10 lines onwards

like image 14
Dror Avatar answered Nov 06 '22 11:11

Dror