brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: journalctl CONTAINER_NAME= reads one Docker container's logs from the journal

Docker
~/tips/tip-journalctl-docker-container-name

sudo journalctl -b CONTAINER_NAME=webserver

Sends Docker container logs to the systemd journal and filters them by container name.

By default Docker keeps container logs in its own JSON files, separate from everything else on the machine. On a plain Debian or Ubuntu server I like having them in the systemd journal instead, next to the logs of the host. Docker’s journald log driver does that, and every line comes with fields you can filter on.

Set the driver for all new containers in /etc/docker/daemon.json and restart Docker, or give one container --log-driver=journald on docker run. Each line then carries CONTAINER_NAME, CONTAINER_ID, IMAGE_NAME and CONTAINER_TAG, and journalctl matches on those like on any other field:

# /etc/docker/daemon.json
{
  "log-driver": "journald"
}

sudo journalctl -b CONTAINER_NAME=webserver
sudo journalctl -o json CONTAINER_NAME=webserver

The first command shows what the webserver container printed since the last boot, the second gives the same lines as JSON with every field attached, so you can see which image and container ID they came from. The usual journalctl options such as -f and --since work on top. docker logs keeps working with this driver too.

A few catches. The daemon setting only applies to containers created after the restart, so recreate the ones you want to move. CONTAINER_NAME is the name at the time the container started; after a docker rename the journal keeps using the old one. If a container runs with a TTY you may see [10B blob data] instead of text, and adding --all shows it. And the logs now share the journal’s size limits, so a chatty container can push out older host logs.

Further reading: Journald logging driver (Docker docs), journalctl(1) manual page

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link