sparse-intern-71089
03/08/2020, 6:51 PMwhite-balloon-205
glamorous-printer-66548
03/09/2020, 3:19 AMimport { execSync } from "child_process";
const bash = (cmd: string, cwd?: string) => execSync(cmd, { encoding: "utf8", stdio: "inherit", cwd });
const bashSilent = (cmd: string) => execSync(cmd, { encoding: "utf8" });
const gitSha1 = process.env.GIT_SHA1 ?? bashSilent("git rev-parse HEAD").trim();
import * as pulumi from "@pulumi/pulumi";
import { once } from "remeda";
import { baseImageRef } from "../src/constants";
function buildImageLocally() {
const imageRef = baseImageRef + `:built-from-local`;
bash(`DOCKER_BUILDKIT=1 docker build . -t ${imageRef}`, __dirname + "/../../..");
bash(`docker push ${imageRef}`);
const pinnedImageRef = bashSilent(`docker inspect --format='{{index .RepoDigests 0}}' ${imageRef}`);
return pinnedImageRef.trim();
}
/*
This determines the docker image ref to inject into the kubernetes resources.
There are basically 3 cases:
1. CIRCLE_SHA1 environment variable is set which means this deploy runs in circleci. We then just use the CIRCLE_SHA1 to construct an image ref which points to the image built already by circleci.
2. SKIP_BUILD is set which means the user explicitly wanted to skip local image build. We can then use then construct an image ref using the gitSha1 of the latest HEAD commit.
Note this doesn't work well if you have changed the application code locally since it won't create a new docker image containing your changes.
3. Nothing is set. If this is a dry-run (i.e. pulumi preview) we only return a preliminary image ref. If this is not a dry run we try to build and push and image ad-hoc by running a few docker commands.
*/
export const getDockerImageRef = once(() => {
if (process.env.CIRCLE_SHA1) {
return baseImageRef + ":" + process.env.CIRCLE_SHA1;
} else if (process.env.SKIP_BUILD) {
return baseImageRef + ":" + gitSha1;
} else if (pulumi.runtime.isDryRun()) {
return `${baseImageRef}@sha256:<computed value>`;
} else {
return buildImageLocally();
}
});
The places where I use need to use the image I then simply call getDockerImageRef()
and it's doing the right thing for me. Should be easy to modify to skip the push step etc.brief-alligator-51254
03/09/2020, 8:13 AMdocker build
involved.
@glamorous-printer-66548 I will give your code a try. Would you have some usage example to share? 🙂glamorous-printer-66548
03/09/2020, 8:43 AMgetDockerImageRef()
on the image
property of a k8s deployment template. i.e. image: getDockerImageRef()
. I only build, but not run the image locally although that should be relatively easy to do as well. What's your use-case actually?brief-alligator-51254
03/09/2020, 11:29 AMdocker.Container
requires a registry:
const image = new docker.Image(
`${name}-image`,
{
imageName: `${name}-image`,
build: {
args: args.buildArgs,
context: args.context,
dockerfile: args.dockerfile,
},
skipPush: true,
},
)
const container = new docker.Container(
`${name}-container`,
{
image: image.imageName,
ports: [{ internal: 5000, external: args.port }],
},
)
brief-alligator-51254
03/09/2020, 4:49 PMnew docker.Container
using that image created with your code but without pushing it to registry, it will fail as wellbrief-alligator-51254
03/09/2020, 4:50 PMdocker.Container
that can't run thembrief-alligator-51254
03/09/2020, 5:27 PM@pulumi/docker
is just a wrapper around terraform's docker provider, correct?
According to https://www.terraform.io/docs/providers/docker/r/container.html there is no way to skip the pull of images there, even though in Ansible it's possible (https://docs.ansible.com/ansible/latest/modules/docker_container_module.html#parameter-pull).
Does it mean Pulumi can't support that feature either? 😥glamorous-printer-66548
03/09/2020, 5:41 PMbrief-alligator-51254
03/10/2020, 5:18 PM