mirror of
https://github.com/11notes/docker-traefik-labels.git
synced 2026-11-13 06:17:40 -06:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ccdf29b7ae | ||
|
|
69f14549c6 | ||
|
|
6ab10f6b2a | ||
|
|
35fee8e82b | ||
|
|
8686ba58c0 | ||
|
|
437a3e92d3 | ||
|
|
4796cc1aa1 |
@@ -1,5 +1,5 @@
|
||||
# Alpine :: Traefik Labels
|
||||
    
|
||||
    
|
||||
|
||||
Run Traefik Labels based on Alpine Linux. Small, lightweight, secure and fast 🏔️
|
||||
|
||||
@@ -7,8 +7,9 @@ What can I do with this? Simply put: It will export any traefik labels on a cont
|
||||
|
||||
In order to use this image, you need to setup Traefik with a Redis provider and then point this image via REDIS_URL to the same Redis instance. Each entry will have an expire timer set in Redis, so that if a container is removed by a server crashing, Redis will automatically remove stale entries as well. Entries are refreshed every 60 seconds or on all docker container events (create, run, kill, stop, restart, ...).
|
||||
|
||||
This image provides the ability to call a webhook for each container for each event or poll after the data was updates in Redis.
|
||||
|
||||
## Run
|
||||
This will export all labels from all containers to the Redis instance specified in LABELS_REDIS_URL from the same host this container is running on.
|
||||
```shell
|
||||
docker run --name traefik-labels \
|
||||
-v /run/docker.sock:/run/docker.sock \
|
||||
@@ -16,6 +17,15 @@ docker run --name traefik-labels \
|
||||
-d 11notes/traefik-labels:[tag]
|
||||
```
|
||||
|
||||
```shell
|
||||
docker run --name traefik-labels \
|
||||
-v /run/docker.sock:/run/docker.sock \
|
||||
-e LABELS_REDIS_URL="rediss://foo:bar@10.127.198.254:6379/0" \
|
||||
-e LABELS_WEBHOOK="https://domain.com/traefik/labels" \
|
||||
-e LABELS_WEBHOOK_AUTH_BASIC="foo:bar" \
|
||||
-d 11notes/traefik-labels:[tag]
|
||||
```
|
||||
|
||||
This is a demo webserver that will start on :8080, all the traefik labels will be exportet to the Redis instance. They follow the exact same [syntax](https://doc.traefik.io/traefik/routing/providers/kv/) as for normal Redis and Traefik, just as labels.
|
||||
```shell
|
||||
docker run --name demo \
|
||||
@@ -39,9 +49,11 @@ docker run --name demo \
|
||||
## Environment
|
||||
| Parameter | Value | Default |
|
||||
| --- | --- | --- |
|
||||
| `LABELS_REDIS_URL` | the redis URL to connect, use rediss:// for SSL | redis:://localhost:6379/0 |
|
||||
| `LABELS_INTERVAL` | in what interval container information is pulled | 300 |
|
||||
| `LABELS_TIMEOUT` | how many seconds after an interval the keys should stay till they expire | 30 |
|
||||
| `LABELS_REDIS_URL` | the redis URL to connect, use rediss:// for SSL | rediss://localhost:6379/0 |
|
||||
| `LABELS_INTERVAL` | in what interval container information is pulled in seconds | 300 |
|
||||
| `LABELS_TIMEOUT` | how many seconds after an interval the keys should stay till they expire in seconds | 30 |
|
||||
| `LABELS_WEBHOOK` | URL to call on each event or poll for each container | |
|
||||
| `LABELS_WEBHOOK_AUTH_BASIC` | Basic authentication to use in the form of "username:password" for the webhook | |
|
||||
|
||||
## Parent image
|
||||
* [11notes/node:stable](https://github.com/11notes/docker-node)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
# :: Header
|
||||
FROM 11notes/node:stable
|
||||
ENV APP_VERSION=0.1.1
|
||||
ENV APP_ROOT=/labels
|
||||
|
||||
# :: Run
|
||||
|
||||
+49
-18
@@ -8,7 +8,7 @@ class Labels{
|
||||
#poll = false;
|
||||
|
||||
constructor(){
|
||||
this.#docker = new Docker({socketPath: '/run/docker.sock'});
|
||||
this.#docker = new Docker({socketPath:'/run/docker.sock'});
|
||||
}
|
||||
|
||||
#log(message, error){
|
||||
@@ -20,6 +20,10 @@ class Labels{
|
||||
}
|
||||
|
||||
async watch(){
|
||||
if('' !== process.env.LABELS_WEBHOOK){
|
||||
this.#log(`using webhook ${process.env.LABELS_WEBHOOK}`);
|
||||
}
|
||||
|
||||
this.#redis = await redis.createClient({
|
||||
url:process.env.LABELS_REDIS_URL,
|
||||
pingInterval:30000,
|
||||
@@ -30,7 +34,7 @@ class Labels{
|
||||
|
||||
this.#redis.connect();
|
||||
this.#redis.on('ready', ()=>{
|
||||
this.#log('connected to Redis');
|
||||
this.#log('successfully connected to redis database');
|
||||
(async() => {
|
||||
await this.dockerPoll();
|
||||
})();
|
||||
@@ -48,13 +52,16 @@ class Labels{
|
||||
|
||||
dockerEvents(){
|
||||
this.#docker.getEvents({}, (error, data) => {
|
||||
data.on('data', async(chunk) => {
|
||||
const event = JSON.parse(chunk.toString('utf8'));
|
||||
if(/Container/i.test(event?.Type) && /start|stop|restart|kill|die|destroy/i.test(event?.status)){
|
||||
this.#log(`new docker event for container ${event.id}`);
|
||||
await this.dockerInspect(event.id, event.status);
|
||||
}
|
||||
});
|
||||
if(error){
|
||||
this.#log(error, ERROR);
|
||||
}else{
|
||||
data.on('data', async(chunk) => {
|
||||
const event = JSON.parse(chunk.toString('utf8'));
|
||||
if(/Container/i.test(event?.Type) && /^(start|stop|restart|kill|die|destroy)$/i.test(event?.status)){
|
||||
await this.dockerInspect(event.id, event.status);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -65,7 +72,7 @@ class Labels{
|
||||
this.#log(`poll start (interval:${process.env.LABELS_INTERVAL})}`);
|
||||
this.#docker.listContainers((error, containers) => {
|
||||
containers.forEach(async(container) => {
|
||||
await this.dockerInspect(container.Id, 'start');
|
||||
await this.dockerInspect(container.Id);
|
||||
});
|
||||
});
|
||||
}catch(e){
|
||||
@@ -81,22 +88,46 @@ class Labels{
|
||||
return(new Promise((resolve, reject) => {
|
||||
const container = this.#docker.getContainer(id);
|
||||
container.inspect(async(error, data) => {
|
||||
|
||||
let update = false;
|
||||
const webHook = {event:status, labels:{}};
|
||||
const headers = {'Content-Type':'application/json'};
|
||||
|
||||
if('' !== process.env.LABELS_WEBHOOK_AUTH_BASIC){
|
||||
headers['Authorization'] = 'Basic ' + Buffer.from(process.env.LABELS_WEBHOOK_AUTH_BASIC).toString('base64')
|
||||
}
|
||||
|
||||
if(error){
|
||||
reject(error);
|
||||
}
|
||||
|
||||
this.#log(`inspect container ${data.Name.replace(/^\//i, '')}${(
|
||||
(null === status) ? '' : ` event[${status}]`
|
||||
)}`);
|
||||
|
||||
if(/start|restart/i.test(status)){
|
||||
update = true;
|
||||
}
|
||||
|
||||
for(const label in data?.Config?.Labels){
|
||||
if(/traefik\//i.test(label)){
|
||||
switch(true){
|
||||
case /start|restart/i.test(status):
|
||||
await this.#redis.set(label, data?.Config?.Labels[label], {EX:parseInt(process.env.LABELS_INTERVAL) + parseInt(process.env.LABELS_TIMEOUT)});
|
||||
break;
|
||||
|
||||
case /stop|kill|die|destroy/i.test(status):
|
||||
await this.#redis.del(label);
|
||||
break;
|
||||
webHook.labels[label] = data?.Config?.Labels[label];
|
||||
if(update){
|
||||
await this.#redis.set(label, data?.Config?.Labels[label], {EX:parseInt(process.env.LABELS_INTERVAL) + parseInt(process.env.LABELS_TIMEOUT)});
|
||||
}else{
|
||||
await this.#redis.del(label);
|
||||
}
|
||||
}
|
||||
}
|
||||
if('' !== process.env.LABELS_WEBHOOK){
|
||||
try{
|
||||
const webHookCall = await fetch(process.env.LABELS_WEBHOOK, {method:(
|
||||
(update) ? 'PUT' : 'DELETE'
|
||||
), body:JSON.stringify(webHook), headers:headers});
|
||||
}catch(e){
|
||||
this.#log(e, ERROR);
|
||||
}
|
||||
}
|
||||
resolve(true);
|
||||
});
|
||||
}));
|
||||
|
||||
@@ -3,9 +3,11 @@ process.once('SIGINT', () => process.exit(0));
|
||||
const { fork } = require('node:child_process');
|
||||
const child = fork(`${__dirname}/app.js`, [], {
|
||||
env:{
|
||||
LABELS_REDIS_URL:process.env?.LABELS_REDIS_URL || 'redis:://localhost:6379/0',
|
||||
LABELS_REDIS_URL:process.env?.LABELS_REDIS_URL || 'rediss://localhost:6379/0',
|
||||
LABELS_INTERVAL:parseInt(process.env?.LABELS_INTERVAL || 300),
|
||||
LABELS_TIMEOUT:parseInt(process.env?.LABELS_TIMEOUT|| 30),
|
||||
LABELS_WEBHOOK:process.env?.LABELS_WEBHOOK || '',
|
||||
LABELS_WEBHOOK_AUTH_BASIC:process.env?.LABELS_WEBHOOK_AUTH_BASIC || '',
|
||||
}
|
||||
});
|
||||
child.on('error', (error) =>{
|
||||
|
||||
Reference in New Issue
Block a user