Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream logs during --wait #9122

Open
felipecrs opened this issue Jan 25, 2022 · 13 comments
Open

Stream logs during --wait #9122

felipecrs opened this issue Jan 25, 2022 · 13 comments

Comments

@felipecrs
Copy link

felipecrs commented Jan 25, 2022

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 felipecrs changed the title Stream logs during --wait Stream logs during --wait Jan 25, 2022
@felipecrs
Copy link
Author

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

@stale
Copy link

stale bot commented Nov 2, 2022

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.

@felipecrs
Copy link
Author

:-(

@stale stale bot added the stale label Nov 2, 2022
@stale
Copy link

stale bot commented Nov 12, 2022

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

@stale stale bot closed this as completed Nov 12, 2022
@luishdz1010
Copy link

Should be re-opened

@felipecrs
Copy link
Author

Yes, please!

@felipecrs
Copy link
Author

felipecrs commented Nov 23, 2022

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.

@stale
Copy link

stale bot commented Jun 18, 2023

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 stale bot added the stale label Jun 18, 2023
@felipecrs
Copy link
Author

Please don't let this stall :)

@stale
Copy link

stale bot commented Jun 18, 2023

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

@ndeloof
Copy link
Contributor

ndeloof commented Oct 15, 2024

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

@felipecrs
Copy link
Author

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

@WebF0x
Copy link

WebF0x commented Jan 23, 2025

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants