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

复制代码
相关推荐
byoass1 分钟前
企业云盘高可用架构:主备切换、负载均衡与健康检查实战
运维·网络·安全·架构·云计算·负载均衡
白菜欣10 分钟前
Linux —进程概念
linux·运维·服务器
iuu_star13 分钟前
Vue+FastAPI 项目宝塔Linux部署指南
linux·运维·fastapi
杜哥无敌15 分钟前
FreeSSHd vs FileZilla Server vs SFTPGo:Windows SFTP服务器易用性终极横向测评
运维·服务器·windows
IMPYLH22 分钟前
Linux 的 tail 命令
linux·运维·服务器·bash
生成论实验室22 分钟前
《事件关系阴阳博弈动力学:识势应势之道》第五篇:安全关键关系——故障、障碍与冲突
运维·服务器·人工智能·安全·架构
BduL OWED28 分钟前
Docker:基于自制openjdk8镜像 or 官方openjdk8镜像,制作tomcat镜像
docker·容器·tomcat
.柒宇.36 分钟前
AI掘金头条项目 Docker Compose 部署完整教程(附踩坑记录)
运维·后端·python·docker·容器·fastapi
Nightwish536 分钟前
Linux随记(三十)
linux·运维·mysql·ambari
cui_ruicheng42 分钟前
Linux信号机制(一):从概念到产生与处理
linux·运维·服务器