Make docker dind connect to cassandra

I am relatively new to Docker and Docker compose and am quite stuck trying to work how to get my test suite connecting to a docker instance. I am hoping some kind person here would be able to check my config.

What I am doing, is having my test cases read an environment variable CASSANDRA_HOSTNAME to know how to connect to the cassandra instances that is started by docker compose. When I set the hostname to 127.0.0.1, it fails to connect, when I set the hostname to docker the DNS lookup fails:

failed: lookup docker on 169.254.169.254:53: no such host

My .gitlab-ci.yml:

before_script:
  - docker info

image: gitlab/dind

services:
  - docker:dind

build_image:
  script:
    - docker-compose up -d
    - docker build --build-arg CASSANDRA_HOSTNAME=docker .
    - docker-compose down

My docker compose works on my machine, opens the correct ports and so on:

version: '2'

services:
  cassandra:
    image: cassandra:latest
    ports:
      - "9042:9042" 
      - "7000:7000" 
      - "7001:7001" 
      - "9160:9160"

And the relevant parts of my docker file:

FROM golang:1.14.4-alpine3.12
...
RUN git clone git@gitlab.com:user/repo.git /work/
RUN go build
ARG CASSANDRA_HOSTNAME
ENV CASSANDRA_HOSTNAME ${CASSANDRA_HOSTNAME}
RUN CGO_ENABLED=0 go test

If I do a docker-compose ps and a netstat -an inside my .gitlab-ci.yml` I can see the following in the logs:

e[32;1m$ docker-compose pse[0;m
stty: standard input: Inappropriate ioctl for device
        Name                      Command               State                                                    Ports                                                   
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
security_cassandra_1   docker-entrypoint.sh cassa ...   Up      0.0.0.0:7000->7000/tcp, 0.0.0.0:7001->7001/tcp, 7199/tcp, 0.0.0.0:9042->9042/tcp, 0.0.0.0:9160->9160/tcp 
e[32;1m$ netstat -ane[0;m
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 172.17.0.4:54166        172.17.0.3:2375         TIME_WAIT  
tcp        0      0 172.17.0.4:54232        172.17.0.3:2375         TIME_WAIT  
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     26882    /var/run/docker.sock
unix  2      [ ACC ]     STREAM     LISTENING     26889    /var/run/docker/libcontainerd/docker-containerd.sock
unix  2      [ ACC ]     STREAM     LISTENING     27000    /run/docker/libnetwork/49d444fe2963a74a48d707b1df01232fd6cb75386de74e5881316a74f39d1d7c.sock
unix  3      [ ]         STREAM     CONNECTED     26891    
unix  3      [ ]         STREAM     CONNECTED     26892    /var/run/docker/libcontainerd/docker-containerd.sock

For anyone else who encounters this problem in the future. The problem is that the gitlab/dind image is 4 years out of date, and doesnt support newer versions of docker that allow networking during the build phase. I had to convert to the newer non gitlab dind image (which is a pain as it adds 5 minutes to the build process to create a comparable image), but it works. :grinning: