docker容器日志

docker容器日志

文章目录

Docker logs

bash 复制代码
#我们在启动日志的时候没有用 `-d` 参数,httpd 容器以前台方式启动,日志会直接打印在当前的终端窗口。
[root@docker ~]# docker run -p 80:80 httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Tue Oct 08 14:28:10.312876 2025] [mpm_event:notice] [pid 1:tid 1] AH00489: Apache/2.4.62 (Unix) configured -- resuming normal operations
[Tue Oct 08 14:28:10.316058 2025] [core:notice] [pid 1:tid 1] AH00094: Command line: 'httpd -D FOREGROUND'

#如果加上 `-d` 参数以后台方式运行容器,我们就看不到输出的日志了。
[root@docker ~]# docker run -d -p 80:80 httpd
a8286845e6f8afc09fdcdf0b94248239963e3ad04495e927a68a2148814c65fd

两种方法查看容器日志

attach 到该容器

bash 复制代码
[root@docker ~]# docker attach a8286845e6f8

#可以在 host 的另一个命令行终端执行 `curl localhost`。

#[root@docker ~]# docker attach a8286845e6f8
172.17.0.1 - - [08/Oct/2025:14:30:24 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [08/Oct/2025:14:30:25 +0000] "GET / HTTP/1.1" 200 45

#只能看到 attach 之后的日志,以前的日志不可见。
#退出 attach 状态比较麻烦(Ctrl+p 然后 Ctrl+q 组合键),一不小心很容器将容器杀掉(比如按下 Ctrl+C)。

docker logs 命令查看日志

bash 复制代码
[root@docker ~]# docker logs a8286845e6f8
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
......

#`docker logs` 能够打印出自容器启动以来完整的日志,并且 `-f` 参数可以继续打印出新产生的日志,效果上与 Linux 命令 `tail -f` 一样。

综合实验构建WordPress

bash 复制代码
#下载镜像
[root@docker ~]# docker pull mysql
[root@docker ~]# docker pull wordpress

#创建mysql容器,并创建wordpress数据库
[root@docker ~]# docker run -d -p 3306:3306 \
-v /mysql:/var/lib/mysql:z \
--name mysql \
-e MYSQL_ROOT_PASSWORD=huawei \
-e MYSQL_DATABASE=wordpress \
mysql
6dd12147c9156055042587950e23d543cca47617c3a82bef14c58ffe90580301

[root@docker ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
6dd12147c915   mysql     "docker-entrypoint.s..."   15 seconds ago   Up 14 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

#创建WordPress容器
[root@docker ~]# docker inspect mysql|grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
[root@docker ~]# docker run -d -p 80:80 -v /www:/var/www/html:z \
--name wordpress \
-e WORDPRESS_DB_HOST=172.17.0.2 \
-e WORDPRESS_DB_USER=root \
-e WORDPRESS_DB_PASSWORD=huawei \
-e WORDPRESS_DB_NAME=wordpress \
wordpress
63fa3a61f9bae2a03f3c0c21b574cca75c9a58c5c2645a6f702179c63f5f06eb

#访问http://192.168.108.30

bae2a03f3c0c21b574cca75c9a58c5c2645a6f702179c63f5f06eb

#访问http://192.168.108.30

复制代码
相关推荐
悠悠1213819 小时前
Jenkins + Ansible 集成实战:把配置管理焊进流水线里
运维·ansible·jenkins
日取其半万世不竭19 小时前
用 n8n 搭建自己的自动化工作流平台
运维·自动化
IT界的老黄牛19 小时前
从 MQ 积压追到事件总线:诊断 4K 线程吃光 7G 内存的实战
java·运维·rocketmq
2501_9200470319 小时前
iptables防火墙
linux·运维·网络安全
Anthony_23120 小时前
Linux 防火墙完全指南:从 iptables 到 firewalld
linux·运维·服务器
月走乂山20 小时前
Linux 服务器安装 CC Switch GUI 工具 + VNC 远程桌面完整教程
linux·运维·服务器
前端 贾公子20 小时前
基于 Nginx 实现一个灰度上线系统
运维·nginx
万里侯20 小时前
Kubernetes成本优化:降低云原生基础设施成本的完整指南
微服务·容器·k8s
认真的薛薛20 小时前
Linux基础:GitOps发布流程
java·linux·运维
北风toto20 小时前
Jenkins新手入门安装插件全报错
java·运维·jenkins