Stream logs during `--wait`
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.
Or some additional flag like --wait --logs would also do the trick.
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.
:-(
This issue has been automatically closed because it had not recent activity during the stale period.
Should be re-opened
Yes, please!
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.
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.
Please don't let this stall :)
This issue has been automatically marked as not stale anymore due to the recent activity.
The challenge here is that we can't dump logs to the console while rendering the "progress" UI showing resource status.
@ndeloof, perhaps some inspiration can be taken from the way how docker build display logs while progressing.
@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.
I got this one, will create a PR soon once finalize
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!
Please open a draft pull-request with your patch for easier review/comments/diagnostic
here is the draft @ndeloof
https://github.com/docker/compose/pull/13461