Skip to content

Commit

Permalink
add dependency on container network
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Nov 6, 2023
1 parent ee6651e commit d72b0ae
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
16 changes: 10 additions & 6 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (g *Generator) postProcessContainers(containers []*NixContainer) {
targetService := strings.Split(networkMode, ":")[1]
targetContainerName := serviceToContainer[targetService].Name
c.ExtraOptions = append(c.ExtraOptions, "--network=container:"+targetContainerName)
c.DependsOn = append(c.DependsOn, targetContainerName)
}

dependsOn := c.service.GetDependencies()
Expand Down Expand Up @@ -242,15 +243,13 @@ func (g *Generator) buildNixContainer(service types.ServiceConfig) (*NixContaine
c.EnvFiles = g.EnvFiles
}

for _, v := range service.Volumes {
c.Volumes[v.Source] = v.String()
}

for _, name := range c.Networks {
networkName := g.Project.With(name)
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--network=%s", networkName))
// Allow other containers to use service name as an alias when a project is set.
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--network-alias=%s", service.Name))
}

for _, v := range service.Volumes {
c.Volumes[v.Source] = v.String()
}

// https://docs.docker.com/compose/compose-file/compose-file-v3/#network_mode
Expand All @@ -261,8 +260,13 @@ func (g *Generator) buildNixContainer(service types.ServiceConfig) (*NixContaine
case strings.HasPrefix(networkMode, "container:"):
// container:[name] mode is supported by both Docker and Podman.
c.ExtraOptions = append(c.ExtraOptions, networkMode)
containerName := strings.TrimSpace(strings.Split(networkMode, ":")[1])
c.DependsOn = append(c.DependsOn, containerName)
}

// Allow other containers to use service name as an alias.
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--network-alias=%s", service.Name))

for _, ip := range service.DNS {
c.ExtraOptions = append(c.ExtraOptions, "--dns="+ip)
}
Expand Down
6 changes: 4 additions & 2 deletions testdata/TestDocker_WithProject_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
logDriver = "journald";
autoStart = false;
extraOptions = [
"--network=myproject_default"
"--network-alias=jellyseerr"
"--dns=1.1.1.1"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--network=container:myproject_sabnzbd"
];
};
systemd.services."docker-jellyseerr" = {
Expand Down Expand Up @@ -144,6 +144,9 @@
"traefik.http.routers.transmission.tls.certresolver" = "htpc";
"traefik.http.services.transmission.loadbalancer.server.port" = "9091";
};
dependsOn = [
"myproject_sabnzbd"
];
logDriver = "journald";
autoStart = false;
extraOptions = [
Expand Down Expand Up @@ -208,7 +211,6 @@
docker network inspect myproject_default || docker network create myproject_default
'';
wantedBy = [
"docker-jellyseerr.service"
"docker-myproject_sabnzbd.service"
"docker-photoprism-mariadb.service"
"docker-torrent-client.service"
Expand Down
6 changes: 4 additions & 2 deletions testdata/TestDocker_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
logDriver = "journald";
autoStart = false;
extraOptions = [
"--network=default"
"--network-alias=jellyseerr"
"--dns=1.1.1.1"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--network=container:sabnzbd"
];
};
systemd.services."docker-jellyseerr" = {
Expand Down Expand Up @@ -144,6 +144,9 @@
"traefik.http.routers.transmission.tls.certresolver" = "htpc";
"traefik.http.services.transmission.loadbalancer.server.port" = "9091";
};
dependsOn = [
"sabnzbd"
];
logDriver = "journald";
autoStart = false;
extraOptions = [
Expand Down Expand Up @@ -208,7 +211,6 @@
docker network inspect default || docker network create default
'';
wantedBy = [
"docker-jellyseerr.service"
"docker-photoprism-mariadb.service"
"docker-sabnzbd.service"
"docker-torrent-client.service"
Expand Down
6 changes: 4 additions & 2 deletions testdata/TestPodman_WithProject_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
logDriver = "journald";
autoStart = false;
extraOptions = [
"--network=myproject_default"
"--network-alias=jellyseerr"
"--dns=1.1.1.1"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--network=container:myproject_sabnzbd"
];
};
systemd.services."podman-jellyseerr" = {
Expand Down Expand Up @@ -149,6 +149,9 @@
"traefik.http.routers.transmission.tls.certresolver" = "htpc";
"traefik.http.services.transmission.loadbalancer.server.port" = "9091";
};
dependsOn = [
"myproject_sabnzbd"
];
logDriver = "journald";
autoStart = false;
extraOptions = [
Expand Down Expand Up @@ -213,7 +216,6 @@
podman network create myproject_default --opt isolate=true --ignore
'';
wantedBy = [
"podman-jellyseerr.service"
"podman-myproject_sabnzbd.service"
"podman-photoprism-mariadb.service"
"podman-torrent-client.service"
Expand Down
6 changes: 4 additions & 2 deletions testdata/TestPodman_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
logDriver = "journald";
autoStart = false;
extraOptions = [
"--network=default"
"--network-alias=jellyseerr"
"--dns=1.1.1.1"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--network=container:sabnzbd"
];
};
systemd.services."podman-jellyseerr" = {
Expand Down Expand Up @@ -149,6 +149,9 @@
"traefik.http.routers.transmission.tls.certresolver" = "htpc";
"traefik.http.services.transmission.loadbalancer.server.port" = "9091";
};
dependsOn = [
"sabnzbd"
];
logDriver = "journald";
autoStart = false;
extraOptions = [
Expand Down Expand Up @@ -213,7 +216,6 @@
podman network create default --opt isolate=true --ignore
'';
wantedBy = [
"podman-jellyseerr.service"
"podman-photoprism-mariadb.service"
"podman-sabnzbd.service"
"podman-torrent-client.service"
Expand Down
5 changes: 3 additions & 2 deletions testdata/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ services:
- /var/volumes/transmission/config:/config
- /var/volumes/transmission/scripts:/scripts
- storage:/storage
depends_on:
- sabnzbd
environment:
TZ: ${TIMEZONE}
PUID: ${PUID}
Expand Down Expand Up @@ -96,8 +98,7 @@ services:
- "traefik.http.routers.jellyseerr.rule=Host(`requests.${DOMAIN}`)"
- "traefik.http.routers.jellyseerr.tls.certresolver=htpc"
- "traefik.http.routers.jellyseerr.middlewares=chain-authelia@file"
depends_on:
- sabnzbd
network_mode: "service:sabnzbd"
logging:
driver: "json-file"
options:
Expand Down

0 comments on commit d72b0ae

Please sign in to comment.