183

When I use docker-compose up I can see logs for all containers in my docker-compose.yml file.

However, when I use docker-compose run app I only see console output for app but none of the services that app depends on. How can see log output for the other services?

sthomps
  • 4,480
  • 7
  • 35
  • 54

4 Answers4

257

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.

Update June 10th 2022

As commented by @Sandburg docker compose is now integrated into docker. As described here most of the docker compose commands can be called the same way just without the hyphen. So docker compose instead of docker-compose:

The new Compose V2, which supports the compose command as part of the Docker CLI, is now available.

Compose V2 integrates compose functions into the Docker platform, continuing to support most of the previous docker-compose features and flags. You can run Compose V2 by replacing the hyphen (-) with a space, using docker compose, instead of docker-compose.

Update July 1st 2019

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.

-f, --follow Follow log output.

-t, --timestamps Show timestamps.

--tail="all" Number of lines to show from the end of the logs for each container.

See docker logs

You can start Docker compose in detached mode and attach yourself to the logs of all container later. 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)
  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)
  3. Use Ctrl + z or Ctrl + c to detach yourself from the log output without shutting down your running containers

If you're interested in logs of a single container you can use the docker keyword instead:

  1. Use 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
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
  • 26
    I don't know why this is the accepted answer. Question was about docker-compose run and a lot of times you run that with -rm so there are no logs. I would like to see output of shell script as i'm running it. – James O'Brien Apr 22 '18 at 21:27
  • 6
    While this is useful info, this is NOT an answer to the question. As @JamesO'Brien said, the question is about `docker-compose run` and attaching to those services, which have unique names from the services listed in `docker.compose.yml` – TetraDev Jun 28 '19 at 17:54
  • 2
    I think that there is a critical thing that is missing in every piece of documentation that I've seen: the container instance launched by `docker-compose run` doesn't count as a "service", as meant by docker-compose. `docker-compose logs` only prints the logs of "services". This is why you can't print the logs of the main command using `docker-compose logs`, so if you use `docker-compose run -d`, you can only check the logs of the main job using `docker logs` (not `compose`) command. – GolDDranks Aug 25 '21 at 07:35
  • `docker-compose` has been integrated in docker and replaced by `docker compose` without dash. – Sandburg Jun 10 '22 at 10:24
  • `docker-compose logs -f -t` works only within the directory containing the docker-compose.yaml – Hans Ratzinger Mar 30 '23 at 18:13
  • `docker compose` without hyphen needs to be activated up to the current docker version 23.0.2 [Evolution of Compose](https://docs.docker.com/compose/compose-v2/) – Hans Ratzinger Mar 30 '23 at 18:57
  • 1
    @HansRatzinger thanks for your comment. I couldn't find any hint about the activation of `docker compose` without a hyphen in the linked documentation. Could you further explain, please? I'm thinking about improving my answer if I can point to the documentation. – Bruno Bieri Mar 31 '23 at 05:42
  • @BrunoBieri thanks for your question. May be this will give a better explanation: https://stackoverflow.com/a/64907243/11634713 – Hans Ratzinger Apr 01 '23 at 17:05
46

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

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 :

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

Where 5 represents last 5 lines from both logs.

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

Invincible
  • 1,418
  • 18
  • 23
15
  1. use the command to start containers in detached mode: docker-compose up -d
  2. to view the containers use: docker ps
  3. to view logs for a container: docker logs <containerid>
RJFalconer
  • 10,890
  • 5
  • 51
  • 66
akshaya pandey
  • 997
  • 6
  • 16
5

Unfortunately we need to run docker-compose logs separately from docker-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.

#!/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
Nathan
  • 5,272
  • 2
  • 26
  • 28
  • Is there a windows equivalent of the above? – Espresso Feb 21 '21 at 17:53
  • 2
    @Espresso, sure. `(docker-compose --verbose up | Tee-Object build.log) || ($env:FAILED='yes')`. I cannot answer in full, since that would require threading. I don't think it's worth emulating bash's pipefail. Take a look instead at `Start-TreadJob` and https://learn.microsoft.com/en-us/dotnet/api/system.threading.semaphore?view=net-6.0 – Mavaddat Javid Jan 25 '22 at 06:40