Understanding container logging
- The container application does not connect to a STDOUT, which is why logs, by default, are written to the container
- Use
docker logs
mycontainer to get access to the container log - Using
docker logs
is convenient for troubleshooting
Demo
Output
controlplane $ docker run -d mariadb
5c2398dbcbfff2d400f4a3d910e32a3f8ab75171641fb60366e7f95ec259a214
controlplane $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c2398dbcbff mariadb "docker-entrypoint.s…" 6 seconds ago Exited (1) 4 seconds ago xander
As you can see the mariadb
container exited with status code 1 i.e., with error. To check for logs to inspect the error use docker logs
.
Output
controlplane $ docker logs xander
2024-10-18 22:18:28+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.5.2+maria~ubu2404 started.
2024-10-18 22:18:29+00:00 [Warn] [Entrypoint]: /sys/fs/cgroup/cpuset:/system.slice/docker-5c2398dbcbfff2d400f4a3d910e32a3f8ab75171641fb60366e7f95ec259a214.scope
2024-10-18 22:18:29+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2024-10-18 22:18:29+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.5.2+maria~ubu2404 started.
2024-10-18 22:18:29+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of MARIADB_ROOT_PASSWORD, MARIADB_ROOT_PASSWORD_HASH, MARIADB_ALLOW_EMPTY_ROOT_PASSWORD and MARIADB_RANDOM_ROOT_PASSWORD
In the log inspection we found that it is asking to provide one of the environment variable to run the container.
Output
controlplane $ docker run -d --name xander -e MARIADB_ROOT_PASSWORD=password mariadb
docker: Error response from daemon: Conflict. The container name "/xander" is already in use by container "dff2bf072092f8a2a19029dee93d99c1fbb8a0274828b8e4c8dbf34195c9597d". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
Now there is a conflict because container with same name is already present.
Output
controlplane $ docker rm xander
xander
Try again running the same command and this time it worked!!
Output
controlplane $ docker run -d --name xander -e MARIADB_ROOT_PASSWORD=password mariadb
aa39301ca6be70ed02b2e3ebf34e6b4da7330e1987791ba4c31715676ce5a23d
controlplane $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa39301ca6be mariadb "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 3306/tcp xander