
How to use docker logs

When you're working with Docker containers, it's really handy to be able to check out their logs. This helps you see what's going on inside the container, whether it's running smoothly or if there's something you need to fix. The docker logs
command is your go-to tool for this job.
Let's start with the basics. If you just want to see all the logs for a container, you can use the command like this:
docker logs my_container
Here, my_container
is the name or ID of your container. This command will show you all the logs that the container has generated up to the moment you run the command.
Now, let's dive into some options that can make docker logs
even more useful for you.
Following the Logs in Real-Time
Sometimes, you want to keep an eye on the logs as they're being generated. This is super helpful when you're debugging or monitoring a running container. You can do this by adding the --follow
option (or its shorter version, -f
):
docker logs -f my_container
This command will start showing you the logs and keep updating with new log entries as they come in. It's like watching a live feed of what's happening inside your container.
Showing Only Recent Logs
If you're only interested in the most recent log entries, you can use the --tail
option. For example, to see the last 100 lines of the logs, you would run:
docker logs --tail 100 my_container
If you don't specify a number, it defaults to showing all logs. Just a heads up, if you try to use a negative number or something that's not a whole number, Docker will show you all the logs instead.
Adding Timestamps to Your Logs
To make it easier to track when things happened, you can add timestamps to your logs with the --timestamps
option (or -t
for short):
docker logs -t my_container
This will add a timestamp in the RFC3339Nano format to each log entry, like this: 2014-09-16T06:17:46.000000000Z
. It helps you see exactly when each log message was created.
Including Extra Details
If you need more context about your logs, the --details
option can be really useful. It adds extra information like environment variables and labels that were set when the container was created:
docker logs --details my_container
Filtering Logs by Time
You might want to look at logs from a specific time period. The --since
option lets you do that. For example, to see logs from the last 30 minutes, you would use:
docker logs --since 30m my_container
You can also specify an exact date and time, like 2024-05-01T12:00:00Z
, or use other time formats.
Similarly, if you want to see logs up to a certain point in time, you can use the --until
option. For example, to see logs up to 30 minutes ago, you would run:
docker logs --until 30m my_container
Just like with --since
, you can use different time formats with --until
.
These are some of the main ways you can use docker logs
to keep an eye on what's happening in your containers. Whether you're debugging, monitoring, or just curious, these commands give you the flexibility to see exactly what you need to see.