Skip to content

Commit d1ca640

Browse files
lostpolarisSitansh Rajput
andauthored
fix: Add support for entrypoint and command (#122)
* fix: Add support for entrypoint and command https://www.docker.com/blog/docker-best-practices-choosing-between-run-cmd-and-entrypoint/ https://docs.podman.io/en/v5.0.1/markdown/podman-run.1.html#entrypoint-command-command-arg1 Podman is happy to take in a list for entrypoint, whereas Docker only wants the executable, with the rest being held within cmd. --------- Co-authored-by: Sitansh Rajput <me@lostpolaris>
1 parent b7e2b14 commit d1ca640

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

compose.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,21 @@ func (g *Generator) buildNixContainer(service types.ServiceConfig, networkMap ma
495495
c.Command = service.Command
496496
}
497497
if entrypoint := service.Entrypoint; !entrypoint.IsZero() {
498-
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--entrypoint=%s", sliceToStringArray(entrypoint)))
498+
// Docker's --entrypoint takes a single executable
499+
// Any extra argv after the executable belongs to CMD, so we prepend
500+
// it to the container's command, matching compose semantics.
501+
if g.Runtime == ContainerRuntimeDocker {
502+
if len(entrypoint) == 0 {
503+
c.ExtraOptions = append(c.ExtraOptions, "--entrypoint=")
504+
} else {
505+
c.ExtraOptions = append(c.ExtraOptions, "--entrypoint="+entrypoint[0])
506+
if len(entrypoint) > 1 {
507+
c.Command = slices.Concat(entrypoint[1:], c.Command)
508+
}
509+
}
510+
} else if g.Runtime == ContainerRuntimePodman {
511+
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--entrypoint=%s", sliceToStringArray(entrypoint)))
512+
}
499513
}
500514

501515
// Figure out explicit dependencies for this container.

testdata/TestCommandAndEntrypoint.docker.nix

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
# Containers
1414
virtualisation.oci-containers.containers."test-both" = {
1515
image = "nginx:latest";
16-
cmd = [ "ls" "-la" "\"escape me please\"" ];
16+
cmd = [ "-g" "daemon off;" "-c" "/etc/config/nginx/conf/nginx.conf" "ls" "-la" "\"escape me please\"" ];
1717
log-driver = "journald";
1818
autoStart = false;
1919
extraOptions = [
20-
"--entrypoint=[\"nginx\", \"-g\", \"daemon off;\", \"-c\", \"/etc/config/nginx/conf/nginx.conf\"]"
20+
"--entrypoint=nginx"
2121
"--network-alias=both"
2222
"--network=test_default"
2323
];
@@ -39,7 +39,7 @@
3939
log-driver = "journald";
4040
autoStart = false;
4141
extraOptions = [
42-
"--entrypoint=[]"
42+
"--entrypoint="
4343
"--network-alias=empty-command-and-entrypoint"
4444
"--network=test_default"
4545
];
@@ -77,10 +77,11 @@
7777
};
7878
virtualisation.oci-containers.containers."test-string" = {
7979
image = "nginx:latest";
80+
cmd = [ "bash" "/abc.sh" ];
8081
log-driver = "journald";
8182
autoStart = false;
8283
extraOptions = [
83-
"--entrypoint=[\"ENV_VAR=\${ABC}\", \"bash\", \"/abc.sh\"]"
84+
"--entrypoint=ENV_VAR=\${ABC}"
8485
"--network-alias=string"
8586
"--network=test_default"
8687
];

0 commit comments

Comments
 (0)