in plugins/docker/base-image/src/main/java/co/elastic/gradle/dockerbase/DockerDaemonActions.java [177:238]
public String instructionAsDockerFileInstruction(ContainerImageBuildInstruction instruction) {
if (instruction instanceof From from) {
return "FROM " + from.getReference().get();
} else if (instruction instanceof final FromLocalImageBuild fromLocalImageBuild) {
return "# " + fromLocalImageBuild.otherProjectPath() + " (" + fromLocalImageBuild.getArchitecture() +")\n" +
"FROM " + fromLocalImageBuild.tag().get();
} else if (instruction instanceof Copy copySpec) {
return "COPY " + Optional.ofNullable(copySpec.getOwner()).map(s -> "--chown=" + s + " ").orElse("") +
workingDir.relativize(getContextDir().resolve(copySpec.getLayer())) + " /";
} else if (instruction instanceof Run run) {
String mountOptions = getBindMounts().entrySet().stream()
.map(entry -> {
// Key is something like: target=/mnt, additional options are possible
return "--mount=type=bind," + entry.getKey() +
",source=" + workingDir.relativize(entry.getValue());
}).collect(Collectors.joining(" "));
return "RUN " + mountOptions + "\\\n " +
String.join(" && \\ \n\t", run.getCommands());
} else if (instruction instanceof RepoConfigRun repoConfigRun) {
if (buildable.getIsolateFromExternalRepos().get()) {
return "";
} else {
return "RUN " + String.join(" && \\ \n\t", repoConfigRun.getCommands());
}
} else if (instruction instanceof RepoInstall repoInstall) {
if (buildable.getIsolateFromExternalRepos().get()) {
return "";
} else {
return convertInstallToRun(new Install(repoInstall.packages())).map(this::instructionAsDockerFileInstruction).collect(Collectors.joining("\n"));
}
} else if (instruction instanceof CreateUser createUser) {
// Specific case for Alpine and Busybox
return String.format(
"""
RUN if ! command -v busybox &> /dev/null; then \\\s
groupadd -g %4$s %3$s ; \\\s
useradd -r -s /bin/false -g %4$s --uid %2$s %1$s ; \\\s
else \\\s
addgroup --gid %4$s %3$s ; \\\s
adduser -S -s /bin/false --ingroup %3$s -H -D -u %2$s %1$s ; \\\s
fi
""",
createUser.getUsername(),
createUser.getUserId(),
createUser.getGroup(),
createUser.getGroupId()
);
} else if (instruction instanceof SetUser) {
user = ((SetUser) instruction).getUsername();
return "USER " + user;
} else if (instruction instanceof Env) {
return "ENV " + ((Env) instruction).getKey() + "=" + ((Env) instruction).getValue();
} else if (instruction instanceof HealthCheck healthcheck) {
return "HEALTHCHECK " + Optional.ofNullable(healthcheck.getInterval()).map(interval -> "--interval=" + interval + " ").orElse("") +
Optional.ofNullable(healthcheck.getTimeout()).map(timeout -> "--timeout=" + timeout + " ").orElse("") +
Optional.ofNullable(healthcheck.getStartPeriod()).map(startPeriod -> "--start-period=" + startPeriod + " ").orElse("") +
Optional.ofNullable(healthcheck.getRetries()).map(retries -> "--retries=" + retries + " ").orElse("") +
"CMD " + healthcheck.getCmd();
} else {
throw new GradleException("Docker instruction " + instruction + " is not supported for Docker daemon build");
}
}