The “docker logs” command is a Docker CLI command used to retrieve and print the logs generated by a running Docker container. It allows users to access and view the standard output (stdout) and standard error (stderr) streams of a container.
When executing the “docker logs” command, you specify the target container by its ID or name. Docker retrieves the logs associated with that container and displays them in the terminal or command prompt.
Here are the main functionalities and use cases of the “docker logs” command:
- Viewing Container Logs: The primary purpose of the “docker logs” command is to view the logs generated by a container. Containers often produce various types of log messages, including application output, error messages, debugging information, and other relevant information. By using “docker logs,” users can easily access and monitor these logs in real-time.
- Troubleshooting and Debugging: When a container encounters issues or errors, examining its logs is an essential step in troubleshooting and debugging. The “docker logs” command provides valuable insights into the container’s execution, allowing users to identify potential issues, trace error messages, and analyze the container’s behavior.
- Monitoring Container Health and Performance: Monitoring container logs is crucial for understanding the health and performance of an application running within a container. By regularly inspecting the logs, users can track important events, identify bottlenecks, monitor resource usage, and detect anomalies that may impact the application’s performance or stability.
- Integration with Logging Systems: Docker logs can be redirected and integrated with external logging systems or tools. By piping the output of the “docker logs” command to another process or tool, users can centralize and aggregate container logs, perform advanced log analysis, and gain insights across multiple containers or even an entire Docker environment.
It’s important to note that the “docker logs” command only displays the logs that have been generated since the container started running. If a container has already stopped, you won’t be able to retrieve its logs using this command. In such cases, you can use the “–since” or “–until” flags to specify a time range for log retrieval.
docker logs Command Examples
1. Print logs from a container:
# docker logs container_name
2. Print logs and follow them:
# docker logs -f container_name
3. Print last 5 lines:
# docker logs container_name --tail 5
4. Print logs and append them with timestamps:
# docker logs -t container_name
5. Print logs from a certain point in time of container execution (i.e. 23m, 10s, 2013-01-02T13:23:37):
# docker logs container_name --until time
Summary
In summary, the “docker logs” command is a valuable tool for retrieving and printing the logs generated by a Docker container. It enables users to monitor container behavior, troubleshoot issues, debug applications, and integrate with external logging systems. By analyzing container logs, users can gain insights into the container’s execution, performance, and overall health.