ARTICLE

Writing and Managing Application Logs with Docker

From Learn Docker in a Month of Lunches by Elton Stoneman

Welcome to stderr and stdout!

# run the container in the foreground:
docker container run diamol/ch15-timecheck:3.0

# exit the container with Ctrl-C when you're done
Figure 1. A container in the foreground takes over the terminal session until it exits
Figure 2. Docker watches the application process in the container and collects its output streams
# run a detached container
docker container run -d --name timecheck diamol/ch15-timecheck:3.0

# check the mosty recent log entry:
docker container logs --tail 1 timecheck

# stop the container and check the logs again:
docker container stop timecheck
docker container logs --tail 1 timecheck

# check where Docker stores the container log file:
docker container inspect --format='{{.LogPath}}' timecheck
Figure 3. Docker stores container logs in a JSON file and manages the lifetime of that file
{"log":"Environment: DEV; version: 3.0; time check: 09:42.56\r\n","stream":"stdout","time":"2019-12-19T09:42:56.814277Z"}
{"log":"Environment: DEV; version: 3.0; time check: 09:43.01\r\n","stream":"stdout","time":"2019-12-19T09:43:01.8162961Z"}
# run with log options and an app setting to write lots of logs:
docker container run -d --name timecheck2 --log-opt max-size=5k --log-opt max-file=3 -e Timer__IntervalSeconds=1 diamol/ch15-timecheck:3.0

# wait for a few minutes

# check the logs:
docker container inspect --format='{{.LogPath}}' timecheck2
Figure 4. Rolling log files let you keep a known amount of log data per container

Relaying logs from other sinks to stdout

# run a container from the new image:
docker container run -d --name timecheck3 diamol/ch19-timecheck:4.0

# check - there are no logs coming from stdout:
docker container logs timecheck3

# now connect to the running container, for Linux:
docker container exec -it timecheck3 sh

# OR windows containers:
docker container exec -it timecheck3 cmd

# and read the application log file:
cat /logs/timecheck.log
Figure 5. If the app doesn’t write anything to the output streams you won’t see any container logs
Figure 6. You need to package a utility in your container image to relay logs from a file
# app image
FROM diamol/dotnet-runtime AS base

WORKDIR /app
COPY --from=builder /out/ .
COPY --from=utility /out/ .

# windows
FROM base AS windows
CMD start /B dotnet TimeCheck.dll && dotnet Tail.dll /logs timecheck.log

# linux
FROM base AS linux
CMD dotnet TimeCheck.dll & dotnet Tail.dll /logs timecheck.log
# runa  container with the tail utility process:
docker container run -d --name timecheck4 diamol/ch19-timecheck:5.0

# check the logs:
docker container logs timecheck4

# and connect to the container - on Linux:
docker container exec -it timecheck4 sh

# OR with Windows containers:
docker container exec -it timecheck4 cmd

# check the log file:
cat /logs/timecheck.log
Figure 7. A log relay utility gets the application logs out to Docker, but using twice as much disk space

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Manning Publications

Follow Manning Publications on Medium for free content and exclusive discounts.