Giter VIP home page Giter VIP logo

dns.docker's Introduction

Access container services by container name

Creates a tiny DNS server running in a container which integrates with docker daemon providing additional DNS resolution by container name.

In other words: Access your containers by http://container-name.docker

Tested on: Ubuntu 16 and Ubuntu 18 Prerequisites: docker and docker-compose

Docker setup

  • Create custom subnet

    docker network create --subnet 172.0.0.0/24 local

    This step is required as the dns container will need a static IP

  • Create docker-compose.yml

    I tend to keep a global file in ~/development/docker-compose.yml and projects in ~/development/* eg. keep this repo is in ~/development/dns.docker

version: '2'
services:
  dns.docker:
    build: ./dns.docker
    container_name: dns.docker
    restart: always
    networks:
      local:
        ipv4_address: 172.0.0.53
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

# Remember to create the subnet first first eg. `docker network create --subnet 172.0.0.0/24 local`
networks:
  local:
    external:
      name: local

Run and test it

cd ~
docker-compose up -d dns.docker
docker-compose logs -f dns.docker
dig anything.docker @172.0.0.53

The above docker-compose logs -f dns.docker should output ! [anything.docker] to confirm the container is running correctly and ready to resolve queries.

Once launched, the docker daemon will keep it running between restarts thanks to 'restart: always'

Ubuntu setup

In this step we're going to tell ubuntu to query the container running at 172.0.0.53 for anything in *.docker domain

  • sudo apt-get install dnsmasq

  • Create /etc/NetworkManager/dnsmasq.d/dns-local.conf with this content

    server=/docker/172.0.0.53

    This will tell dns resolver that '.docker' is a local private domain, and 172.0.0.53 is the dns server to query for its records.

  • Tell NetworkManager to use dnsmasq as resolver edit /etc/NetworkManager/NetworkManager.conf in section [main] add

    dns=dnsmasq

  • Disable default systemd-resolved resolver

    sudo systemctl disable systemd-resolved.service sudo systemctl stop systemd-resolved sudo rm /etc/resolv.conf

  • Restart network manager or reboot

What next

Now you can add any docker services to the yml file like so:

  mysql57.docker:
    container_name: mysql57.docker
    image: mysql/mysql-server:5.7.21
    restart: always
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      MYSQL_ROOT_PASSWORD: ""
      MYSQL_ROOT_HOST: "%"
    command: mysqld --sql_mode="NO_ENGINE_SUBSTITUTION"
    networks:
      local:

Start, test it

docker-compose up mysql57.docker
dig mysql57.docker

Your localhost as well as other containers in the 'local' subnet will be able to access these containers easily by their container_name eg. mysql57.docker. This is completely maintenance free ie run & forget it exists.

TODO

  • Find a way to use local private domain with systemd-resolved so that we don't need to install dnsmasq

dns.docker's People

Stargazers

 avatar

Watchers

 avatar

Forkers

wdmtech

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.