How to View Docker Container Logs

Updated on 13 August, 2025
Learn how to view, follow, and filter logs for Docker containers and Docker Compose services with practical examples.
How to View Docker Container Logs header image

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.

The Short Answer

Here is a list of the most commonly used commands to view logs in Docker.

# View logs of a specific container
$ docker container logs [CONTAINER]

# View live logs in real-time
$ docker container logs --follow [CONTAINER]

# View the last 10 lines of logs
$ docker container logs --tail 10 [CONTAINER]

# View logs from Docker Compose services
$ docker compose logs [SERVICE]

# Follow all Docker Compose service logs
$ docker compose logs -f

Continue reading for detailed explanations and examples of each command.

View Container Logs

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

docker container logs [OPTIONS] [CONTAINER]
  • [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.

Command Options

Option(s) Description
-t, --timestamps Adds timestamps to each log entry.
--tail <number> Shows only the last N lines of logs.
--since <timestamp> Shows logs since a specific timestamp or relative time.
--until <timestamp> Shows logs until a specific timestamp or relative time.
-f, --follow Follows log output in real-time.

Command Demonstration

  1. Create and run a demo container based on the latest version of the nginx image.

    console
    $ docker run -d --name test-app nginx:latest
    

    This command creates a container test-app and starts it.

  2. Verify the container.

    console
    $ docker container ls
    

    You should see the test-app container listed in the output.

  3. View the logs of the container.

    console
    $ docker container logs test-app
    
  4. View logs with timestamps.

    console
    $ docker container logs --timestamps test-app
    
  5. View only the last 10 lines of logs.

    console
    $ docker container logs --tail 10 test-app
    
  6. View logs from the last 5 minutes.

    console
    $ docker container logs --since 5m test-app
    
  7. Follow the logs of the application container.

    console
    $ docker container logs --follow test-app
    

    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.

View Logs from Docker Compose

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

console
$ docker compose logs [OPTIONS] [SERVICE...]
  • [OPTIONS] modify the command behavior.
  • [SERVICE...] is the optional service name. If omitted, the command shows logs from all services.

Command Options

Option(s) Description
--tail <number> Show only the last N lines from each service.
--since <timestamp> Show logs since a specific time.
-f, --follow Follow log output in real-time.
-t, --timestamps Add timestamps to log entries.
--no-color Disable colored output.
--no-log-prefix Don't print service names before log lines.

Command Demonstration

  1. Create a docker-compose.yaml manifest to define a multi-container Docker compose application.

    console
    $ nano docker-compose.yaml
    
  2. Paste the following contents.

    yaml
    services:
      web:
        image: nginx:latest
        ports:
          - "8080:80"
        depends_on:
          - app
    
      app:
        image: node:16-alpine
        command: sh -c "while true; do echo 'App is running on $(date)'; sleep 3; done"
    
      database:
        image: postgres:13
        environment:
          POSTGRES_PASSWORD: secret
          POSTGRES_DB: myapp
    

    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.
  3. Start the services.

    console
    $ docker compose up -d
    
  4. Verify the created containers.

    console
    $ docker compose ps
    

    You should see a list of containers with the field SERVICE indicating its service name.

  5. View logs from all services.

    console
    $ docker compose logs
    
  6. View logs from only the web service.

    console
    $ docker compose logs web
    
  7. View logs from multiple services.

    console
    $ docker compose logs web app
    
  8. Follow logs from a specific service with limited history.

    console
    $ docker compose logs --follow --tail 10 app
    
  9. View logs with timestamps and follow mode.

    console
    $ docker compose logs --follow --timestamps
    
  10. Combine multiple options.

    console
    $ docker compose logs --follow --timestamps --tail 20 --since 5m web database
    

    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.

Conclusion

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.

Tags:

Comments

No comments yet.