将容器的日志记录到 Linux 日志系统
1.将容器的日志记录到 syslog
打开一个终端监控系统日志
[root@host1 ~]# tail -f /var/log/messages
Sep 16 16:15:46 host1 systemd[2615]: Reached target Sockets.
Sep 16 16:15:46 host1 systemd[2615]: Finished Create User's Volatile Files and Directories.
Sep 16 16:15:46 host1 systemd[2615]: Reached target Basic System.
Sep 16 16:15:46 host1 systemd[2615]: Reached target Main User Target.
Sep 16 16:15:46 host1 systemd[2615]: Startup finished in 305ms.
打开另一个终端将 Redis 容器的日志记录到 syslog 系统中
[root@host1 ~]# docker run --rm -d --log-driver syslog --name redis redis
06fe4942e07954d229c6305504568d7bd877b8f9840d06d0bfcc47279cd7be46
则第一个终端窗口可以看到
[root@host1 ~]# tail -f /var/log/messages
Sep 16 16:15:46 host1 systemd[2615]: Reached target Sockets.
Sep 16 16:15:46 host1 systemd[2615]: Finished Create User's Volatile Files and Directories.
Sep 16 16:15:46 host1 systemd[2615]: Reached target Basic System.
Sep 16 16:15:46 host1 systemd[2615]: Reached target Main User Target.
Sep 16 16:15:46 host1 systemd[2615]: Startup finished in 305ms.
Sep 16 16:15:46 host1 systemd[1]: Started User Manager for UID 0.
Sep 16 16:15:46 host1 systemd[1]: Started Session 2 of User root.
Sep 16 16:15:46 host1 systemd[1]: Starting Hostname Service...
Sep 16 16:15:46 host1 systemd[1]: Started Hostname Service.
Sep 16 16:16:16 host1 systemd[1]: systemd-hostnamed.service: Deactivated successfully.
Sep 16 16:16:43 host1 systemd-logind[1049]: New session 4 of user root.
Sep 16 16:16:43 host1 systemd[1]: Started Session 4 of User root.
Sep 16 16:16:43 host1 systemd[1]: Starting Hostname Service...
Sep 16 16:16:43 host1 systemd[1]: Started Hostname Service.
Sep 16 16:17:13 host1 systemd[1]: systemd-hostnamed.service: Deactivated successfully.
Sep 16 16:17:32 host1 systemd[1]: var-lib-docker-overlay2-0f61ea22555f2081fae87df1eddb833720f67eabba2e00cda4e8af49b8c68eb4\x2dinit-merged.mount: Deactivated successfully.
Sep 16 16:17:32 host1 systemd[1]: var-lib-docker-overlay2-0f61ea22555f2081fae87df1eddb833720f67eabba2e00cda4e8af49b8c68eb4-merged.mount: Deactivated successfully.
Sep 16 16:17:32 host1 dockerd[1415]: time="2025-09-16T16:17:32.963061344+08:00" level=info msg="Configured log driver does not support reads, enabling local file cache for container logs" container=06fe4942e07954d229c6305504568d7bd877b8f9840d06d0bfcc47279cd7be46 driver=syslog
Sep 16 16:17:33 host1 systemd[1]: Started libcontainer container 06fe4942e07954d229c6305504568d7bd877b8f9840d06d0bfcc47279cd7be46.
Sep 16 16:17:33 host1 kernel: docker0: port 2(vethd63bee6) entered blocking state
Sep 16 16:17:33 host1 kernel: docker0: port 2(vethd63bee6) entered disabled state
Sep 16 16:17:33 host1 kernel: vethd63bee6: entered allmulticast mode
Sep 16 16:17:33 host1 kernel: vethd63bee6: entered promiscuous mode
Sep 16 16:17:33 host1 NetworkManager[1037]: <info> [1758010653.3664] manager: (vethd63bee6): new Veth device (/org/freedesktop/NetworkManager/Devices/5)
Sep 16 16:17:33 host1 kernel: eth0: renamed from veth53feb03
Sep 16 16:17:33 host1 kernel: docker0: port 2(vethd63bee6) entered blocking state
Sep 16 16:17:33 host1 kernel: docker0: port 2(vethd63bee6) entered forwarding state
Sep 16 16:17:33 host1 NetworkManager[1037]: <info> [1758010653.4130] device (vethd63bee6): carrier: link connected
Sep 16 16:17:33 host1 06fe4942e079[1415]: Starting Redis Server
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:C 16 Sep 2025 08:17:33.773 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:C 16 Sep 2025 08:17:33.774 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:C 16 Sep 2025 08:17:33.774 * Redis version=8.2.1, bits=64, commit=00000000, modified=1, pid=1, just started
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:C 16 Sep 2025 08:17:33.774 * Configuration loaded
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.774 * monotonic clock: POSIX clock_gettime
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.779 * Running mode=standalone, port=6379.
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * <bf> RedisBloom version 8.2.0 (Git=unknown)
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * <bf> Registering configuration options: [
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * <bf> #011{ bf-error-rate : 0.01 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * <bf> #011{ bf-initial-size : 100 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * <bf> #011{ bf-expansion-factor : 2 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * <bf> #011{ cf-bucket-size : 2 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * <bf> #011{ cf-initial-size : 1024 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * <bf> #011{ cf-max-iterations : 20 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * <bf> #011{ cf-expansion-factor : 1 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * <bf> #011{ cf-max-expansions : 32 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * <bf> ]
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.785 * Module 'bf' loaded from /usr/local/lib/redis/modules//redisbloom.so
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.828 * <search> Redis version found by RedisSearch : 8.2.1 - oss
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.828 * <search> RediSearch version 8.2.1 (Git=dba8dd0)
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.830 * <search> Low level api version 1 initialized successfully
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.832 * <search> gc: ON, prefix min length: 2, min word length to stem: 4, prefix max expansions: 200, query timeout (ms): 500, timeout policy: return, cursor read size: 1000, cursor max idle (ms): 300000, max doctable size: 1000000, max number of search results: 1000000,
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.835 * <search> Initialized thread pools!
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.835 * <search> Disabled workers threadpool of size 0
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.838 * <search> Subscribe to config changes
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.838 * <search> Enabled role change notification
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.839 * <search> Cluster configuration: AUTO partitions, type: 0, coordinator timeout: 0ms
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.840 * <search> Register write commands
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.840 * Module 'search' loaded from /usr/local/lib/redis/modules//redisearch.so
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> RedisTimeSeries version 80200, git_sha=1439d4a439ca9c063e6ef124a510abff09a5d493
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> Redis version found by RedisTimeSeries : 8.2.1 - oss
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> Registering configuration options: [
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> #011{ ts-compaction-policy : }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> #011{ ts-num-threads : 3 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> #011{ ts-retention-policy : 0 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> #011{ ts-duplicate-policy : block }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> #011{ ts-chunk-size-bytes : 4096 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> #011{ ts-encoding : compressed }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> #011{ ts-ignore-max-time-diff: 0 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> #011{ ts-ignore-max-val-diff : 0.000000 }
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.845 * <timeseries> ]
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.846 * <timeseries> Detected redis oss
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.848 * <timeseries> Enabled diskless replication
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.848 * Module 'timeseries' loaded from /usr/local/lib/redis/modules//redistimeseries.so
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.858 * <ReJSON> Created new data type 'ReJSON-RL'
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.861 * <ReJSON> version: 80200 git sha: unknown branch: unknown
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.862 * <ReJSON> Exported RedisJSON_V1 API
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.862 * <ReJSON> Exported RedisJSON_V2 API
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.862 * <ReJSON> Exported RedisJSON_V3 API
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.862 * <ReJSON> Exported RedisJSON_V4 API
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.862 * <ReJSON> Exported RedisJSON_V5 API
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.862 * <ReJSON> Enabled diskless replication
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.862 * <ReJSON> Initialized shared string cache, thread safe: false.
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.862 * Module 'ReJSON' loaded from /usr/local/lib/redis/modules//rejson.so
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.862 * <search> Acquired RedisJSON_V5 API
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.864 * Server initialized
Sep 16 16:17:33 host1 06fe4942e079[1415]: 1:M 16 Sep 2025 08:17:33.865 * Ready to accept connections tcp
Sep 16 16:17:34 host1 avahi-daemon[1038]: Joining mDNS multicast group on interface vethd63bee6.IPv6 with address fe80::8094:f9ff:fe3c:52ab.
Sep 16 16:17:34 host1 avahi-daemon[1038]: New relevant interface vethd63bee6.IPv6 for mDNS.
Sep 16 16:17:34 host1 avahi-daemon[1038]: Registering new address record for fe80::8094:f9ff:fe3c:52ab on vethd63bee6.*.
^C
[root@host1 ~]#
退出(停止)容器
[root@host1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fe4942e079 redis "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 6379/tcp redis
96632885a463 lagoudocker/cadvisor:v0.37.0 "/usr/bin/cadvisor -…" 17 hours ago Exited (0) 17 hours ago cadvisor-stdout
62b9981bf08e php-nginx-supervisor:v1 "/usr/bin/supervisor…" 30 hours ago Exited (255) 26 hours ago 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp php-web-app
7ecac05c7dac centos-with-nginx:1.0 "nginx -g 'daemon of…" 5 days ago Exited (255) 4 days ago 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp test-nginx
0dbf7ea2178e registry "/entrypoint.sh /etc…" 6 days ago Up 11 minutes 0.0.0.0:5000->5000/tcp, [::]:5000->5000/tcp myregistry
c8efe28b118e busybox "sh" 6 days ago Exited (137) 6 days ago stoic_raman
65a371a8cacb httpd "httpd-foreground" 6 days ago Exited (0) 6 days ago testweb
7ab5ac9bc007 ubuntu "/bin/bash" 6 days ago Exited (0) 6 days ago inspiring_joliot
b97f426e8369 ubuntu "/bin/echo" 6 days ago Exited (0) 6 days ago friendly_chaplygin
9ac8245b5b08 img-layers-test "python /app/app.py" 8 days ago Exited (0) 8 days ago crazy_kowalevski
49e91767b48d ubuntu "/bin/bash" 8 days ago Exited (0) 8 days ago frosty_lichterman
38b001022f7a hello-world "/hello" 8 days ago Exited (0) 8 days ago intelligent_brahmagupta
[root@host1 ~]# docker stop redis
redis
[root@host1 ~]# docker stop registry
Error response from daemon: No such container: registry
[root@host1 ~]# docker stop 0dbf7ea2178e
0dbf7ea2178e
2.将容器的日志记录到 journald
选择 journald 作为日志驱动可将日志定向输出到 sysemd 日志系统中
[root@host1 ~]# docker run --rm -d --log-driver journald --name redis redis
9dd80a7a4e596945fa557dc2755f49da02028c5f310a8986aa8f6c61d0bd16b9
使用 journalctl 命令查看该容器的日志信息
[root@host1 ~]# journalctl CONTAINER_NAME=redis
9月 16 16:31:25 host1 9dd80a7a4e59[1415]: Starting Redis Server
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:C 16 Sep 2025 08:31:26.018 # WARNING Memory overcommit m>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:C 16 Sep 2025 08:31:26.018 * oO0OoO0OoO0Oo Redis is star>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:C 16 Sep 2025 08:31:26.018 * Redis version=8.2.1, bits=6>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:C 16 Sep 2025 08:31:26.018 * Configuration loaded
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.019 * monotonic clock: POSIX cloc>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.021 * Running mode=standalone, po>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * <bf> RedisBloom version 8.2>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * <bf> Registering configurat>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * <bf> { bf-error-rat>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * <bf> { bf-initial-s>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * <bf> { bf-expansion>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * <bf> { cf-bucket-si>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * <bf> { cf-initial-s>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * <bf> { cf-max-itera>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * <bf> { cf-expansion>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * <bf> { cf-max-expan>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * <bf> ]
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.022 * Module 'bf' loaded from /us>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.028 * <search> Redis version foun>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.028 * <search> RediSearch version>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.029 * <search> Low level api vers>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.029 * <search> gc: ON, prefix min>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.029 * <search> Initialized thread>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.029 * <search> Disabled workers t>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.029 * <search> Subscribe to confi>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.029 * <search> Enabled role chang>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.030 * <search> Cluster configurat>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.030 * <search> Register write com>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.030 * Module 'search' loaded from>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.031 * <timeseries> RedisTimeSerie>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.031 * <timeseries> Redis version >
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.032 * <timeseries> Registering co>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.032 * <timeseries> { ts-c>
9月 16 16:31:26 host1 9dd80a7a4e59[1415]: 1:M 16 Sep 2025 08:31:26.032 * <timeseries> { ts-n>[root@host1 ~]#