MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Docker commands Guide – docker logs with examples

Hereโ€™s a complete tutorial on docker logs, explaining what it does, a comprehensive list of examples, and use cases.


What is docker logs?

docker logs is a Docker command used to retrieve logs from a running or stopped container. It shows the stdout (standard output) and stderr (standard error) streams of the main process inside the container.

Key Features:

  • Works for both running and stopped containers.
  • Supports real-time log streaming (--follow).
  • You can filter logs by time or show recent logs (--since, --tail).
  • Helps in debugging and monitoring containerized services.

Basic Syntax

docker logs [OPTIONS] CONTAINER
Code language: CSS (css)

Common Options:

  • -f, --follow: Stream real-time logs.
  • --since: Show logs since a specific timestamp (--since 10m for last 10 minutes).
  • --tail: Show only the last n lines (--tail 50).
  • -t, --timestamps: Show timestamps for each log line.
  • --details: Show additional information provided by the container (if available).

Examples of docker logs

1. Show Logs from a Running Container

docker logs my_container

This displays all logs from my_container.


2. Follow Logs in Real-Time

docker logs -f my_container

This streams the logs in real-time. New log entries appear as they are written.


3. Show Logs with Timestamps

docker logs -t my_container

This displays logs with timestamps, useful for tracking when each event occurred.


4. Show the Last 50 Lines of Logs

docker logs --tail 50 my_container

This shows only the most recent 50 lines of logs.


5. Show Logs Since a Specific Time

docker logs --since "2023-02-07T12:00:00" my_container
Code language: CSS (css)

This displays logs generated after 12:00 PM on February 7, 2023.


6. Show Logs from the Last 10 Minutes

docker logs --since 10m my_container

This shows logs generated in the last 10 minutes.


7. Combine Options: Follow Logs with a Time Filter

docker logs -f --since 5m my_container

This streams real-time logs and includes logs from the last 5 minutes.


8. View Logs from a Stopped Container

docker logs my_stopped_container

You can still retrieve logs from a container even after it has stopped.


9. Use docker logs in a Script

#!/bin/bash
docker logs --tail 100 my_app > logs.txt
echo "Logs saved to logs.txt"
Code language: PHP (php)

This script saves the last 100 lines of logs from my_app to logs.txt.


10. Filter Logs from a Specific Service in Docker Compose

docker-compose logs my_service

This displays logs for a specific service (my_service) in a Docker Compose setup.


Use Cases for docker logs

1. Debugging Application Issues

  • View logs to identify errors or crashes.
  • Example: Check logs for a web server (nginx) to find why itโ€™s returning 500 errors.

2. Monitoring Real-Time Activity

  • Stream logs in real-time to monitor container activity.
  • Example: Watch real-time logs from a Node.js application to see incoming requests.

3. Tracking Application Performance

  • Use timestamps to analyze performance over time.
  • Example: Measure how long certain tasks take in a background worker container.

4. Security and Auditing

  • Review logs for security incidents or unauthorized access attempts.
  • Example: Check logs from a MySQL container for failed login attempts.

5. Log Analysis for Business Insights

  • Extract and analyze logs to gain insights into user behavior or system performance.
  • Example: Analyze web server logs to track page visits.

6. CI/CD Pipelines and Testing

  • Use logs to debug failed tests or builds in CI/CD pipelines.
  • Example: Check logs from a test runner container to identify why tests failed.

List of Common docker logs Commands

CommandDescription
docker logs my_containerShow all logs from the container
docker logs -f my_containerStream logs in real-time
docker logs -t my_containerShow logs with timestamps
docker logs --tail 100 my_containerShow the last 100 lines of logs
docker logs --since "2023-02-07T12:00:00" my_containerShow logs since a specific time
docker logs --since 10m my_containerShow logs from the last 10 minutes
docker logs -f --since 5m my_containerStream logs in real-time from the last 5 minutes
docker logs my_stopped_containerView logs from a stopped container

Best Practices for Using docker logs:

  1. Use --tail to avoid overwhelming output, especially for large logs.
  2. Stream logs in real-time (-f) when monitoring services.
  3. Combine with --since and --timestamps for accurate debugging.
  4. Centralize logs using tools like ELK (Elasticsearch, Logstash, Kibana) or Promtail/Loki/Grafana for better management and searchability.
  5. Rotate logs regularly to avoid disk space issues (log-opts can help).

Common Errors and Solutions

  1. “No such container”
    โ†’ Ensure the container exists and is running/stopped. Use docker ps -a to verify.
  2. “Container is not writing logs”
    โ†’ Check the containerโ€™s logging driver (docker inspect) to ensure itโ€™s set to json-file or another supported driver.
  3. Logs are too large
    โ†’ Use --tail or --since to filter logs and reduce output size. Consider enabling log rotation.

Combining docker logs with Other Commands

Check Logs and Restart a Container

docker logs my_app
docker restart my_app

Monitor Logs and Check Resource Usage

docker logs -f my_app &
docker stats my_app

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Debug for Docker Container

To debug a Docker container, check its state and logs first, then its network configuration, then test connectivity from another container on the same network. This page…

Read More

Datadog Tutorials: Tag Specifications

In a Datadog Agent log collection config, the tags field adds key:value tags to every log line the Agent collects from a source. You can then filter,…

Read More

Install Jenkins with Docker: Persistent Setup, Port Mapping, and Logs

Learn how to run Jenkins in Docker with correct port mapping, persistent storage, and operational commands. This updated guide keeps the original workflow but adds modern, production-minded practices.

Read More

How to Access a Running Docker Container Shell: docker exec vs docker attach

Need a shell inside a running container that is already in detached mode? This updated guide shows the correct workflow, compares docker exec and docker attach, and explains when SSH does and does not make sense.

Read More

Install Atlassian Jira with Docker: Quick Start, Persistent Volumes, and Deployment Checks

This guide shows how to run Jira in Docker using the original one-line command, then improve it with persistent storage and repeatable deployment steps. It also includes a workflow diagram, command examples, and practical validation checks.

Read More

Install Sonatype Nexus with Docker: RM2/RM3 Commands, Validation Steps, and Important Caveats

A structured, production-minded rewrite of the original 2016 Docker setup for Sonatype Nexus Repository Manager 2 and 3. Includes command examples, a workflow diagram, and clear warnings about outdated tags/endpoints.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x