compose icon indicating copy to clipboard operation
compose copied to clipboard

Add an `--index=N` flag to `docker-compose logs` to monitor a single service instance: e.g. `docker-compose logs --index=8 --tail 100 -f app`

Open swiss-knight opened this issue 4 years ago • 5 comments

/!\ If your request is about evolving the compose file format, please report on the Compose Specification

Is your feature request related to a problem? Please describe. Let's say I spin up an app with the --scale option:

docker-compose up --build --scale app=20 app

Then, I would like to monitor it using the logs option, but things goes too fast on the terminal because of the 20 instances and everything is interleaved:

docker-compose logs --tail 100 -tf app

Describe the solution you'd like I'd like to have an --index flag, as in docker-compose exec to be able to log only one of the instances, e.g. the number 8:

docker-compose logs --index=8 --tail 100 -f app

The behaviour of that flag as described in docker-compose exec would be perfect for logs as well:

$ docker-compose exec --help
Execute a command in a running container

Usage: exec [options] [-e KEY=VAL...] [--] SERVICE COMMAND [ARGS...]

Options:
    -d, --detach      Detached mode: Run command in the background.
    --privileged      Give extended privileges to the process.
    -u, --user USER   Run the command as this user.
    -T                Disable pseudo-tty allocation. By default `docker-compose exec`
                      allocates a TTY.
    --index=index     index of the container if there are multiple
                      instances of a service [default: 1]
    -e, --env KEY=VAL Set environment variables (can be used multiple times,
                      not supported in API < 1.25)
    -w, --workdir DIR Path to workdir directory for this command.

But for the moment there is no such thing:

$ docker-compose logs --help
View output from containers.

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.

Describe alternatives you've considered Cannot figure out an alternative.

Additional context Docker version 20.10.8, build 3967b7d
docker-compose version 1.27.4, build 40524192
Host: Ubuntu 18.04

swiss-knight avatar Sep 20 '21 07:09 swiss-knight

If you want to only watch log for a single container, why don't you just use docker logs?

ndeloof avatar Sep 24 '21 07:09 ndeloof

Indeed. But once the docker logs command has been launched, I miss the (colorful) container name on the very left of the printed log. Therefore, if I open several different logs in different tabs of my terminal, I will for sure forgot which tab correspond to which container. Displaying the human readable name of a container is a very nice feature of docker-compose logs which is not provided by docker logs.

swiss-knight avatar Sep 25 '21 17:09 swiss-knight

understood

ndeloof avatar Sep 26 '21 10:09 ndeloof

Hello, Could I pickup this? Any tips to start with? -- start reading https://github.com/docker/compose/blob/v2/cmd/compose/logs.go & try to figure out about new log options to insert.

SeifMostafa avatar Mar 18 '22 08:03 SeifMostafa

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 21 '22 10:09 stale[bot]

This issue has been automatically closed because it had not recent activity during the stale period.

stale[bot] avatar Nov 02 '22 03:11 stale[bot]