5. Configuring Logging for Containerised Applications Flashcards
What are application logs designed to do?
Report events that occur during the execution of a program.
How do software developers implement logging in their applications?
They code log writing within their source code at appropriate locations for reporting.
How are log messages provided to us and for what purpose?
They are provided as output during program execution for the purposes of debugging.
What can be said about the responsibilities of a twelve-factor app when it comes to writing and managing log files?
A twelve-factor app never concerns itself with routing or storage of its output system. It should not attempt to write or manage log files. Instead, each running process writes its event stream, unbuffered, to STDOUT.
What is a stream in the context of a process’s communications?
A communication channel between the process and its environment, in which data flows from one end of the stream to another.
This is how an application’s process receives its input and provides its output.
Where are twelve-factor applications are recommended to write their output stream to?
The standard out (STDOUT).
What is STDERR?
Standard Error is an output channel similar to Standard Out (STDOUT), but used separately to distinguish program output from any generated errors.
Sometimes, STDOUT and STDERR are combined.
Which streams of an application output does Docker capture and store for containers?
Both the Standard Out (STDOUT) and Standard Error (STDERR).
What can be said about writing logs directly to files from Docker containers?
This is considered a bad practice and must be avoided if we can. We must instead send the logs to STDOUT or STDERR.
If we have a legacy application that is configure to write its logs to a file and we instead want to send logs to a source, how can we get around this?
It’s possible to link the file where the app writes logs to a stream — a symbolic link is a Linux kernel mechanism for achieving this using the ln
command.
Dockerfile
RUN ln -sf /dev/stdout /var/log/nginx/access.log
What are the logging mechanisms provided by Docker?
- Pluggable system — implemented using pluggable drivers.
- Batteries included — inbuilt options for managing logs locally.
- Third-party vendors — popular logging solutions are available.
What are the three popular logging drivers for Docker containers?
- json-file — default driver that stores logs locally in JSON format (legacy/suboptimal).
- local — flexible and more performant file-based logging solution.
- journald — logs sent to journald service running on the Docker host.
How can we change the default logging driver for the Docker daemon?
It requires reconfiguration of the daemon, which can be achieved using its configuration file daemon.json
.
{ # ... "log-driver": "local", "log-opts": { "max-size": "10m", "max-file": "6" } # ... }
How can we run containers with an alternative logging setup than the one configured for the Docker daemon?
By using the --log-driver
option when running docker run
and providing it with the name of the driver.
~~~
docker run –name todo –log-driver local …
``
How can we inspect the logs of a container?
By running docker logs
and providing it with the name or ID of the container to be inspected.
docker logs todo
What can be said about the local availability of logs that are sent from a container to an external logging service, starting from Docker version 20.10?
That the versions hereafter requires to view logs locally when using any driver other than json-file
, local
, or journald
. This means that the logs sent to an external logging system will also be available locally for a developer while iterating over the inner loop of code development.
What command-line options are there to customise the log output when using docker logs
?
-
--details
— display additional info from tag. -
--follow
— follow the log output. -
--tail
— show last N lines of log output. -
--since
— show log output since a point in time. -
--until
— show log output to a point in time. -
--timestamps
— annotate logs with a timestamp.
How do we inspect the details about the details on the configuration of Docker daemon?
By using the docker info
command.
From the command-line, how can we find out the configured logging driver of the Docker daemon?
By executing `docker info -f “{{.LoggingDriver}}”
Where is the Docker daemon configuration file located on Linux, and where is it in MacOS?
LINUX Inside the `/etc/docker` directory.
/etc/docker/daemon.json
~~~
MAC
Inside the ~/.docker
directory.
~~~
~/.docker/daemon.json
How can you configure the Docker daemon to use a log driver other than the default one?
By adding or setting the value of the log-driver
entry in daemon.json
and restarting the docker service by running the following:
MAC
killall Docker && open /Applications/Docker.app
LINUX
sudo systemtcl restart docker.service
How do you run a container in the background and print the container ID?
By using the -detach
(or -d
) option for the docker run
command.
docker run -r //...
How do you assign a name to a container to run?
By using the --name
option for the docker run
command and passing it the name to assign.
docker run --name todo //...
How do you initiate a sort of “watch” on a running detached container’s logs to print logs in real time?
By running docker logs
with the -f
(or --follow
) option.
docker logs -f todo