I have an app writing logs to systemd. I would like to run this app in a Docker container and access the logs from host journal system.
The answer is given here but does not work in my case for a unknown reason: https://stackoverflow.com/a/45701983/2291710
Given the following C++ program:
#include <systemd/sd-journal.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
return sd_journal_send("MESSAGE=Hello World!", "PRIORITY=5", NULL);
}
I create a new container and open bash
while binding /run/systemd/journal/socket
:
docker run -v /run/systemd/journal/socket:/run/systemd/journal/socket -v $PWD:/test -it -w /test ubuntu:20.04 bash
Then install the dependencies and compile my file from within the container:
apt-get update && apt-get install -y g++ strace libsystemd-dev
g++ main.cpp -o out -lsystemd
But then when I run ./out
nothing show up while I opened journalctl --user -f
on host. However, if I run ./out
from host, then the expected message is displayed by journalctl
.
Running strace
on ./out
from the container or the host demonstrates they are both writing to /run/systemd/journal/socket
.
Is there any reason for which it would not work?
Socket seen from host:
$ stat /run/systemd/journal/socket
File: /run/systemd/journal/socket
Size: 0 Blocks: 0 IO Block: 4096 socket
Device: 19h/25d Inode: 506 Links: 1
Access: (0666/srw-rw-rw-) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-03-23 13:29:18.401063839 +0100
Modify: 2022-03-21 13:00:06.487999531 +0100
Change: 2022-03-21 13:00:06.487999531 +0100
Birth: -
Socket seen from container:
# stat /run/systemd/journal/socket
File: /run/systemd/journal/socket
Size: 0 Blocks: 0 IO Block: 4096 socket
Device: 19h/25d Inode: 506 Links: 1
Access: (0666/srw-rw-rw-) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-03-23 12:29:18.401063839 +0000
Modify: 2022-03-21 12:00:06.487999531 +0000
Change: 2022-03-21 12:00:06.487999531 +0000
Birth: -
Using Docker 20.10.12 and not running SELinux.
Using /run/systemd/journal/
did not work either.