Docker Swarm Service creation is stuck, How to make sure it exits instead of getting stuck

167 Views Asked by At

I'm using docker swarm here and used docker service create, it is stuck due to some mount error or any service pre-requisite which is not being met. But instead of forever getting stuck. I want it to exit when such a failure scenario has come across.

How to do it ? I don't see anything present in inbuilt in docker for now. Tried using a script too.

docker service creation...
service_pid=$!
timeout_seconds=600
while true; do
    if docker service ls --filter name="$service_name" --format '{{.Name}}' | grep -q "$service_name"; then
        echo "Service $service_name created successfully."
        break
    elif [ $SECONDS -ge $timeout_seconds ]; then
        echo "Service creation timed out after $timeout_seconds seconds."
        # Optionally, you can clean up the service here if desired
        docker service rm "$service_name" >/dev/null 2>&1
        break
    fi
    sleep 5
done

But the problem here it's not leaving docker service creation command so it does not even go next line of it.

1

There are 1 best solutions below

1
Chris Becke On

docker service create typically waits for the service tasks to deploy and become healthy before returning.

The detach flag (docker service create --detach ...) can be used to return immediately, so service creating can be tracked from your script.