Skip to content

Commit 315e8c6

Browse files
committed
[Feature] Allow pass ip instead of container to loadbalancer
1 parent e24d692 commit 315e8c6

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.7.4-alpine3.10
1+
FROM python:3.8.0-alpine3.10
22

33
WORKDIR /usr/local/app
44

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ Automatic update `/etc/hosts` on start/stop containers by labels.
55
Requirements
66
-----
77
* **Native linux**
8-
_This tool has no effect on macOS or windows, because docker on these OS running in
9-
VM and you can't directly access from host to each container via ip._
8+
_This tool has no effect on macOS or windows, because docker on these OS run in
9+
VM and you can't directly access from host to each container via ip.
10+
Yet you can pass traffic through loadbalancer, see section above._
1011

1112
Usage
1213
-----
@@ -64,12 +65,12 @@ If priority is the same then early created container will be used.
6465

6566
Load Balancer
6667
----
67-
To pass the traffic through the loadbalancer you should define container's name in order to use it's ip for recording in hosts.
68+
In order to pass the traffic through the loadbalancer you should define container's name or valid IPv4.
6869
Just add one more colon and container name after it.
6970
```bash
7071
$ docker run -d --name lb nginx
7172
$ docker run -d --label ru.grachevko.dhu="nginx1.local:0:lb" nginx
72-
$ docker run -d --label ru.grachevko.dhu="nginx2.local:0:lb" nginx
73+
$ docker run -d --label ru.grachevko.dhu="nginx2.local:0:127.0.0.1" nginx
7374
$ ping nginx1.local // ip of lb
7475
$ ping nginx2.local // ip of lb
7576
```

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ services:
3838
image: nginx:alpine
3939
labels:
4040
ru.grachevko.dhu: 'nginx5.local:0:lb'
41+
42+
nginx6:
43+
image: nginx:alpine
44+
labels:
45+
ru.grachevko.dhu: 'nginx6.local:0:127.0.0.1'

main.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import docker
22
import re
3+
from netaddr import valid_ipv4
34

45
LABEL = 'ru.grachevko.dhu'
56
MARKER = '#### DOCKER HOSTS UPDATER ####'
@@ -22,17 +23,24 @@ def scan():
2223
for string in label.split(';'):
2324
priority = 0
2425
lb = container
26+
ip = False
2527

2628
if ':' in string:
2729
parts = string.split(':')
2830
string = parts[0]
2931
priority = int(parts[1]) if len(parts) >= 2 else priority
30-
lb = docker.containers.get(parts[2]) if len(parts) == 3 else lb
3132

32-
if not lb:
33-
continue
33+
if len(parts) == 3:
34+
lbString = parts[2]
35+
36+
if valid_ipv4(lbString):
37+
ip = lbString
38+
else:
39+
lb = docker.containers.get(lbString)
40+
41+
if ip == False:
42+
ip = next(iter(lb.attrs.get('NetworkSettings').get('Networks').values())).get('IPAddress')
3443

35-
ip = next(iter(lb.attrs.get('NetworkSettings').get('Networks').values())).get('IPAddress')
3644
if ip:
3745
containers.append({
3846
'ip': ip,

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
docker>=4.0.2
2+
netaddr

0 commit comments

Comments
 (0)