Enhancement: add string replacements to docker auto discovery labels

This commit is contained in:
Mikael Lofjärd 2024-12-23 17:28:14 +01:00
parent 2d91b2b748
commit 7f5cca2181
2 changed files with 32 additions and 0 deletions

View File

@ -136,6 +136,29 @@ When your Docker instance has been properly configured, this service will be aut
**When using docker swarm use _deploy/labels_** **When using docker swarm use _deploy/labels_**
### Replacement strings in labels
Homepage will substitue `${name}` and `${image}` with the container name and image respectively.
```yaml
services:
emby:
image: lscr.io/linuxserver/emby:latest
container_name: emby
ports:
- 8096:8096
restart: unless-stopped
labels:
- homepage.group=Media
- homepage.name=$${name} # Will be replaced with 'emby'
- homepage.icon=emby.png
- homepage.href=http://emby.home/
- homepage.description=$${image} # Will be replaced with 'lscr.io/linuxserver/emby:latest'
```
Note the double `$$` used in YAML. How to enter a `$` character might differ depending on you service configuration language.
## Widgets ## Widgets
You may also configure widgets, along with the standard service entry, again, using dot notation. You may also configure widgets, along with the standard service entry, again, using dot notation.

View File

@ -93,6 +93,12 @@ export async function servicesFromDocker() {
let constructedService = null; let constructedService = null;
const containerLabels = isSwarm ? shvl.get(container, "Spec.Labels") : container.Labels; const containerLabels = isSwarm ? shvl.get(container, "Spec.Labels") : container.Labels;
const containerName = isSwarm ? shvl.get(container, "Spec.Name") : container.Names[0]; const containerName = isSwarm ? shvl.get(container, "Spec.Name") : container.Names[0];
const containerImage = isSwarm ? shvl.get(container, "Spec.Image") : container.Image;
const replacements = {
name: containerName.replace(/^\//, ""),
image: containerImage
}
Object.keys(containerLabels).forEach((label) => { Object.keys(containerLabels).forEach((label) => {
if (label.startsWith("homepage.")) { if (label.startsWith("homepage.")) {
@ -114,6 +120,9 @@ export async function servicesFromDocker() {
if (value === "widget.version") { if (value === "widget.version") {
substitutedVal = parseInt(substitutedVal, 10); substitutedVal = parseInt(substitutedVal, 10);
} }
Object.keys(replacements).forEach((replacement) => {
substitutedVal = substitutedVal.replace(`\${${replacement}}`, replacements[replacement]);
});
shvl.set(constructedService, value, substitutedVal); shvl.set(constructedService, value, substitutedVal);
} }
}); });