3. 查看容器运行日志

使用 docker logs 命令可以查看容器的日志输出(容器主进程的标准输出和标准错误输出)。

命令格式

docker logs [选项] 容器名或容器ID

命令别名: docker container logs

常用选项

选项
说明
-t
显示时间戳信息。
-n <整数n>
显示末尾的 n 行信息。
-f
实时跟踪日志信息,不断开连接。

示例

查看 mysql_server 的日志信息的最后 5 行。

weimingze@mzstudio:~$ sudo docker logs  -n 5  mysql_server
2025-06-20T12:06:44.806865Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2025-06-20T12:06:44.807161Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2025-06-20T12:06:44.809979Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2025-06-20T12:06:44.856067Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '9.3.0'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2025-06-20T12:06:44.856406Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock