CuriousTechie
CuriousTechie

Reputation: 421

Logging from bash in Docker: logger error: socket /dev/log: No such file or directory

I am trying to configure logger to my script

#! /bin/bash

alias log="logger -s -t conf_nginx"
exec >> /var/log/myscript/file.log
exec 2>&1

log "this a log message"

when I am executing this script, I only see this line in the log file

$ cat /var/log/myscript/file.log  
logger: socket /dev/log: No such file or directory

Please help

$cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 9 (stretch)" NAME="Debian GNU/Linux" VERSION_ID="9" VERSION="9 (stretch)" ID=debian HOME_URL="debian.org/" SUPPORT_URL="debian.org/support" BUG_REPORT_URL="bugs.debian.org/"

Upvotes: 2

Views: 7563

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295736

On Docker

Because a Docker container has its own filesystem namespace, the /dev/log socket from the host won't be available within -- and because best-practice in the Docker world is to have each container only running a single service, there generally won't exist a separate log daemon inside the local container.

Best practice is to just log to the container's stdout and stderr. Thus, instead of running logger -s -t conf_nginx "this is a log message", just echo "this is a log message" >&2.

On Shell Aliases

Aliases are a facility intended for interactive use, not scripting, and are in some shells turned off by default in interactive mode. Use a function instead:

log() { logger -s -t conf_nginx "$@"; }

Upvotes: 2

Related Questions