Bash script: How do I prevent Ctrl+C from being sent to docker-compose?

I’m trying to execute a piece of code when the user presses Ctrl+C in a Bash script, which makes an HTTP request to the running Docker container and saves the result to a local directory, before the container shuts down.

I’ve read on other posts that to prevent Ctrl+C being sent to child processes, I can use trap, like so:

function sigint_handler () {
  echo "Saving container state..."
  curl "http://localhost:$CONTAINER_PORT/api/state" > container_state.json
  exit
}

trap sigint_handler INT

docker-compose up

However, this doesn’t seem to be working. It looks like docker-compose is receiving the Ctrl+C and getting shut down first, then my SIGINT handler is executing:

^CGracefully stopping... (press Ctrl+C again to force)
Stopping metrics_grafana_1    ... done
Stopping metrics_prometheus_1 ... done
Saving container state...

How can I get my SIGINT handler to execute, then send SIGINT to docker-compose?

Source: Docker Questions