How to View Specific Docker Compose Service Logs

How to View Specific Docker Compose Service Logs

When using docker-compose up, we can see the logs for all the containers in our YAML file; however, if we specify a particular container service, the output will not show any service dependencies on the logs. Therefore, this article will discuss how to correctly export and output Docker Compose service logs.

View Specific Docker Compose Service Logs

You can start Docker Compose in detached mode and attach yourself to the logs of all containers later. Then, if you are done watching the logs trail, you can detach from the logs output without shutting down your services.

Follow the steps below to accomplish this:

  • Use docker-compose up -d to start all services in detached mode using the -d option.
  • Use docker-compose logs -t -f to attach yourself to the logs of all running services. The -t option gives us timestamps, while the -f parameter means we follow the log output while the services run.
  • Use Ctrl+Z or Ctrl+C to detach yourself from the log output without shutting down your running containers.
  • If we need to specify the logs of a single container, we can use the command below:
    docker-compose logs -t -f <service name>
    
  • To save the output to a file, we can stdout using the >> operator and append them at the end of our command.
    docker-compose logs -t -f >> sample.log
    

Use Docker Compose V2

As of Docker Compose version 2, we can now use docker-compose without the dash (-). We can also use most of the docker-compose commands, which can also be called the similarly without the dashes:

docker compose logs -t -f --tail 3 sample-service test-service >> sample.log
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - Docker Compose