docker容器日志
文章目录
- docker容器日志
-
- [Docker logs](#Docker logs)
- 两种方法查看容器日志
-
- [attach 到该容器](#attach 到该容器)
- [用 `docker logs` 命令查看日志](#用
docker logs命令查看日志)
- 综合实验构建WordPress
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