Docker-compose to a remote host using ssh fails in gitlab ci

I’m unable to docker-compose successfully on a remote host using SSH from a gitlab CI/CD pipeline runner.

docker-compose fails with the following errors :

http.client.RemoteDisconnected: Remote end closed connection without response

urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

I’m not able to repro the issue outside Gitlab. Meaning, I tried on my local machine by running the same docker image my gitlab deploy step use, do the exact same commands in my script manually and it worked like a charm…

I’m even trying the ssh config in the script by connecting to the host and printing docker version and it succeeds properly.

deploy_to_staging.sh

#!/bin/bash

apk update
apk add openssh-client
apk add --update bash

eval $(ssh-agent -s)
mkdir -p ~/.ssh
chmod 600 ~/.ssh
# copy the server private key
echo "$SSH_PRIVATE_KEY"
echo "$SSH_PRIVATE_KEY" | ssh-add -
# copy server host key .pub file from /etc/ssh/ and paste it into this variable
echo "$SSH_SERVER_HOSTKEYS"
ssh-keyscan -p 12345 11.222.3.444
ssh-keyscan -p 12345 11.222.3.444 >> ~/.ssh/known_hosts

ssh root@11.222.3.444 -p 12345 "docker version; exit"

# Docker-compose app and db
docker-compose --verbose -H "ssh://root@11.222.3.444:12345" -f docker-compose.staging.yml up -d --build

exit

gitlab-ci.yml

image: alpine:latest

services:
  - docker:dind

stages:
  - build
  - package
  - deploy

[...]

deploy:
  image: docker/compose:alpine-1.28.2
  stage: deploy
  environment:
    name: staging
  only:
    - master
  script:
    - sh deploy_to_staging.sh

Thanks for your help