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?
The docker logs command shows information logged by a running container. The docker service logs command shows information logged by all containers participating in a service. The information that is logged and the format of the log depends almost entirely on the container's endpoint command.
The output in docker-compose is the stdout/stderr from the command the container runs. You see this with docker run if you don't detach, and you can get this from docker logs on a container you're detached from or docker-compose logs from a compose submitted group of containers.
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.
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 dash:
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.
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.
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.
docker-compose up -d
to start all services in detached mode (-d
) (you won't see any logs in detached mode)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)Ctrl + z
or Ctrl + c
to detach yourself from the log output without shutting down your running containersIf you're interested in logs of a single container you can use the docker
keyword instead:
docker logs -t -f <name-of-service>
To save the output to a file you add the following to your logs command:
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
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/
docker-compose up -d
docker ps
docker logs <containerid>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With