
Docker is a platform that allows you to create, run, and manage applications in a container. As containerized applications run in isolated environments, you may need to monitor them and sometimes troubleshoot issues. A container generates logs of activities and events. Docker collects the standard output (stdout
) and standard error (stderr
) streams from the main process running inside the container and makes them accessible through logging commands.
This article explains how to check the logs of Docker containers and Docker Compose services.
The Short Answer
Here is a list of the most commonly used commands to view logs in Docker.
# View logs of a specific container
$ docker container logs [CONTAINER]
# View live logs in real-time
$ docker container logs --follow [CONTAINER]
# View the last 10 lines of logs
$ docker container logs --tail 10 [CONTAINER]
# View logs from Docker Compose services
$ docker compose logs [SERVICE]
# Follow all Docker Compose service logs
$ docker compose logs -f
Continue reading for detailed explanations and examples of each command.
View Container Logs
To view the logs of a container, you can use the docker container logs
command. It shows all the container output written to stdout and stderr.
docker container logs
Command Syntax
docker container logs [OPTIONS] [CONTAINER]
[OPTIONS]
modify the command behavior.[CONTAINER]
is the name or the ID of the container whose logs you want to see.
This command works for both running and stopped containers.
Command Options
Option(s) | Description |
---|---|
-t , --timestamps |
Adds timestamps to each log entry. |
--tail <number> |
Shows only the last N lines of logs. |
--since <timestamp> |
Shows logs since a specific timestamp or relative time. |
--until <timestamp> |
Shows logs until a specific timestamp or relative time. |
-f , --follow |
Follows log output in real-time. |
Command Demonstration
Create and run a demo container based on the latest version of the
nginx
image.console$ docker run -d --name test-app nginx:latest
This command creates a container
test-app
and starts it.Verify the container.
console$ docker container ls
You should see the
test-app
container listed in the output.View the logs of the container.
console$ docker container logs test-app
View logs with timestamps.
console$ docker container logs --timestamps test-app
View only the last 10 lines of logs.
console$ docker container logs --tail 10 test-app
View logs from the last 5 minutes.
console$ docker container logs --since 5m test-app
Follow the logs of the application container.
console$ docker container logs --follow test-app
The command shows existing logs and occupies the terminal while waiting for new entries. As the nginx container receives requests, new log entries appear in real-time.
View Logs from Docker Compose
Docker Compose applications run multiple connected services, each in its own container. You can view the logs of these services using the docker compose logs
command.
docker compose logs
Command Syntax
$ docker compose logs [OPTIONS] [SERVICE...]
[OPTIONS]
modify the command behavior.[SERVICE...]
is the optional service name. If omitted, the command shows logs from all services.
Command Options
Option(s) | Description |
---|---|
--tail <number> |
Show only the last N lines from each service. |
--since <timestamp> |
Show logs since a specific time. |
-f , --follow |
Follow log output in real-time. |
-t , --timestamps |
Add timestamps to log entries. |
--no-color |
Disable colored output. |
--no-log-prefix |
Don't print service names before log lines. |
Command Demonstration
Create a
docker-compose.yaml
manifest to define a multi-container Docker compose application.console$ nano docker-compose.yaml
Paste the following contents.
yamlservices: web: image: nginx:latest ports: - "8080:80" depends_on: - app app: image: node:16-alpine command: sh -c "while true; do echo 'App is running on $(date)'; sleep 3; done" database: image: postgres:13 environment: POSTGRES_PASSWORD: secret POSTGRES_DB: myapp
Save and exit the manifest file.
This file creates a Docker application with the following three services:
web
: It is a web server based on the latestnginx
image. It maps the port8080
on the host to the port80
in the container and depends on theapp
service to start first.app
: It is a simple application based onnode:16-alpine
image. It runs a shell command that logs a message "App is running..." with timestamp every three seconds.database
: It is a database service app based on thepostgres:13
image. It's configured with a password and a database name through environment variables.
Start the services.
console$ docker compose up -d
Verify the created containers.
console$ docker compose ps
You should see a list of containers with the field
SERVICE
indicating its service name.View logs from all services.
console$ docker compose logs
View logs from only the
web
service.console$ docker compose logs web
View logs from multiple services.
console$ docker compose logs web app
Follow logs from a specific service with limited history.
console$ docker compose logs --follow --tail 10 app
View logs with timestamps and follow mode.
console$ docker compose logs --follow --timestamps
Combine multiple options.
console$ docker compose logs --follow --timestamps --tail 20 --since 5m web database
This command shows logs from both the web and database services, showing timestamps, limiting to the last 20 lines, and only displaying logs from the past 5 minutes.
Conclusion
You have learned how to check the logs of Docker containers and Docker compose services. You can now view container logs using commands like docker container logs
, follow live logs with the --follow
option, and check logs of multi-container Docker compose services using docker compose logs
. Visit the Docker Documentation for more information and advanced configuration options.
No comments yet.