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

复制代码
相关推荐
handsomestWei10 分钟前
Docker引擎API接入配置
运维·http·docker·容器·api
Tingjct15 分钟前
Linux常用指令
linux·运维·服务器
daad77740 分钟前
wifi_note
运维·服务器·数据库
IT界的老黄牛41 分钟前
Linux 压缩命令实战:tar、gzip、bzip2、xz、zstd 怎么选?一篇讲清楚
linux·运维·服务器
12345,catch a tiger1 小时前
虚拟机ubuntu安装Vmware Tools
linux·运维·ubuntu
不是书本的小明3 小时前
K8S应用优化方向
网络·容器·kubernetes
程序猿编码3 小时前
一个授予普通进程ROOT权限的Linux内核级后门:原理与实现深度解析
linux·运维·服务器·内核·root权限
小夏子_riotous3 小时前
openstack的使用——9. 密钥管理服务Barbican
linux·运维·服务器·系统架构·centos·云计算·openstack
梦想的旅途24 小时前
自动化运营如何防封?解析 API 协议下的拟人化风控算法
运维·自动化
AC赳赳老秦5 小时前
OpenClaw text-translate技能:多语言批量翻译,解决跨境工作沟通难题
大数据·运维·数据库·人工智能·python·deepseek·openclaw