Docker:dind : ping child container (API) is impossible

Hi,

I am using a docker with dind service in order to build 2 containers.
The first is a mongodb which is linked to a nodeJS + Express API thanks to a docker compose :

When the containers are built, I need to launch some unit tests. I usually connect to http://localhost:8080, but i cannot even ping my container.

here is my docker-compose.yml :

version: '3.5'

services:
    api:
        image: node:8
        container_name: api
        volumes:
          - ./www/:/usr/src/app
        working_dir: /usr/src/app
        ports:
          - 8080:8080
        links:
          - db
        restart: always
        hostname: api
        command: npm start
    db:
        image: bitnami/mongodb:3.6.13-r10
        container_name: db
        labels:
          kompose.service.type: nodeport
        ports:
          - 27017:27017
        volumes:
          - ./www/build/bdd/mongod.conf:/opt/bitnami/mongodb/conf
          - ./www/build/api/data/:/bitnami/mongodb/data
        restart: always
        command: mongod --dbpath /bitnami/mongodb/ --bind_ip 0.0.0.0

the gitlab-ci.yml :

image: k0walski/dind-docker-compose:0.3 # a docker:latest image with docker-compose already installed

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""

services:
  - name: docker:dind
    alias: dind
  - node:7
  - bitnami/mongodb:3.6

before_script:
  - docker-compose up -d

stages:
  - launch
  - test

compose-up:
  only:
    - dev
  tags:
    - ekkoGitRunner
  stage: launch
  script:
    - ./get_docker_ip.py
    - cat /etc/hostname
    - cat /etc/hosts
    - netstat -ant
    - docker ps
    - echo $DOCKER_HOST
    - nc -vz 172.19.0.3 8080

A netstat -ant gives nothing,
a docker network inspect ekko_default gives :

$ docker network inspect ekko_default
[
    {
        Name": "ekko_default",
        "Id": "d8666e9f739fd97f41c116fc91306a2e6869d691ffd1e53092c6f0477beaf962",
        "Created": "2020-01-23T20:03:37.17793378Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "194cd5a2fd2c3655540ab9cd27ca63c74374f3c31650c7c2c858d9a6b0eb918f": {
                "Name": "api",
                "EndpointID": "b8614e7719c0b88f74f512f53a8592504bde4e46dcf9011ec1265df04c9586cc",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            },
            "f5013e2a2f09cf644b0df30c4ff2e20816c0345196752bff1811df05af379850": {
                "Name": "db",
                "EndpointID": "2ed00288d81714a60d1072f7c92995f97559efab4957e9cf627a73cea183e76e",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "ekko",
            "com.docker.compose.version": "1.25.3"
        }
    }
]

And when I use nc -vz 172.19.0.3 8080, I have :
nc: 172.19.0.3 (172.19.0.3:8080): Operation timed out

Some help would be appreciated :slight_smile:

I resolved my problem and will explain here how I did it just in case someone encounters the same issue :

I was doing this wrong by wanting to run the test in the parent container.

Instead, I created a container sharing my project’s folder within the same network and ran the test within this test container.

docker run -dit --name test_container --network="containers_network" -v /path/to/project/folder:/data container_name

then

docker exec test_container sh -c "cd /data && npm i --save should chai request && npm i --g mocha should chai request && mocha"

My test does not run on localhost but container_name now.

1 Like