查看特定的 Docker Compose 服务日志
使用 docker-compose up 时,我们可以在 YAML 文件中看到所有容器的日志; 然而,如果我们指定一个特定的容器服务,输出将不会在日志中显示任何服务依赖性。 因此,本文将探讨如何正确导出输出Docker Compose服务日志。
查看特定的 Docker Compose 服务日志
您可以以分离模式启动 Docker Compose,稍后将自己附加到所有容器的日志中。 然后,如果您完成了对日志跟踪的观察,您可以在不关闭服务的情况下从日志输出中分离出来。
请按照以下步骤完成此操作:
-
使用
docker-compose up -d
使用 -d 选项以分离模式启动所有服务。 -
使用
docker-compose logs -t -f
将自己附加到所有正在运行的服务的日志中。 -t 选项为我们提供时间戳,而 -f 参数表示我们在服务运行时跟踪日志输出。 - 使用 Ctrl+Z 或 Ctrl+C 将自己从日志输出中分离出来,而无需关闭正在运行的容器。
-
如果我们需要指定单个容器的日志,可以使用下面的命令:
$ docker-compose logs -t -f <service name>
-
要将输出保存到文件中,我们可以使用
>>
运算符将其添加到命令的末尾。$ docker-compose logs -t -f >> sample.log
使用 Docker Compose V2
从 Docker Compose 版本 2 开始,我们现在可以使用不带破折号 (-) 的 docker-compose。 我们还可以使用大多数 docker-compose 命令,也可以类似地调用它们而无需破折号:
$ docker compose logs -t -f --tail 3 sample-service test-service >> sample.log
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
发布时间:2025/03/26 浏览次数:107 分类:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
发布时间:2025/03/26 浏览次数:97 分类:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
发布时间:2025/03/26 浏览次数:124 分类:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
发布时间:2025/03/26 浏览次数:202 分类:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
发布时间:2025/03/26 浏览次数:87 分类:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
发布时间:2025/03/26 浏览次数:140 分类:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application