Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker logs command

The docker logs command allows you to view the logs of a running or stopped Docker container. This can be helpful for troubleshooting issues, understanding the state of your applications, and monitoring container activity. In this tutorial, we'll go over the basics of using the docker logs command.

Syntax:

docker logs [OPTIONS] CONTAINER
  • OPTIONS: Additional options that can be used with the docker logs command.
  • CONTAINER: The name or ID of the container whose logs you want to view.

Options:

  • --follow or -f: Stream the logs in real-time. This will continuously display new logs as they are generated.
  • --since or --until: Show logs only since or until a specific timestamp (e.g., 2023-05-08T10:23:59). You can also use relative time, such as "10m" for the last 10 minutes or "1h" for the last hour.
  • --timestamps or -t: Show timestamps for each log entry.
  • --tail: Show the specified number of lines at the end of the logs. Use "all" to show all lines. By default, --tail is set to "all".

Examples:

  1. Display logs of a container:

    docker logs my_container
    

    Replace my_container with the name or ID of the container you want to view logs for.

  2. Stream logs in real-time:

    docker logs -f my_container
    
  3. Show logs since a specific timestamp:

    docker logs --since="2023-05-08T10:23:59" my_container
    
  4. Show logs from the last hour:

    docker logs --since="1h" my_container
    
  5. Display logs with timestamps:

    docker logs -t my_container
    
  6. Show the last 100 lines of logs:

    docker logs --tail=100 my_container
    

This should give you a basic understanding of how to use the docker logs command to view and monitor container logs. Remember that you can combine multiple options to tailor the output to your specific needs.

  1. How to Use Docker Logs Command:

    • Description: The docker logs command is used to view the logs generated by a running Docker container.

    • Code Example:

      docker logs <container_id_or_name>
      
  2. Viewing Container Logs with Docker Logs:

    • Description: Use docker logs to view the standard output and standard error logs of a running container.

    • Code Example:

      docker logs my_container
      
  3. Docker Logs Command Options and Flags:

    • Description: Customize log viewing with options such as --details, --since, and --follow.

    • Code Example:

      docker logs --details --since="2023-01-01" --follow my_container
      
  4. Real-time Logging with Docker Logs:

    • Description: Follow the real-time log output by using the --follow option with docker logs.

    • Code Example:

      docker logs --follow my_container
      
  5. Filtering Logs with Docker Logs Command:

    • Description: Filter logs based on criteria such as timestamps or log levels.

    • Code Example:

      docker logs --since="2023-01-01" --until="2023-01-31" my_container
      
  6. Docker Logs and Log Drivers:

    • Description: Consider log drivers configured for a container when using docker logs.

    • Code Example (Container with Log Driver):

      docker run --log-driver=syslog my_container
      docker logs my_container
      
  7. Formatting Output with Docker Logs:

    • Description: Format log output using options such as --timestamps and --format.

    • Code Example:

      docker logs --timestamps --format "{{.Time}} {{.ID}} {{.Message}}" my_container
      
  8. Redirecting Docker Logs to a File:

    • Description: Save container logs to a file using shell redirection.

    • Code Example:

      docker logs my_container > logs.txt
      
  9. Docker Logs and Container IDs:

    • Description: Identify containers by their IDs when using docker logs.

    • Code Example:

      docker logs my_container_id
      
  10. Security Considerations with Docker Logs:

    • Description: Exercise caution when viewing logs, as sensitive information may be present.

    • Code Example:

      docker logs my_sensitive_container
      
  11. Docker Logs and Multiple Containers:

    • Description: View logs for multiple containers by specifying their IDs or names.

    • Code Example:

      docker logs container1 container2
      
  12. Customizing Log Output in Docker:

    • Description: Customize log output using formatting options available with docker logs.

    • Code Example:

      docker logs --format "{{.Time}} | {{.Message}}" my_container
      
  13. Troubleshooting with Docker Logs:

    • Description: Troubleshoot container issues by examining logs for error messages or unexpected behavior.

    • Code Example:

      docker logs my_troubled_container
      
  14. Docker Logs vs Docker Events:

    • Description: docker logs displays container output, while docker events provides high-level events related to the Docker daemon.

    • Code Example (Events):

      docker events