Please post your Compose file or equivalent.
The below reference to the hook scripts is not an error. The stack trace above it however is an error.
What happens when you run occ
within the container as described here (such as to view your active config - i.e. occ config:list system
?
The stack trace itself looks incomplete, but is occurring at the database connect.
Also, please check your Nextcloud log as described in the support template.
EDIT: Wait, are you really trying to downgrade? Your post subject says 31.0.7.1->31.0.2.1
.