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

复制代码
相关推荐
行者..................2 小时前
第1课:搭建 Linux 驱动与 Qt 开发基础环境
linux·运维·qt·mpsoc
草莓熊Lotso3 小时前
Linux IPC 进阶:System V 消息队列与信号量(含内核管理深度解析)
linux·运维·服务器·数据库·c++·人工智能·mysql
s1kSec4 小时前
天翼云openclaw钉钉配置解决,解决404、401报错
运维·服务器
以太浮标10 小时前
华为eNSP模拟器综合实验之- ACL控制列表核心命令全解析及场景应用
运维·网络·网络协议·华为·信息与通信
巨斧空间掌门10 小时前
JDK17 下载 windows Linux
linux·运维·服务器
小挪号底迪滴10 小时前
Docker容器化实战:从“在我机器上能跑“到环境一致性
运维·docker·容器
江畔何人初10 小时前
kube-apiserver、kube-proxy、Calico 关系
运维·服务器·网络·云原生·kubernetes
_下雨天.11 小时前
Nginx性能调优与深度监控
运维·nginx
皮卡蛋炒饭.11 小时前
进程得控制
linux·运维·服务器