https://github.com/grachevko/docker-hosts-updater
Automatic update /etc/hosts on start/stop containers.
https://github.com/grachevko/docker-hosts-updater
development-environment dns docker service-discovery
Last synced: about 1 month ago
JSON representation
Automatic update /etc/hosts on start/stop containers.
- Host: GitHub
- URL: https://github.com/grachevko/docker-hosts-updater
- Owner: grachevko
- Created: 2016-08-10T21:32:43.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2021-09-13T19:25:17.000Z (about 4 years ago)
- Last Synced: 2025-09-01T01:27:24.806Z (about 1 month ago)
- Topics: development-environment, dns, docker, service-discovery
- Language: Python
- Homepage:
- Size: 41 KB
- Stars: 39
- Watchers: 3
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
docker-hosts-updater
----------
Automatic update `/etc/hosts` on start/stop containers by labels.Requirements
-----
* **Native linux**
_This tool has no effect on macOS or windows, because docker on these OS run in
VM and you can't directly access from host to each container via ip.
Yet you can pass traffic through loadbalancer, see section above._Usage
-----
Start up `docker-hosts-updater`:```bash
$ docker run -d --restart=always \
--name docker-hosts-updater \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /etc/hosts:/opt/hosts \
grachevko/docker-hosts-updater
```
Start containers with label `ru.grachevko.dhu` option% docker run -d --label ru.grachevko.dhu=nginx.local nginx
Try to ping from host% ping nginx.local
Default hosts
-----
By default adding records with container name and container hostname.
To disable it you can use environments `CONTAINER_HOSTNAME_DISABLED` and `CONTAINER_NAME_DISABLED`:
```bash
$ docker run -d --restart=always \
--name docker-hosts-updater \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /etc/hosts:/opt/hosts \
-e CONTAINER_HOSTNAME_DISABLED=false \
-e CONTAINER_NAME_DISABLED=false \
grachevko/docker-hosts-updater
```Multiple Hosts
-----
You can add multiple hosts, just separate them by semicolon:```bash
$ docker run --label ru.grachevko.dhu="nginx.local;nginx.ru" nginx
$ ping nginx.local
$ ping nginx.ru
```Subdomains
-----
Add subdomains by using pattern `{www,api}.nginx.local`:```bash
$ docker run -d --label ru.grachevko.dhu="{www,api}.nginx.local" nginx
$ ping nginx.local
$ ping www.nginx.local
$ ping api.nginx.local
```Priority
----
If you want to run two containers with same hosts and want one override another,
just add priority after colon:```bash
$ docker run -d --label ru.grachevko.dhu="nginx.local" nginx
$ docker run -d --label ru.grachevko.dhu="nginx.local:10" nginx
$ ping nginx.local
```
Container with greater priority will be used. Default priority 0.
If priority is the same then early created container will be used.Load Balancer
----
In order to pass the traffic through the loadbalancer you should define container's name or valid IPv4.
Just add one more colon and container name after it.
```bash
$ docker run -d --name lb nginx
$ docker run -d --label ru.grachevko.dhu="nginx1.local:0:lb" nginx
$ docker run -d --label ru.grachevko.dhu="nginx2.local:0:127.0.0.1" nginx
$ ping nginx1.local // ip of lb
$ ping nginx2.local // ip of lb
```
Keep in mind, loadbalancer container must have fixed name.