docker-compose dns and python docker api nested container

46 views Asked by At

The cis container is running a simple flask server that calls daemon() on the backend. As part of the nested container's functionality, it needs to be able to access the git container as well as other infrastructure on the network.

In the current setup, all services are accessible to each other by hostname, and they are accessible to the other machines on the network, also by hostname. However, the nested container is unable to resolve hostnames.

What can I do so that the nested container is able to resolve the necessary hostnames?

def daemon(dc:DockerClient,
           url:str, image:str)->Tuple[str,int]:
    try:
        cont = dc.containers.create(image,
                                    #network='myproject_default',
                                    network='ops')
        try:
            cont.start()
            status = cont.wait()['StatusCode']
            logs = cont.logs().decode('utf-8')
            return logs, status
        finally:
            cont.remove()        
    except errors.APIError:
        raise
    except Exception:
        raise

Sample docker-compose config:

version: '3.8'
services:
  git:
    image: rockstorm/git-server
    hostname:        git.mynetwork.com
    ports:
      - "0.0.0.0:myport1:myport1/tcp"
    networks:
      - default
      - ops

  cis:
    image: cis
    hostname:                 cis.mynetwork.com
    ports:
      - "0.0.0.0:myport2:myport2/tcp"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:rw
    networks:
      - default
      - ops
    depends_on:
      git:
        condition:   service_started

in /etc/hosts:

myip           git                 git.innovanon.com
myip           cis                 cis.innovanon.com
0

There are 0 answers