Home >Operation and Maintenance >Docker >How to view docker startup log
Docker is a popular application containerization platform that speeds up building, deploying and managing applications. However, when running applications using Docker, we may encounter some unexpected problems, which requires looking at the Docker startup log to understand the root cause of the problem. So, how to view the Docker startup log?
Docker provides a built-in command docker logs, which can be used to view the standard output and standard error output in the container. To use this command, you can first check the ID or name of the running container, which can be viewed through the docker ps command:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8623f22d0b74 nginx "nginx -g 'daemon of…" 10 days ago Up 2 hours 80/tcp web
In the above output, the container ID is 8623f22d0b74 and the container name is web. You can use the docker logs command to view the log output within the container:
$ docker logs 8623f22d0b74 172.17.0.1 - - [02/May/2020:19:41:04 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" "-" 172.17.0.1 - - [02/May/2020:19:41:06 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://localhost/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" "-"
In the above output, we can see the access log of the NGINX server. This command can simply help us find the startup problem of the container.
In addition to using the docker logs command, we can also use the docker events command to view Docker startup events. This command can observe all events emitted by Docker and output corresponding information. For example, we can use the following command to view all events of a Docker run:
$ docker events ... 2020-05-02T19:44:07.842095081+00:00 container destroy 8623f22d0b746eee40b1568a78ffdf2f1555a9c5b557d8c5a5a5eaa32c7f1ede (image=nginx, name=web) 2020-05-02T19:44:08.273816081+00:00 network disconnect 6cbedee6e77d 03ac9adf39af42c8d651f0ed60073c92837e866aebb0c75bfcb3f3c9a701bcb7 (endpoint=03ac9adf39af42c8d651f0ed60073c92837e866aebb0c75bfcb3f3c9a701bcb7, name=bridge, type=bridge) 2020-05-02T19:44:09.260029423+00:00 network destroy 6cbedee6e77d (name=bridge)
In the above output, we can see the destruction events of the container, the disconnection event of the network, and the destruction event of the network. This command can help us understand other events of the Docker container.
All logs of a Docker container will be stored in the container's file system. Therefore, we can view them using regular file viewing tools such as cat, tail or grep. For example, if you want to view the system log of the container, you can use the following command:
$ docker inspect --format='{{.LogPath}}' 8623f22d0b74 /var/lib/docker/containers/8623f22d0b74cea5f8d76432734ec06c19683d698583cc59dd8f4af4bb70ac10/8623f22d0b74cea5f8d76432734ec06c19683d698583cc59dd8f4af4bb70ac10-json.log
This command will output the log file path of the container. We can continue to use the tail command to view the last few lines of the log file:
$ docker inspect --format='{{.LogPath}}' 8623f22d0b74 | xargs tail -f ...
In the above output, we can see the real-time log output of the container. You can use the Ctrl-C shortcut to stop viewing the logs.
Summary
Docker’s log management is a complex process, but by using the built-in commands provided by Docker, we can easily view problems occurring within the container. By using the docker logs command, docker events command or viewing the container log file, we can keep abreast of the running status of the Docker container, providing strong support for deploying and managing Docker applications.
The above is the detailed content of How to view docker startup log. For more information, please follow other related articles on the PHP Chinese website!