Skip to content

Unable to access exposed ports opened by docker-compose when running in a GitLab CI job

Apologies if this is a known limitation of the GitLab runner/setup, but I haven't found any documentation suggesting this is the case.

In my .gitlab-ci.yml I'm using docker-compose to start my application image along with a temporary MySQL service. The relevant job/stage of the build is:

test:
  image: craigotis/docker-c-npm:v1
  stage: test
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com/craigotis/myapp
    - docker images
    - docker-compose up -d
    - sleep 60 # temporary workaround to get 'logs' to work below
    - docker-compose logs
    - docker-compose port app 9143
    - netstat -a
    - docker-compose ps
    - /usr/local/bin/wait-for-it.sh -h localhost -p 9143 -t 60
    - cd mocha
    - npm i
    - npm test
    - docker-compose down

Note that if I run the docker-compose up on my local machine, it works fine and I can access port localhost:9143 in a browser without issue.

My docker-compose.yml file is fairly straightforward:

version: '2'

networks:
    myapp_net:
        driver: bridge

services:
    app:
        image: registry.gitlab.com/craigotis/myapp:latest
        depends_on:
        - "db"
        networks:
        - myapp_net
        command: wait-for-it.sh mysql_server:3306 -t 60 -- java -jar /opt/myapp*.jar
        ports:
        - "9143:9143"

    db:
        image: mysql:latest
        networks:
        - myapp_net
        container_name: mysql_server
        command:
        environment:
        - MYSQL_ALLOW_EMPTY_PASSWORD=true

The logs for the application image suggest it's listening:

app_1  | [Thread-1] INFO spark.webserver.SparkServer - == Spark has ignited ...
app_1  | [Thread-1] INFO spark.webserver.SparkServer - >> Listening on 0.0.0.0:9143
app_1  | [Thread-1] INFO org.eclipse.jetty.server.Server - jetty-9.0.z-SNAPSHOT
app_1  | [Thread-1] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector@4e883fb1{HTTP/1.1}{0.0.0.0:9143}

However in my CI container, I cannot reach the port:

$ docker-compose port app 9143
0.0.0.0:9143

$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 runner-4e4528ca-project-1925166-concurrent-0:38542 docker:2375             TIME_WAIT   
tcp        0      0 runner-4e4528ca-project-1925166-concurrent-0:38540 docker:2375             TIME_WAIT   
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path

$ docker-compose ps
stty: standard input: Not a tty
    Name                  Command               State           Ports          
------------------------------------------------------------------------------
myapp_1   wait-for-it.sh mysql_serve ...   Up      0.0.0.0:9143->9143/tcp 
mysql_server   docker-entrypoint.sh --cha ...   Up      3306/tcp     
          
$ /usr/local/bin/wait-for-it.sh -h localhost -p 9143 -t 60
wait-for-it.sh: waiting 60 seconds for localhost:9143
wait-for-it.sh: timeout occurred after waiting 60 seconds for localhost:9143

Is there a trick to using docker-compose within a CI build stage to open ports to the CI container itself?