Docker container logs are a crucial tool for monitoring and troubleshooting applications running in Docker containers. They provide detailed information about the behavior of the application, including any errors or warnings that occur during runtime. By examining container logs, developers and system administrators can quickly diagnose and resolve issues, such as performance problems or application crashes.
In this example, I’m using the static-site community image. You can get this from the docker-curriculum repo using the run command shown here.
docker run -d -P --name static-site prakhar1989/static-site
If you want to code along with me, you will need to get the port for your static site using the docker port command and specifying the static-site container.
docker port static-site
Open a browser and navigate to your localhost and port number.
I like to use VSCode to work with my Docker containers and if you install the extension, you can manage your containers, images, registries, networks, volumes, and contexts. To view the logs for a container, right click on the container name in the container browser and select View Logs.
If you look at the terminal window, you can see the docker logs command that was executed. You could alternatively execute this command in a git bash or PowerShell terminal.
docker logs --tail 1000 -f [container id]
The tail option sets the number of lines to show from the end of the logs. If you set the tail option to all then all lines will be shown. The “f” option means that it will follow log output and will continue streaming the new output from the container’s STDOUT and STDERR. The id at the end is the container id.
If you refresh the static site web page, your log should update since we used the follow option. The –details option will show extra details provided to logs which is useful if you are troubleshooting cryptic errors.
docker logs --tail 1000 -f --details [container id]
You can use the –since option to show logs since the specified timestamp. You can pass in a UTC (e.g. 2013-01-02T13:23:37Z) datetime as shown here or you could also use the relative format specifying minutes.
# UTC Format docker logs --tail 1000 -f --since 2023-04-01T01:00:00Z [container id] #Relative Format docker logs --tail 1000 -f --since 60 [container id]
Similarly, you can use the same time formats with the until option to show logs before a timestamp.
# UTC Format docker logs --tail 1000 -f --until 2023-04-01T01:00:00Z [container id] #Relative Format docker logs --tail 1000 -f --until 60 [container id]
And the –timestamps option will show timestamps in the log output.
docker logs --tail 1000 -f --timestamps [container id]
You can also view container logs in Docker Desktop. Click to expand any running container and you should see the same logs that we saw using the VSCode extension. And, if we refresh the static site again, we can verify that the log file is updated.
How To View Docker Logs – Online Course
If you’re struggling with Docker errors, check out my Pluralsight course, Tactics and Tools for Troubleshooting Docker. This course provides developers with the skills they need to troubleshoot and fix the most common Docker errors. By the end of this course, you will have the skills you need to reduce the time it takes you to assess and fix issues. Click the link below to get started with a free trial today!