Home » Uncategorized » How to View Docker Container Logs

How to View Docker Container Logs

In Docker, you can view the logs of a running or stopped container using the docker logs command. This command retrieves the standard output (stdout) and standard error (stderr) logs from the container.

The following example shows how to view Docker container logs.

How to View Logs of a Running Docker Container

To view the logs of a running Docker container, we can use the docker logs command followed by the container ID or name.

We can use the docker ps -a command to get the list of all Docker containers (both running and stopped). It returns the container ID, name, command, and other details.

We can use either the container ID or name to retrieve the container’s IP address.

Suppose we want to view the logs of a container app-nginx, we can run the following command in the terminal:

docker logs app-nginx

Output: 👇️

Docker container logs
Docker container logs

In this example, the docker logs command retrieves and displays the logs from the app-nginx container.

How to Follow Logs in Real-Time

To follow the logs of a Docker container in real time, we can use the -f flag with the docker logs command.

Suppose we want to follow the logs of a container app-mqtt in real time, we can run the following command in the terminal:

docker logs -f app-mqtt
Docker container logs in real time
Docker container logs in real-time

This command continuously streams the logs from the app-mqtt container to the terminal.

How to View Specific Number of Log Lines

To view a specific number of log lines from the end of the logs, we can use the --tail flag with the docker logs command.

Suppose we want to view the last 10 lines of logs from a container named app-nginx, we can run the following command in the terminal:

docker logs --tail 10 app-nginx
Retrieve specific log details of Docker container
Retrieve specific log details of the Docker container

This command retrieves and displays the last 10 lines of logs from the app-nginx container.

Conclusion

We can view Docker container logs using the docker logs command.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.