compose icon indicating copy to clipboard operation
compose copied to clipboard

Stream logs during `--wait`

Open felipecrs opened this issue 3 years ago • 14 comments

The addition of --wait (#8777) was nice. But it would be especially helpful in CI/CD environments if the logs were streamed during the "wait".

For example, I'm trying to start an Oracle DB with --wait and it's not working. On CI/CD it's almost impossible to capture the logs for it unless I do some very weird trickery of running docker compose logs -f in parallel.

I understand that --wait implies --detach, but I guess it does not need to be the case until the --wait condition is met.

felipecrs avatar Jan 25 '22 15:01 felipecrs

Or some additional flag like --wait --logs would also do the trick.

felipecrs avatar Apr 11 '22 23:04 felipecrs

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 Nov 02 '22 00:11 stale[bot]

:-(

felipecrs avatar Nov 02 '22 00:11 felipecrs

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

stale[bot] avatar Nov 12 '22 12:11 stale[bot]

Should be re-opened

luishdz1010 avatar Nov 12 '22 13:11 luishdz1010

Yes, please!

felipecrs avatar Nov 12 '22 14:11 felipecrs

Out of curiosity, I built a simple Bash script that accomplishes the task at some sort:

#!/usr/bin/env bash

set -euo pipefail

container_name="${1}"

max_attempts="${2:-120}"

# Display logs while we wait for the container to be healthy
docker logs --follow "${container_name}" &
docker_logs_pid="${!}"

function cleanup() {
    # do not fail if the process has already exited
    kill "${docker_logs_pid}" 2>/dev/null || true
}

# Cleanup log process on exit
trap 'cleanup' EXIT

# Wait for container to be healthy
attempt=0
while true; do
    status=$(docker inspect --format '{{.State.Health.Status}}' "${container_name}" || true)
    if [[ "${status}" == "healthy" ]]; then
        cleanup
        echo "Container ${container_name} is healthy after ${attempt} seconds"
        exit 0
    elif [[ "${status}" != "starting" ]]; then
        attempt=$(( attempt + 1 ))
        if [[ "${attempt}" -ge "${max_attempts}" ]]; then
            cleanup
            echo "Container ${container_name} is not healthy after ${attempt} seconds"
            echo "Last healthcheck logs:"
            docker inspect --format '{{json .State.Health}}' "${container_name}"
            exit 1
        fi
    fi
    sleep 1
done

It has proven to be extremely helpful for debugging issues during CI pipelines. And it would be even better if docker-compose have built-in support for it.

felipecrs avatar Nov 23 '22 16:11 felipecrs

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 Jun 18 '23 05:06 stale[bot]

Please don't let this stall :)

felipecrs avatar Jun 18 '23 05:06 felipecrs

This issue has been automatically marked as not stale anymore due to the recent activity.

stale[bot] avatar Jun 18 '23 05:06 stale[bot]

The challenge here is that we can't dump logs to the console while rendering the "progress" UI showing resource status.

ndeloof avatar Oct 15 '24 15:10 ndeloof

@ndeloof, perhaps some inspiration can be taken from the way how docker build display logs while progressing.

felipecrs avatar Oct 16 '24 13:10 felipecrs

@ndeloof AFTER the docker compose up --wait --logs command fails, could we dump the logs of the healthcheck-failing containers?

Could be an easier first step and help troubleshoot most cases.

WebF0x avatar Jan 23 '25 03:01 WebF0x

I got this one, will create a PR soon once finalize

Image

mjagyo avatar Nov 30 '25 14:11 mjagyo

hello, apologies but I think I kinda hit a deadend here, not sure what went wrong but somehow I can't get this test to be successful, though I did not change anything under listCommand

=== RUN   TestStartStop
=== PAUSE TestStartStop
=== CONT  TestStartStop
    start_stop_test.go:32: Running command: docker compose version
Docker Compose version v5.0.0-rc.2-28-g9fada6cc2.m
=== RUN   TestStartStop/Up_a_project
    start_stop_test.go:42: Running command: docker compose -f ./fixtures/start-stop/compose.yaml --project-name e2e-start-stop-no-dependencies up -d
 Running heere     start_stop_test.go:46: Running command: docker compose ls --all
NAME                             STATUS                  CONFIG FILES
e2e-start-stop-no-dependencies   exited(1), running(2)   /Users/user/Documents/Projects/compose/pkg/e2e/fixtures/start-stop/compose.yaml
    start_stop_test.go:47:
        	Error Trace:	/Users/user/Documents/Projects/compose/pkg/e2e/start_stop_test.go:47
        	Error:      	Expect "NAME                             STATUS                  CONFIG FILES
        	            	e2e-start-stop-no-dependencies   exited(1), running(2)   /Users/user/Documents/Projects/compose/pkg/e2e/fixtures/start-stop/compose.yaml
        	            	" to match "e2e-start-stop-no-dependencies\s+running\(2\)"
        	Test:       	TestStartStop/Up_a_project

anyone can point me on where to look?

I can share my changes here if anyone wants to look, thanks!

mjagyo avatar Dec 16 '25 10:12 mjagyo

here is my patch file

wait-log.patch

mjagyo avatar Dec 16 '25 10:12 mjagyo

Please open a draft pull-request with your patch for easier review/comments/diagnostic

ndeloof avatar Dec 16 '25 10:12 ndeloof

here is the draft @ndeloof

https://github.com/docker/compose/pull/13461

mjagyo avatar Dec 16 '25 13:12 mjagyo