File tree Expand file tree Collapse file tree 5 files changed +24
-9
lines changed Expand file tree Collapse file tree 5 files changed +24
-9
lines changed Original file line number Diff line number Diff line change 1
- FROM python:3.7.4 -alpine3.10
1
+ FROM python:3.8.0 -alpine3.10
2
2
3
3
WORKDIR /usr/local/app
4
4
Original file line number Diff line number Diff line change @@ -5,8 +5,9 @@ Automatic update `/etc/hosts` on start/stop containers by labels.
5
5
Requirements
6
6
-----
7
7
* ** 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._
10
11
11
12
Usage
12
13
-----
@@ -64,12 +65,12 @@ If priority is the same then early created container will be used.
64
65
65
66
Load Balancer
66
67
----
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 .
68
69
Just add one more colon and container name after it.
69
70
``` bash
70
71
$ docker run -d --name lb nginx
71
72
$ 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
73
74
$ ping nginx1.local // ip of lb
74
75
$ ping nginx2.local // ip of lb
75
76
```
Original file line number Diff line number Diff line change @@ -38,3 +38,8 @@ services:
38
38
image : nginx:alpine
39
39
labels :
40
40
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'
Original file line number Diff line number Diff line change 1
1
import docker
2
2
import re
3
+ from netaddr import valid_ipv4
3
4
4
5
LABEL = 'ru.grachevko.dhu'
5
6
MARKER = '#### DOCKER HOSTS UPDATER ####'
@@ -22,17 +23,24 @@ def scan():
22
23
for string in label .split (';' ):
23
24
priority = 0
24
25
lb = container
26
+ ip = False
25
27
26
28
if ':' in string :
27
29
parts = string .split (':' )
28
30
string = parts [0 ]
29
31
priority = int (parts [1 ]) if len (parts ) >= 2 else priority
30
- lb = docker .containers .get (parts [2 ]) if len (parts ) == 3 else lb
31
32
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' )
34
43
35
- ip = next (iter (lb .attrs .get ('NetworkSettings' ).get ('Networks' ).values ())).get ('IPAddress' )
36
44
if ip :
37
45
containers .append ({
38
46
'ip' : ip ,
Original file line number Diff line number Diff line change 1
1
docker >= 4.0.2
2
+ netaddr
You can’t perform that action at this time.
0 commit comments