Docker Logs
When containers are running in detached mode with the -d flag, you don’t see any output in your terminal. This is nice for keeping your terminal clean, but what if something goes wrong or you need to see what the application is doing?
Enter the docker logs command.
docker logs [OPTIONS] CONTAINER
(Note: CONTAINER can be either the container ID or the container name.)
Viewing Logs
If you run a container that generates output:
docker run -d --name logdate alpine sh -c 'while true; do echo "LOGGING: $(date)"; sleep 1; done'
You can view the logs like this:
docker logs logdate
If you run docker logs over and over again, you will get different output because you are getting the logs from the start up to the present moment.
Useful Flags
-
Follow Logs in Real-Time: Use the
-fflag to continuously stream the logs (similar totail -fin Linux).docker logs -f logdate(Press
Ctrl+Cto exit) -
Tail Recent Logs: If a container has been running for a long time, the log output can be huge. You can view only the most recent logs using the
--tailoption:docker logs --tail 5 logdate