Stephen Rasku
Stephen Rasku

Reputation: 2682

Can't use `systemd-cat` inside Docker container

I am trying to test logging to journald from inside my container. I want this to work inside a container since our build system runs inside a container. I need to test a script that uses journalctl and I need to generate output for it to parse. The test script needs to run in the container.

I am launching my container like this:

$ docker run --rm -it -v $(pwd):/home/edge -w /home/edge --log-driver=journald ubuntu /bin/bash 
root@78b56defde31:/home/edge# systemd-cat -t overflow ls /etc
Failed to create stream fd: No such file or directory

If I run the same command in the Debian host it works:

$ systemd-cat -t overflow ls /etc
$ sudo journalctl | grep overflow
Nov 01 08:09:53 nanode64 overflow[9037]: acpi
Nov 01 08:09:53 nanode64 overflow[9037]: adduser.conf
Nov 01 08:09:53 nanode64 overflow[9037]: adjtime
Nov 01 08:09:53 nanode64 overflow[9037]: aliases
Nov 01 08:09:53 nanode64 overflow[9037]: alternatives
Nov 01 08:09:53 nanode64 overflow[9037]: anacrontab
Nov 01 08:09:53 nanode64 overflow[9037]: apache2
Nov 01 08:09:53 nanode64 overflow[9037]: apg.conf
Nov 01 08:09:53 nanode64 overflow[9037]: apm
Nov 01 08:09:53 nanode64 overflow[9037]: apt
...

Upvotes: 0

Views: 1881

Answers (1)

larsks
larsks

Reputation: 312740

There is nothing running inside your container other than bash. There is no journald running inside the container, and thus no socket for systemd-cat to communicate with. I suspect that your attempt to use systemd-cat may stem from a misunderstanding of how the journald log driver works.

You're using Docker's journald logging driver, which means that anything output by your container to stdout/stderr will show up in your host journal. That is, if you were to run:

docker run --rm -v $(pwd):/home/edge -w /home/edge --log-driver=journald ubuntu echo this is a test

You would, on the host, see:

# journalctl -fl
...
Nov 01 11:45:08 lkellogg-pc0dzzve dockerd[3736]: this is a test
...

If you were to show that message using -o verbose you would see that it actually contains a variety of additional metadata, such as the container id and container name:

# journalctl -fl -o verbose MESSAGE='this is a test'
Wed 2017-11-01 11:48:02.002147 EDT [s=9a99c0d26c3548a2ba6a6416bc37094c;i=15c9cce1;b=3f2b2d45203c485f9fa4373148c81925;m=219ad310e0d;t=55cedcbef19b3;x=6f1641d7b8ebf7ee]
    PRIORITY=6
    _TRANSPORT=journal
    _UID=0
    _GID=0
    _SYSTEMD_SLICE=system.slice
    _BOOT_ID=3f2b2d45203c485f9fa4373148c81925
    _MACHINE_ID=229916fba5b54252ad4d08efbc581213
    _HOSTNAME=lkellogg-pc0dzzve
    _CAP_EFFECTIVE=3fffffffff
    _PID=3736
    _COMM=dockerd
    _EXE=/usr/bin/dockerd
    _CMDLINE=/usr/bin/dockerd -G docker --dns 172.23.254.1 --log-driver journald -s overlay2
    _SYSTEMD_CGROUP=/system.slice/docker.service
    _SYSTEMD_UNIT=docker.service
    _SELINUX_CONTEXT=system_u:system_r:unconfined_service_t:s0
    MESSAGE=this is a test
    CONTAINER_ID=5d20a635fcff
    CONTAINER_ID_FULL=5d20a635fcff5c57212a65d5d0ebd77f3fd9529a8fd4fe6b718238877ecf5173
    CONTAINER_NAME=optimistic_easley
    CONTAINER_TAG=5d20a635fcff
    _SOURCE_REALTIME_TIMESTAMP=1509551282002147

Upvotes: 3

Related Questions