Skip to content

Commit

Permalink
Configurable containers domain (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
mageddo authored Mar 31, 2019
1 parent 22e28df commit 966a02a
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 23 deletions.
3 changes: 3 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 2.12.0
* Possibility to change container hostname domain, [see the docs](https://github.com/mageddo/dns-proxy-server/blob/70a0ff8/docs/features.md#access-container-by-its-container-name--service-name)

### 2.11.0
* Now you can customize host machine hostname, see [the docs](https://github.com/mageddo/dns-proxy-server/blob/fa1e044b/docs/features.md#solve-host-machine-ip-from-anywhere)
* Increased default loglevel to INFO
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.11.0
2.12.0
20 changes: 17 additions & 3 deletions conf/conf.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package conf

import (
"fmt"
"github.com/mageddo/dns-proxy-server/events/local"
"github.com/mageddo/dns-proxy-server/flags"
"os"
Expand Down Expand Up @@ -98,7 +99,7 @@ func GetString(value, defaultValue string) string {
return value
}

func RegisterContainerNames() bool {
func ShouldRegisterContainerNames() bool {
if v := os.Getenv(env.MG_REGISTER_CONTAINER_NAMES); v == "1" {
return true
}
Expand All @@ -109,11 +110,24 @@ func RegisterContainerNames() bool {
}

func GetHostname() string {
if hostname := os.Getenv(env.MG_HOST_MACHINE_HOSTNAME); len(strings.Trim(hostname, " ")) != 0 {
if hostname := os.Getenv(env.MG_HOST_MACHINE_HOSTNAME); len(strings.TrimSpace(hostname)) != 0 {
return hostname
}
if conf, _ := getConf(); conf != nil && len(conf.HostMachineHostname) != 0 {
return conf.HostMachineHostname
}
return *flags.Hostname
return *flags.HostMachineHostname
}

func FormatDPSDomain(subdomain string) string {
return fmt.Sprintf("%s.%s", subdomain, GetDPSDomain())
}
func GetDPSDomain() string {
if domain := os.Getenv(env.MG_DOMAIN); len(strings.TrimSpace(domain)) != 0 {
return domain
}
if conf, _ := getConf(); conf != nil && len(conf.HostMachineHostname) != 0 {
return conf.Domain
}
return *flags.Domain
}
20 changes: 20 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ __Using environment variable__
MG_REGISTER_CONTAINER_NAMES=1 ./dns-proxy-server
```

You can also customize the domain from docker to whatever you want by

__Activating by command line__

./dns-proxy-server --domain docker

__Configuring at json config file__

```
...
"domain": "docker"
...
```

__Using environment variable__

```bash
MG_DOMAIN=docker ./dns-proxy-server
```

### Specify from which network solve container IP
If your container have multiple networks then you can specify which network to use when solving IP by specifying `dps.network` label.

Expand Down
4 changes: 2 additions & 2 deletions events/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func getHostnames(inspect types.ContainerJSON) []string {
}
hostnames = append(hostnames, getHostnamesFromEnv(inspect)...)

if conf.RegisterContainerNames() {
if conf.ShouldRegisterContainerNames() {
hostnames = append(hostnames, getHostnameFromContainerName(inspect))
if hostnameFromServiceName, err := getHostnameFromServiceName(inspect); err == nil {
hostnames = append(hostnames, hostnameFromServiceName)
Expand Down Expand Up @@ -155,7 +155,7 @@ func getMachineHostname(inspect types.ContainerJSON) (string, error) {
}

func getHostnameFromContainerName(inspect types.ContainerJSON) string {
return fmt.Sprintf("%s.docker", inspect.Name[1:])
return fmt.Sprintf("%s.%s", inspect.Name[1:], conf.GetDPSDomain())
}

func getHostnameFromServiceName(inspect types.ContainerJSON) (string, error) {
Expand Down
3 changes: 3 additions & 0 deletions events/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ type LocalConfiguration struct {

// hostname to solve host machine IP
HostMachineHostname string `json:"hostMachineHostname"`

// domain utilized to solve container names
Domain string `json:"domain"`
}

type EnvVo struct {
Expand Down
21 changes: 7 additions & 14 deletions flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ var (
docker = start as docker service,
normal = start as normal service,
uninstall = uninstall the service from machine `)
publishServicePort = flag.Bool("service-publish-web-port", true, "Publish web port when running as service in docker mode")
logToFile = flag.String("log-file", "console", "Log to file instead of console, (true=log to default log file, /tmp/log.log=log to custom log location)")
logLevel = flag.String("log-level", "INFO", "Log Level ERROR, WARNING, INFO, DEBUG")
publishServicePort = flag.Bool("service-publish-web-port", true, "Publish web port when running as service in docker mode")
logToFile = flag.String("log-file", "console", "Log to file instead of console, (true=log to default log file, /tmp/log.log=log to custom log location)")
logLevel = flag.String("log-level", "INFO", "Log Level ERROR, WARNING, INFO, DEBUG")
registerContainerNames = flag.Bool("register-container-names", false, "If must register container name / service name as host in DNS server")
Version = flag.Bool("version", false, "Current version")
Hostname = flag.String("host-machine-hostname", "host.docker", "The hostname to get host machine IP")
Help = flag.Bool("help", false, "This message")
Version = flag.Bool("version", false, "Current version")
HostMachineHostname = flag.String("host-machine-hostname", "host.docker", "The hostname to get host machine IP")
Domain = flag.String("domain", "docker", "Domain utilized to solver containers and services hostnames")
Help = flag.Bool("help", false, "This message")
)

func init(){
Expand All @@ -49,14 +50,6 @@ func PublishServicePort() bool {
}

func GetRawCurrentVersion() string {

//if len(version) == 0 {
//b, err := ioutil.ReadFile(utils.GetPath("VERSION")) // just pass the file name
//if err == nil {
// return string(b)
//}
//log.Logger.Warningf("status=could-not-recover-version, err=%v", err)
//}
return version
}

Expand Down
9 changes: 6 additions & 3 deletions service/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ func NewDockerScript() *Script {

script := `'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin ; ` +
`docker rm -f dns-proxy-server &> /dev/null ;` +
`docker run --hostname dns.mageddo --name dns-proxy-server %s ` +
`docker run -e HOSTNAMES=%s --name dns-proxy-server %s ` +
`-v /opt/dns-proxy-server/conf:/app/conf ` +
`-v /var/run/docker.sock:/var/run/docker.sock ` +
`-v /etc/resolv.conf:/etc/resolv.conf ` +
`defreitas/dns-proxy-server:%s'`
script = fmt.Sprintf(script, getExposedPort(), flags.GetRawCurrentVersion())
script = fmt.Sprintf(
script, fmt.Sprintf("dns.mageddo,%s", conf.FormatDPSDomain("dns")),
getExposedPort(), flags.GetRawCurrentVersion(),
)
return &Script{script}
}

Expand All @@ -25,4 +28,4 @@ func getExposedPort() string {
} else {
return ""
}
}
}
1 change: 1 addition & 0 deletions utils/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const (
MG_REGISTER_CONTAINER_NAMES = "MG_REGISTER_CONTAINER_NAMES"

MG_HOST_MACHINE_HOSTNAME = "MG_HOST_MACHINE_HOSTNAME"
MG_DOMAIN = "MG_DOMAIN"
)

0 comments on commit 966a02a

Please sign in to comment.