
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.
Here is a list of the most commonly used commands to view logs in Docker.
Continue reading for detailed explanations and examples of each command.
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[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.
Create and run a demo container based on the latest version of the nginx image.
This command creates a container test-app and starts it.
Verify the container.
You should see the test-app container listed in the output.
View the logs of the container.
View logs with timestamps.
View only the last 10 lines of logs.
View logs from the last 5 minutes.
Follow the logs of the application container.
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.
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[OPTIONS] modify the command behavior.[SERVICE...] is the optional service name. If omitted, the command shows logs from all services.Create a docker-compose.yaml manifest to define a multi-container Docker compose application.
Paste the following contents.
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 latest nginx image. It maps the port 8080 on the host to the port 80 in the container and depends on the app service to start first.app: It is a simple application based on node: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 the postgres:13 image. It's configured with a password and a database name through environment variables.Start the services.
Verify the created containers.
You should see a list of containers with the field SERVICE indicating its service name.
View logs from all services.
View logs from only the web service.
View logs from multiple services.
Follow logs from a specific service with limited history.
View logs with timestamps and follow mode.
Combine multiple options.
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.
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.
0 Comments
Be the first to comment and share your perspective with the community.