Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The first argument specifies the target file, the second argument specifies the
Each docker container will be added as `<container-name>.docker`, `<container-short-id>.docker`.
Which network will be used for containers with multiple networks is undefined behaviour.

You can also set additional names (seprated by `;`) via the label `docker-hosts.hosts`.

**Tip:** if you want to use this on a dedicated DNS server instead of using the hosts file directly, I suggest using [CoreDNS](https://coredns.io/) with the [hosts plugin](https://coredns.io/plugins/hosts/).

## Build and installation
Expand Down
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ func ContainerToHosts(container types.Container, tld string) string {
if details.IPAddress == "" {
continue
}
hosts = details.IPAddress
// <docker-hosts.host>
if container.Labels["docker-hosts.hosts"] != "" {
names := strings.Split(container.Labels["docker-hosts.hosts"], ";")
for _, name := range names {
hosts += "\t" + strings.TrimPrefix(name, "/") + "." + tld
}
}
for _, name := range container.Names {
// <name>.docker
hosts += "\t" + strings.TrimPrefix(name, "/") + "." + tld
Expand Down Expand Up @@ -128,11 +134,13 @@ func main() {
panic(err)
}

file := "/etc/hosts"
file := hostFile
if len(os.Args) > 1 {
file = os.Args[1]
}

fmt.Printf("Hostfile was selected as: %s\n", file)

tld := "docker"
if len(os.Args) > 2 {
tld = os.Args[2]
Expand Down
5 changes: 5 additions & 0 deletions unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !windows

package main

const hostFile = "/etc/hosts"
5 changes: 5 additions & 0 deletions windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build windows

package main

const hostFile = "C:\\Windows\\System32\\drivers\\etc\\hosts"