GitLab ci: service and image vs service and docker run

I need some help understanding the service option in the GitLab ci. Originally I wanted to test some C++ code that connects to a MongoDB. Because I wasn’t able to connect to the MongoDB I reduced my example to something quite simple.

This is my gitlab-ci.yml:

stages:
  - connect1
  - connect2

image: docker:latest

variables:
  MONGOIMAGE: "mongo:4.2.3-bionic"
  MONGO_INITDB_ROOT_USERNAME: "root"
  MONGO_INITDB_ROOT_PASSWORD: "geheim"

connect1:
  stage: connect1
  services:
    - name: $MONGOIMAGE
  image: mongo
  script:
    - mongo --host mongo --username $MONGO_INITDB_ROOT_USERNAME --password $MONGO_INITDB_ROOT_PASSWORD --eval "db.help()"

connect2:
  stage: connect2
  services:
    - name: $MONGOIMAGE
  script:
    - docker run --rm mongo mongo --host mongo --username $MONGO_INITDB_ROOT_USERNAME --password $MONGO_INITDB_ROOT_PASSWORD --eval "db.help()"

connect1 successfully connects to the MongoDB started as service and prints the expected help message.

connect2 fails to connect with this message:

Running with gitlab-runner 13.0.1 (21cb397c)
  on default runner ntkJxGir
Preparing the "docker" executor
Using Docker executor with image docker:latest ...
Starting service mongo:4.2.3-bionic ...
Pulling docker image mongo:4.2.3-bionic ...
Using docker image sha256:97a9a3e851586594f8b4a33dbefa090c7eebbb40383fa2608e0b7c7364181094 for mongo:4.2.3-bionic ...
Waiting for services to be up and running...
Pulling docker image docker:latest ...
Using docker image sha256:0bfe00e7bcd536451161fd64b2fe05f4ff6373ce9dd241e9762b68c7f56ce438 for docker:latest ...
Preparing environment
Running on runner-ntkjxgir-project-26-concurrent-0 via adbcf27aaa77...
Getting source from Git repository
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in /builds/platform/sim-cad/.git/
Checking out 5b57ea3b as servicetest...
Skipping Git submodules setup
Restoring cache
Downloading artifacts
Running before_script and script
$ docker run --rm mongo mongo --host mongo --username $MONGO_INITDB_ROOT_USERNAME --password $MONGO_INITDB_ROOT_PASSWORD --eval "db.help()"
MongoDB shell version v4.2.7
connecting to: mongodb://mongo:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-06-04T16:20:47.614+0000 E  QUERY    [js] Error: couldn't connect to server mongo:27017, connection attempt failed: HostNotFound: Could not find address for mongo:27017: SocketException: Host not found (authoritative) :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-06-04T16:20:47.615+0000 F  -        [main] exception: connect failed
2020-06-04T16:20:47.615+0000 E  -        [main] exiting with code 1
Running after_script
Uploading artifacts for failed job
ERROR: Job failed: exit code 1

And now the question: what is happening here? Why can’t I connect from a self started docker container to the service? Normally I want to run a docker container with my C++ code to connect to the MongoDB service but it fails like connect2.

I just recognized there is a GitLab ci sublevel. Maybe an admin could move this post?