Viewing Logs of Detached Containers
When you run a Docker container in detached mode (using the -d
flag), the container runs in the background, and you don't have direct access to its output. However, you can still view the logs of a detached container using the Docker CLI.
Viewing Logs of a Detached Container
To view the logs of a detached container, you can use the docker logs
command. The basic syntax is:
docker logs [container_name or container_id]
For example, let's say you have a detached container named my-app
:
docker run -d --name my-app nginx
You can view the logs of this container using the following command:
docker logs my-app
This will display the logs of the my-app
container in your terminal.
Viewing Logs in Real-time
If you want to view the logs of a detached container in real-time, you can use the -f
(follow) flag with the docker logs
command:
docker logs -f my-app
This will keep the log output open in your terminal, and it will continuously display new log entries as they are generated by the container.
Filtering Logs
You can also filter the logs by time or by specific log levels. For example, to view the last 10 log entries:
docker logs --tail 10 my-app
Or to view the logs from the last 5 minutes:
docker logs --since 5m my-app
You can also filter the logs by log level using the --until
flag:
docker logs --until 5m my-app
This will display the logs from the last 5 minutes.
Logging Drivers
Docker supports various logging drivers, which determine how the container logs are stored and managed. The default logging driver is json-file
, but you can configure a different logging driver for your Docker daemon or individual containers. This can be useful for integrating with external log management systems.