Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

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 a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

Hereโ€™s a step-by-step tutorial for Dockerfile, including explanations and examples for each major command. Dockerfile Tutorial A Dockerfile is a text file containing instructions to build a…

Read More

Docker Tutorials: Docker Image – Example and Sample Programs of Dockerfile

Reference Rajesh Kumar Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

Docker Tutorials: How to Install Docker in Ubuntu?

Install Docker Engine in Ubuntu NOTE – All commands you must run as root user or add a current user into a linux group name called “docker”…

Read More

Docker Lab, Excercise & Assignment – 7 – Docker Volume

Below is a very detailed tutorial and lab manual for learning Docker Volumes, using the Ubuntu image for practical, hands-on labs. This covers all major types of…

Read More

Docker Lab, Excercise & Assignment – 4 – Docker Networking

Hereโ€™s an in-depth, step-by-step tutorial and lab manual for Docker Networkingโ€”starting from basics, covering all core concepts, and providing a hands-on guide to every feature and command….

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