在通常的Linux服务器中,有一些服务本身没有日志,只能通过 tail -f /var/log/messages来查看其运行日志,比如nrpe server。但是,如果想在docker容器中实现这个功能就需要费点事了。具体步骤如下: 1、安装rsyslog # yum -y install rsyslog
2、启动syslog服务 # rsyslogd
# logger "Hello World"
解决办法如下: 1、修改syslog配置—— # vi /etc/rsyslog.conf 1、注释掉 $ModLoad imjournal 2、设置 $OmitLocalLogging 为 off 3、注释掉 $IMJournalStateFile imjournal.state
2、提交容器至镜像,重新创建容器 [root@localhost home]# docker commit f2570fed2149 centos_with_syslog:v1.0 830e298b4c103f70154ec9c8ceae44ec4602ed22063dae8e13a3f9aff4c3260a [root@localhost home]# docker run -d -p 223:222 -v /dev/log:/dev/log -ti centos_with_syslog:v1.0 /run.sh dc2eb83e80ced6f553eb1dd7032489d37b4d934a9126f61dfbd4d6a2243e98f1
[root@localhost home]# docker exec -ti dc2eb83e80ced6f553eb1dd7032489d37b4d934a9126f61dfbd4d6a2243e98f1 /bin/bash [root@dc2eb83e80ce /]# logger "hello everyone" [root@dc2eb83e80ce /]# exit exit [root@localhost home]# tail -f /var/log/messages (注意实在宿主机上的messages文件中查看日志) Aug 1 19:57:51 localhost docker: time="2015-08-01T19:57:51+08:00" level=info msg="-job log(exec_start: /bin/bash , dc2eb83e80ced6f553eb1dd7032489d37b4d934a9126f61dfbd4d6a2243e98f1, centos_with_syslog:v1.0) = OK (0)" Aug 1 19:57:51 localhost docker: time="2015-08-01T19:57:51+08:00" level=info msg="POST /v1.18/exec/9ae97d0666bf5abcd8d95eb87d10c5d013a0f006c2cb1adc7ff8f7baaacf14fc/resize?h=37&w=191" Aug 1 19:57:51 localhost docker: time="2015-08-01T19:57:51+08:00" level=info msg="+job execResize(9ae97d0666bf5abcd8d95eb87d10c5d013a0f006c2cb1adc7ff8f7baaacf14fc, 37, 191)" Aug 1 19:57:51 localhost docker: time="2015-08-01T19:57:51+08:00" level=info msg="-job execResize(9ae97d0666bf5abcd8d95eb87d10c5d013a0f006c2cb1adc7ff8f7baaacf14fc, 37, 191) = OK (0)" Aug 1 19:58:09 localhost logger: hello everyone (责任编辑:IT) |