Skip to content

Commit

Permalink
fix(build): prepend localhost to image for Podman
Browse files Browse the repository at this point in the history
Part of: #4
  • Loading branch information
aksiksi committed Oct 12, 2024
1 parent 7aad170 commit 7f8c853
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
29 changes: 22 additions & 7 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,15 +725,30 @@ func (g *Generator) parseServiceBuild(service types.ServiceConfig, c *NixContain
cx = path.Join(g.rootPath, cx)
}

tags := service.Build.Tags
var imageName string
if c.Image != "" {
// Always prepend the image name to the list of specified tags.
tags = slices.Insert(tags, 0, service.Image)
imageName = c.Image
} else {
// If no image is set on the service, we'll define an image name based
// on the container name.
imageName = fmt.Sprintf("compose2nix/%s", c.Name)
}

// Always use the image name as a tag.
tags := []string{imageName}
// Apply additional tags on top, prefixed with the image name.
for _, tag := range service.Build.Tags {
tags = append(tags, fmt.Sprintf("%s:%s", imageName, tag))
}

// Set the image on the container.
if g.Runtime == ContainerRuntimePodman {
// Podman automatically prepends a registry name of "localhost" to any
// tag we set.
//
// See: https://docs.podman.io/en/latest/markdown/podman-build.1.html#tag-t-imagename
c.Image = fmt.Sprintf("localhost/%s", imageName)
} else {
// If no image is set on the service, we'll add a custom tag and point
// the image to it.
imageName := fmt.Sprintf("compose2nix/%s", c.Name)
tags = slices.Insert(tags, 0, imageName)
c.Image = imageName
}

Expand Down
2 changes: 1 addition & 1 deletion testdata/TestBuildSpec.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
};
script = ''
cd /some/path
docker build -t compose2nix/test-museum -t latest -t non-latest --build-arg GIT_COMMIT=development-cluster .
docker build -t compose2nix/test-museum -t compose2nix/test-museum:latest -t compose2nix/test-museum:non-latest --build-arg GIT_COMMIT=development-cluster .
'';
};
systemd.services."docker-build-test-prefetcharr" = {
Expand Down
6 changes: 3 additions & 3 deletions testdata/TestBuildSpec.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Containers
virtualisation.oci-containers.containers."test-museum" = {
image = "compose2nix/test-museum";
image = "localhost/compose2nix/test-museum";
environment = {
"ENTE_CREDENTIALS_FILE" = "/credentials.yaml";
};
Expand Down Expand Up @@ -55,7 +55,7 @@
];
};
virtualisation.oci-containers.containers."test-prefetcharr" = {
image = "prefetcharr";
image = "localhost/prefetcharr";
environment = {
"JELLYFIN_URL" = "http://example.com/jellyfin";
};
Expand Down Expand Up @@ -132,7 +132,7 @@
};
script = ''
cd /some/path
podman build -t compose2nix/test-museum -t latest -t non-latest --build-arg GIT_COMMIT=development-cluster .
podman build -t compose2nix/test-museum -t compose2nix/test-museum:latest -t compose2nix/test-museum:non-latest --build-arg GIT_COMMIT=development-cluster .
'';
};
systemd.services."podman-build-test-prefetcharr" = {
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestBuildSpec_BuildEnabled.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Containers
virtualisation.oci-containers.containers."test-museum" = {
image = "compose2nix/test-museum";
image = "localhost/compose2nix/test-museum";
environment = {
"ENTE_CREDENTIALS_FILE" = "/credentials.yaml";
};
Expand Down

0 comments on commit 7f8c853

Please sign in to comment.