[英]How to see the logs of a docker container
I have a simple code for which I have created a docker container and the status shows it running fine.我有一个简单的代码,我为它创建了一个 docker 容器,状态显示它运行良好。 Inside the code I have used some print()
commands to print the data.在代码中,我使用了一些print()
命令来打印数据。 I wanted to see that print command output.我想看看那个打印命令输出。
For this I have seen docker logs .为此,我看到了 docker logs 。 But it seems not to be working as it shows no logs.但它似乎不起作用,因为它没有显示日志。 How to check logs.?如何查看日志。?
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3b3fd261b94 myfirstdocker "python3 ./my_script…" 22 minutes ago Up 22 minutes elegant_darwin
$ sudo docker logs a3b3fd261b94
<shows nothing>
The first point you need to print your logs to stdout
.您需要将日志打印到stdout
的第一点。
To check docker logs just use the following command:要检查 docker 日志,只需使用以下命令:
docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--help Print usage
--since string Show logs since timestamp
--tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
Some example:一些例子:
docker logs --since=1h <container_id>
If there's not so much supposed output (eg script just tries to print few bytes), I'd suspect python is buffering it.如果没有那么多假设的输出(例如脚本只是尝试打印几个字节),我怀疑 python 正在缓冲它。
Try adding more data to the output to be sure that buffer is flushed, and also using PYTHONUNBUFFERED=1 (although, python3 still may do some buffering despite of this setting).尝试向输出添加更多数据以确保缓冲区被刷新,并使用 PYTHONUNBUFFERED=1(尽管如此设置,python3 仍然可能会做一些缓冲)。
Let's try using that docker create start and then logs command again and see what happens.让我们尝试使用 docker create start 然后再次记录命令,看看会发生什么。
sudo docker create busybox echo hi there sudo docker create busybox echo hi there
now I will take the ID and run a docker start and paste the ID that starts up the container it executes echo high there inside of it and then immediately exits.现在我将获取 ID 并运行 docker start 并粘贴启动容器的 ID,它在容器内部执行 echo high,然后立即退出。
Now I want to go back to that stopped container and get all the logs that have been emitted inside of it.现在我想回到那个停止的容器并获取其中发出的所有日志。 To do so I can run at docker logs and then paste the ID in and I will see that when the container had been running it had printed out the string Hi there .为此,我可以在 docker logs 中运行,然后将 ID 粘贴到其中,我将看到当容器运行时它已打印出字符串Hi there 。
One thing to be really clear about is that by running docker logs I am not re-running or restarting the container to in any way shape or form, I am just getting a record of all the logs that have been emitted from that container.需要真正明确的一件事是,通过运行 docker logs 我不会以任何方式重新运行或重新启动容器,我只是获得了从该容器发出的所有日志的记录。 docker logs container_id docker 记录 container_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.