Bootstrap FreeKB - Docker - docker logs command
Docker - docker logs command

Updated:   |  Docker articles

This assumes you have installed Docker on LinuxDocker is running, and that you have created a container. The docker logs command is used to view whatever logs the container is sending to stdout and stderr.  You may want to first issue the docker ps command to get the name of the containers.

The docker ps command can be used to determine if the container is running.

docker ps

CONTAINER ID     IMAGE              COMMAND                 CREATED         STATUS     PORTS     NAMES
d937372c09ab9    b939aa938add9913   "/docker-entrypoin..."  6 minutes ago   Created              my-container

 

In this example, the logs for the container named my-container will be displayed.

docker logs my-container

 

Each container will have different log files that are being sent to stdout and stderr. For example, the tiangolo/uwsgi-nginx-flask image is configured to send nginx access.log to stdout and nginx error.log to stderr.

~]$ sudo docker exec recipes ls -l /var/log/nginx
total 0
lrwxrwxrwx. 1 root root 11 Sep  4  2023 access.log -> /dev/stdout
lrwxrwxrwx. 1 root root 11 Sep  4  2023 error.log -> /dev/stderr

 

The --follow option can be used to watch the logs in real time.

docker logs my-container --follow

 

The -t or --timestamps option can be included to include the datetime in the output.

docker logs my-container --timestamps

 

The --since and --until options can be included to only show events within a specified datetime.

docker logs my-container --since 2020-11-14T08:00:00 --until 2020-11-14T11:00:00

 

The --tail=10 option can be included to only list the last 10 messages in the log.

docker logs my-container --tail=10

 

You may need to use 2>&1 to grep the logs.

docker logs my-container 2>&1 | grep foo

 

The logs are written to a JSON log file.

~]$ sudo docker inspect my-container --format='{{.LogPath}}'
/var/lib/docker/containers/7b05e7ae67ec2869ed0ce6b5e9589e6bb65eda3d6345147d94457b277addc9d7/7b05e7ae67ec2869ed0ce6b5e9589e6bb65eda3d6345147d94457b277addc9d7-json.log

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 46cdb8 in the box below so that we can be sure you are a human.