Connect to docker container using hostname on local environment in kubernetes

10/1/2020

I have a kubernete docker-compose that contains

frontend - a web app running on port 80
backend - a node server for API running on port 80
database - mongodb

I would like to ideally access frontend via a hostname such as http://frontend:80, and for the browser to be able to access the backend via a hostname such as http://backend:80, which is required by the web app on the client side.

How can I go about having my containers accessible via those hostnames on my localhost environment (windows)?


docker-compose.yml

version: "3.8"
services:
    frontend:
        build: frontend
        hostname: framework
        ports:
            - "80:80"
            - "443:443"
            - "33440:33440"
    backend:
        build: backend
        hostname: backend
    database:
        image: 'mongo'
        environment:
            - MONGO_INITDB_DATABASE=framework-database
        volumes:
            - ./mongo/mongo-volume:/data/database
            - ./mongo/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
        ports:
            - '27017-27019:27017-27019'
-- mateos
dns
docker
docker-compose
kubernetes

1 Answer

10/3/2020

I was able to figure it out, using the docker-compose aliases & networks I was able to connect every container to the same development network.

match containers to hostnames

There was 3 main components:

  • container mapping node dns server - Grabs the aliases via docker ps and creates a DNS server that redirects those requests to 127.0.0.1 (localhost)
  • nginx reverse proxy container - mapping the hosts to the containers via their aliases in the virtual network
  • projects - each project is a docker-compose.yml that may have an unlimited number of containers running on port 80

docker-compose.yml for clientA

version: "3.8"
services:
    frontend:
        build: frontend
        container_name: clienta-frontend
        networks:
            default:
                aliases:
                    - clienta.test
    backend:
        build: backend
        container_name: clienta-backend
        networks:
            default:
                aliases:
                    - api.clienta.test
networks:
    default:
        external: true # connect to external network (see below for more)
        name: 'development' # name of external network

nginx proxy docker-compose.yml

version: '3'
services: 
    parent:
        image: nginx:alpine
        volumes:
            - ./nginx.conf:/etc/nginx/nginx.conf
        ports:
            - "80:80" #map port 80 to localhost
        networks:
            - development
networks:
    development: #create network called development
        name: 'development'
        driver: bridge

DNS Server

import dns from 'native-dns'
import { exec } from 'child_process'

const { createServer, Request } = dns
const authority = { address: '8.8.8.8', port: 53, type: 'udp' }
const hosts = {}

let server = createServer()

function command (cmd) {
    return new Promise((resolve, reject) => {
        exec(cmd, (err, stdout, stderr) => stdout ? resolve(stdout) : reject(stderr ?? err))
    })
}
async function getDockerHostnames(){
    let containersText = await command('docker ps --format "{{.ID}}"')
    let containers = containersText.split('\n')
    containers.pop()
    await Promise.all(containers.map(async containerID => {
        let json = JSON.parse(await command(`docker inspect ${containerID}`))?.[0]
        let aliases = json?.NetworkSettings?.Networks?.development?.Aliases || []
        aliases.map(alias => hosts[alias] = {
            domain: `^${alias}*`,
            records: [
                { type: 'A', address: '127.0.0.1', ttl: 100 }
            ]
        })
    }))
}

await getDockerHostnames()
setInterval(getDockerHostnames, 8000)

function proxy(question, response, cb) {
    var request = Request({
        question: question, // forwarding the question
        server: authority,  // this is the DNS server we are asking
        timeout: 1000
    })

    // when we get answers, append them to the response
    request.on('message', (err, msg) => {
        msg.answer.map(a => response.answer.push(a))
    });

    request.on('end', cb)
    request.send()
}

server.on('close', () => console.log('server closed', server.address()))
server.on('error', (err, buff, req, res) => console.error(err.stack))
server.on('socketError', (err, socket) => console.error(err))
server.on('request', async function handleRequest(request, response) {
    await Promise.all(request.question.map(question => {
        console.log(question.name)
        let entry = Object.values(hosts).find(r => new RegExp(r.domain, 'i').test(question.name))
        if (entry) {
            entry.records.map(record => {
                record.name = question.name;
                record.ttl = record.ttl ?? 600;
                return response.answer.push(dns[record.type](record));
            })
        } else {
            return new Promise(resolve => proxy(question, response, resolve))
        }
    }))

    response.send()
});

server.serve(53, '127.0.0.1');

Don't forget to update your computers network settings to use 127.0.0.1 as the DNS server

Git repository for dns server + nginx proxy in case you want to see the implementation: https://github.com/framework-tools/dockerdnsproxy

-- mateos
Source: StackOverflow