简体   繁体   English

bash 脚本中的 docker 日志不起作用

[英]docker logs within a bash script doesn't work

I'm experimenting a weird behaviour of Docker in a bash script.我正在 bash 脚本中尝试 Docker 的奇怪行为。 Let's see these two examples:让我们看看这两个例子:

logs-are-showed() {
     docker rm -f mybash &>/dev/null
     docker run -it --rm -d --name mybash bash -c "echo hello; tail -f /dev/null"
     docker logs mybash
}

# usage:
# $ localtunnel 8080
localtunnel() {
     docker rm -f localtunnel &>/dev/null
     docker run -it -d --network host --name localtunnel efrecon/localtunnel --port $1
     docker logs localtunnel
}

In the first function logs-are-showed the command docker logs returns me the logs of the container mybash在第一个 function日志中显示命令docker logs返回我容器mybash的日志

In the second function localtunnel the command docker logs doesn't return me anything.在第二个 function localtunnel命令docker logs没有返回任何内容。 After having called the localtunnel function, if I ask for the container logs from outside the script , it shows me the logs correctly.调用localtunnel function 后,如果我从脚本外部询问容器日志,它会正确显示日志。

Why does this happen?为什么会这样?

Processes take time to react.过程需要时间做出反应。 They may be no logs right after starting a process - it has not written anything yet .在启动进程后,它们可能没有日志——它还没有写任何东西。 Wait a bit.稍等一会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM