Docker logs: read and follow application output
Read application output
docker logs lets you review output that Docker has retained from a container. It is often the first useful check when a program fails or appears idle. You can inspect logs from a stopped container as long as the container still exists and its logging configuration supports retrieval.
Lab prerequisites
Use an approved disposable Linux-container lab only. Verify the active context (docker context show), CLI/daemon versions and that the instructor has reviewed and preloaded the BusyBox 1.37 fixture for the target platform; record its digest before use. The tag is inherited from earlier chapters, not a verified immutable identity. --pull=never avoids automatic downloads; --network none isolates normal network access for these local-only tasks. Verify each bm-v04 name is unused; if not, choose a fresh name consistently. Run host commands one at a time in Bash, stop on unexpected errors, and clean up only the container you created. If a fixture times out, inspect its state before retrying; remove only your stopped fixture. All snippets are static, not executed by ByteMind. Before any engine request, confirm the effective approved daemon endpoint, including DOCKER_HOST, DOCKER_CONTEXT and CLI overrides; do this privately in each terminal. Confirm the local tag still resolves to the approved image identity and platform. If any check fails, use the paper exercise instead. The network flag limits container networking, not the client-to-daemon connection. Use a lab with readable, local-only logging and an owner-set execution time limit; confirm your fixture identity before every cleanup.
Generate three known messages
Run the following in order. The short-lived program writes three lines and exits; no service or public network is needed. The first logs request should show boot, ready, done in that order. The second should show only ready and done. The timestamps in the third request depend on the actual run.
docker run --pull=never --network none --name bm-v04-logs busybox:1.37 printf 'boot\nready\ndone\n'docker logs bm-v04-logsdocker logs --tail 2 bm-v04-logsdocker logs --timestamps bm-v04-logsdocker rm bm-v04-logsSnapshots versus following
The default request finishes after retrieving available messages. --tail N limits how many recent lines you see; --since 5m narrows the time range. --follow keeps watching new messages for a running program. To try following, use a second terminal with the lab fixture below; interrupt the logs client with Ctrl+C and then use the first terminal for cleanup. The app emits tick about every two seconds, not on a guaranteed schedule. The loop finishes on its own after about 20 seconds, so following may finish before you press Ctrl+C. If already exited, skip stop and remove only the named fixture.
docker run -d --pull=never --network none --name bm-v04-follow busybox:1.37 sh -c 'for n in 1 2 3 4 5 6 7 8 9 10; do echo tick; sleep 2; done'docker logs --follow --tail 2 bm-v04-followdocker container inspect --format '{{.State.Status}}' bm-v04-follow# Stop only if still running; then remove your exited fixture.docker stop bm-v04-followdocker rm bm-v04-followCommon mistakes
A log request is not a search of every file inside the container. A program that writes only to its own file may produce no visible Docker log messages. Retention and logging drivers also matter. Do not remove a failing container until you have collected the relevant evidence; do not share logs containing credentials or personal information. For a remote logging driver, Docker may maintain a local dual-logging cache. When that cache is unavailable or disabled, retrieval can be limited or fail; rotation can discard older records. Do not change daemon logging settings to force this exercise.
Manual practice
Repeat the first fixture with a fresh lab name and four synthetic messages. Predict which lines --tail 1 and --tail 3 will display; compare the output, then remove only your stopped fixture.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Docker logs: read and follow application output — Knowledge check
Apply the concepts from this lesson.
A retrievable retained log contains three lines A, B, C in order. What does --tail 1 request?
The app writes only to its own file. Why could docker logs be empty?
What should you do before removing a failed container?