Skip to content

Commit aa82445

Browse files
authored
feat: supports any service name in swarm mode by looking internally the hostname (#3193)
1 parent bdf1958 commit aa82445

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

docs/guide/swarm-mode.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ networks:
3838
Note that the `DOZZLE_MODE` environment variable is set to `swarm`. This tells Dozzle to automatically discover other Dozzle instances in the swarm. The `overlay` network is used to create the mesh network between the different Dozzle instances.
3939

4040
> [!NOTE]
41-
> Due to implementation details, the name for the service must be exactly `dozzle`. This is because Dozzle uses the service name to discover other Dozzle instances in the swarm. In the future, it would make sense to query the Docker API for all services with the current container ID.
41+
> Due to implementation details, <strike>the name for the service must be exactly `dozzle`</strike>. This is no longer required starting with version `v8.2`. You can name the service anything you want. The service name is automatically detected by Dozzle using `com.docker.swarm.service.name` label.
4242

4343
## Setting up simple authentication in Swarm Mode
4444

examples/docker.swarm.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
2-
dozzle:
3-
image: amir20/dozzle:latest
2+
my-dozzle-service:
3+
image: amir20/dozzle:local-test
44
environment:
55
- DOZZLE_LEVEL=debug
66
- DOZZLE_MODE=swarm

internal/support/docker/swarm_client_manager.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"fmt"
77
"net"
8+
"os"
89
"sync"
910

1011
"github.com/amir20/dozzle/internal/agent"
@@ -23,6 +24,7 @@ type SwarmClientManager struct {
2324
subscribers *xsync.MapOf[context.Context, chan<- docker.Host]
2425
localClient docker.Client
2526
localIPs []string
27+
name string
2628
}
2729

2830
func localIPs() []string {
@@ -47,12 +49,27 @@ func NewSwarmClientManager(localClient docker.Client, certs tls.Certificate) *Sw
4749
localService := NewDockerClientService(localClient)
4850
clientMap[localClient.Host().ID] = localService
4951

52+
id, ok := os.LookupEnv("HOSTNAME")
53+
if !ok {
54+
log.Fatal("HOSTNAME environment variable not set when looking for swarm service name")
55+
}
56+
57+
container, err := localClient.FindContainer(id)
58+
if err != nil {
59+
log.Fatalf("error finding container %s: %v", id, err)
60+
}
61+
62+
serviceName := container.Labels["com.docker.swarm.service.name"]
63+
64+
log.Debugf("found swarm internal service name: %s", serviceName)
65+
5066
return &SwarmClientManager{
5167
localClient: localClient,
5268
clients: clientMap,
5369
certs: certs,
5470
subscribers: xsync.NewMapOf[context.Context, chan<- docker.Host](),
5571
localIPs: localIPs(),
72+
name: serviceName,
5673
}
5774
}
5875

@@ -67,13 +84,15 @@ func (m *SwarmClientManager) Subscribe(ctx context.Context, channel chan<- docke
6784

6885
func (m *SwarmClientManager) RetryAndList() ([]ClientService, []error) {
6986
m.mu.Lock()
70-
errors := make([]error, 0)
7187

72-
ips, err := net.LookupIP("tasks.dozzle")
88+
log.Debugf("looking up swarm services: tasks.%s", m.name)
89+
ips, err := net.LookupIP(fmt.Sprintf("tasks.%s", m.name))
7390

91+
errors := make([]error, 0)
7492
if err != nil {
7593
log.Fatalf("error looking up swarm services: %v", err)
7694
errors = append(errors, err)
95+
m.mu.Unlock()
7796
return m.List(), errors
7897
}
7998

0 commit comments

Comments
 (0)