In some quick testing, I found that if I defined WP_CLI_ALLOW_ROOT=1 after I was already running as root, then WP CLI wouldn’t complain. I don’t know a lot about GitLab CI, unfortunately, but I’d try this to see if it works:
image: docker:24.0.7
services:
- docker:24.0.7-dind
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_CERTDIR: ""
before_script:
- apk add --no-cache nodejs npm git
- npm install -g @wordpress/env
stages:
- test
wp_env_test:
stage: test
script:
- export WP_CLI_ALLOW_ROOT=1
- wp-env start
…unless, of course, that’s what you already tried.