简体   繁体   English

如何使用 docker-compose run 查看日志输出?

[英]How to view log output using docker-compose run?

When I use docker-compose up I can see logs for all containers in my docker-compose.yml file.当我使用docker-compose up我可以在docker-compose.yml文件中看到所有容器的日志。

However, when I use docker-compose run app I only see console output for app but none of the services that app depends on.但是,当我使用docker-compose run app时,我只看到app的控制台输出,但没有看到app所依赖的服务。 How can see log output for the other services?如何查看其他服务的日志输出?

At the time of writing this the docker-compose run command does not provide a switch to see the logs of other services, hence you need to use the docker-compose logs command to see the logs you want.在撰写本文时, docker-compose run命令没有提供查看其他服务日志的开关,因此您需要使用docker-compose logs命令来查看所需的日志。

Update June 10th 2022 2022 年 6 月 10 日更新

As commented by @Sandburg docker compose is now integrated into docker.正如@Sandburg 所说,docker compose 现在已集成到 docker 中。 As described here most of the docker compose commands can be called the same way just without the dash :如此处所述大多数docker compose 命令都可以以相同的方式调用,只是没有破折号

The new Compose V2, which supports the compose command as part of the Docker CLI, is now available.新的 Compose V2 支持将 compose 命令作为 Docker CLI 的一部分,现已推出。

Compose V2 integrates compose functions into the Docker platform, continuing to support most of the previous docker-compose features and flags. Compose V2 将 compose 功能集成到 Docker 平台中,继续支持之前的大部分 docker-compose 功能和标志。 You can run Compose V2 by replacing the hyphen (-) with a space, using docker compose, instead of docker-compose.您可以通过使用 docker compose 而不是 docker-compose 将连字符 (-) 替换为空格来运行 Compose V2。

Update July 1st 2019 2019 年 7 月 1 日更新

docker-compose logs <name-of-service>

for all services对于所有服务

docker-compose logs

Use the following options from the documentation :使用文档中的以下选项:

Usage: logs [options] [SERVICE...]用法:日志[选项] [服务...]

Options:选项:

--no-color Produce monochrome output. --no-color 产生单色输出。

-f, --follow Follow log output. -f, --follow 跟随日志输出。

-t, --timestamps Show timestamps. -t, --timestamps 显示时间戳。

--tail="all" Number of lines to show from the end of the logs for each container. --tail="all" 从每个容器的日志末尾开始显示的行数。

See docker logs查看泊坞窗日志

You can start Docker compose in detached mode and attach yourself to the logs of all container later.您可以在分离模式下启动 Docker compose,稍后将自己附加到所有容器的日志中。 If you're done watching logs you can detach yourself from the logs output without shutting down your services.如果您已完成查看日志,您可以将自己从日志输出中分离出来,而无需关闭您的服务。

  1. Use docker-compose up -d to start all services in detached mode ( -d ) (you won't see any logs in detached mode)使用docker-compose up -d以分离模式( -d启动所有服务(在分离模式下您不会看到任何日志)
  2. Use docker-compose logs -f -t to attach yourself to the logs of all running services , whereas -f means you follow the log output and the -t option gives you timestamps (See Docker reference )使用docker-compose logs -f -t自己附加到所有正在运行的服务的日志中,而-f表示您遵循日志输出,而-t选项为您提供时间戳(请参阅Docker 参考
  3. Use Ctrl + z or Ctrl + c to detach yourself from the log output without shutting down your running containers使用Ctrl + zCtrl + c将自己从日志输出中分离出来,而无需关闭正在运行的容器

If you're interested in logs of a single container you can use the docker keyword instead:如果您对单个容器的日志感兴趣,可以使用docker关键字:

  1. Use docker logs -t -f <name-of-service>使用docker logs -t -f <name-of-service>

Save the output保存输出

To save the output to a file you add the following to your logs command:要将输出保存到文件中,请将以下内容添加到日志命令中:

  1. docker-compose logs -f -t >> myDockerCompose.log

If you want to see output logs from all the services in your terminal.如果您想查看终端中所有服务的输出日志

docker-compose logs -t -f --tail <no of lines> 

Eg.: Say you would like to log output of last 5 lines from all service例如:假设您想记录所有服务的最后 5 行的输出

docker-compose logs -t -f --tail 5

If you wish to log output from specific services then it can be done as below:如果您希望记录特定服务的输出,则可以按如下方式完成:

docker-compose logs -t -f --tail <no of lines> <name-of-service1> <name-of-service2> ... <name-of-service N>

Usage:用法:

Eg.例如。 say you have API and portal services then you can do something like below :假设您有 API 和门户服务,那么您可以执行以下操作:

docker-compose logs -t -f --tail 5 portal api

Where 5 represents last 5 lines from both logs.其中 5 代表两个日志的最后 5 行。

Ref: https://docs.docker.com/v17.09/engine/admin/logging/view_container_logs/参考: https ://docs.docker.com/v17.09/engine/admin/logging/view_container_logs/

  1. use the command to start containers in detached mode: docker-compose up -d使用命令以分离模式启动容器: docker-compose up -d
  2. to view the containers use: docker ps查看容器使用: docker ps
  3. to view logs for a container: docker logs <containerid>查看容器的日志: docker logs <containerid>

Unfortunately we need to run docker-compose logs separately from docker-compose run .不幸的是,我们需要将docker-compose logsdocker-compose run分开运行。 In order to get this to work reliably we need to suppress the docker-compose run exit status then redirect the log and exit with the right status.为了让它可靠地工作,我们需要抑制docker-compose run退出状态,然后重定向日志并以正确的状态退出。

#!/bin/bash
set -euo pipefail
docker-compose run app | tee app.log || failed=yes
docker-compose logs --no-color > docker-compose.log
[[ -z "${failed:-}" ]] || exit 1

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

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