Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to inline Dockerfile via dockerfileContent option. #307

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## HEAD (Unreleased)
* Add `dockerfileContent` parameter to `docker.Image` in the Node SDK.
* Add `DockerfileContent` parameter to `docker.Image` in the DotNet SDK.
* Add `DockerfileContent` parameter to `docker.Image` in the Python SDK.
* Add `dockerfile_content` parameter to `docker.Image` in the Golang SDK.
* Fix handling of the `cache_from` parameter in `docker.Image` in the Python SDK.
* Fix handling of the `cache_from` parameter in `docker.Image` in the Golang SDK.

Expand Down
22 changes: 20 additions & 2 deletions sdk/dotnet/Docker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public class DockerBuild : ResourceArgs
/// </summary>
[Input("target")]
public Input<string>? Target { get; set; }

/// <summary>
/// is the content of the dockerfile to build
/// example `FROM node`.
/// </summary>
[Input("dockerfileContent")]
public Input<string>? DockerfileContent { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -423,16 +430,27 @@ private static async Task RunDockerBuild(
{
buildArgs.AddRange(build.ExtraOptions);
}
buildArgs.Add(build.Context!); // push the docker build context onto the path.

if (build.DockerfileContent == null)
{
buildArgs.Add(build.Context!);
}

// push the docker build context onto the path.

buildArgs.AddRange(new[] { "-t", imageName }); // tag the image with the chosen name.
if (target != null)
{
buildArgs.AddRange(new[] { "--target", target });
}

if (build.DockerfileContent != null)
{
buildArgs.Add("-");
}

await RunCommandThatMustSucceed("docker", buildArgs.ToArray(), logResource,
reportFullCommandLine: true, stdin: null, build.Env).ConfigureAwait(false);
reportFullCommandLine: true, stdin: build.DockerfileContent, build.Env).ConfigureAwait(false);
}

private static readonly ConcurrentDictionary<(string, string), Task> loginResults = new ConcurrentDictionary<(string, string), Task>();
Expand Down
3 changes: 3 additions & 0 deletions sdk/go/docker/customTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ type DockerBuildArgs struct {

// The target of the dockerfile to build.
Target pulumi.StringInput `pulumi:"target"`

// The content of the dockerfile to build.
DockerfileContent pulumi.StringInput `pulumi:"dockerfileContent"`
}

// nolint:golint
Expand Down
11 changes: 9 additions & 2 deletions sdk/go/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,21 @@ func runDockerBuild(ctx *pulumi.Context, imageName string, build *DockerBuild, c
if build.ExtraOptions != nil {
buildArgs = append(buildArgs, build.ExtraOptions...)
}
// Push the docker build context onto the path.
buildArgs = append(buildArgs, build.Context)
if build.DockerfileContent == nil {
// Push the docker build context onto the path.
buildArgs = append(buildArgs, build.Context)
}

buildArgs = append(buildArgs, "-t", imageName)
if target != "" {
buildArgs = append(buildArgs, "--target", target)
}

if build.DockerfileContent != nil {
// Push the docker build context onto the path.
buildArgs = append(buildArgs, "-")
}

_, err := runCommandThatMustSucceed(ctx, "docker", buildArgs, logResource, true, "", build.Env)
return err
}
Expand Down
17 changes: 14 additions & 3 deletions sdk/nodejs/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ export interface DockerBuild {
* The target of the dockerfile to build
*/
target?: pulumi.Input<string>;

/***
* The content of the dockerfile to build
*/
dockerfileContent?: pulumi.Input<string>;
}

let dockerPasswordPromise: Promise<boolean> | undefined;
Expand Down Expand Up @@ -465,15 +470,21 @@ async function dockerBuild(
if (build.extraOptions) {
buildArgs.push(...build.extraOptions);
}
buildArgs.push(build.context!); // push the docker build context onto the path.
if(!build.dockerfileContent){
buildArgs.push(build.context!);
}

buildArgs.push(...["-t", imageName]); // tag the image with the chosen name.

if (target) {
buildArgs.push(...["--target", target]);
}

await runCommandThatMustSucceed("docker", buildArgs, logResource, undefined, undefined, build.env);
if(build.dockerfileContent){
buildArgs.push('-');
}

await runCommandThatMustSucceed("docker", buildArgs, logResource, undefined, build.dockerfileContent, build.env);
}

interface LoginResult {
Expand Down Expand Up @@ -728,4 +739,4 @@ async function runCommandThatCanFail(
resolve({ code, stdout });
}
});
}
}
18 changes: 14 additions & 4 deletions sdk/python/pulumi_docker/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ class DockerBuild:
The target of the dockerfile to build
"""

dockerfile_content: Optional[pulumi.Input[str]]
"""
The content of the dockerfile to build
"""

def __init__(self, context=None, dockerfile=None, args=None, cache_from=None, extra_options=None, env=None,
target=None):
target=None, dockerfile_content=None):
"""
DockerBuild may be used to specify detailed instructions about how to build a container.

Expand All @@ -143,6 +148,7 @@ def __init__(self, context=None, dockerfile=None, args=None, cache_from=None, ex
:param Optional[Mapping[str, str]] env: Environment variables to set on the invocation of `docker build`, for
example to support `DOCKER_BUILDKIT=1 docker build`.
:param Optional[pulumi.Input[str]] target: The target of the dockerfile to build
:param Optional[pulumi.Input[str]] dockerfile_content is the content of the dockerfile to build
"""
self.context = context
self.dockerfile = dockerfile
Expand Down Expand Up @@ -485,10 +491,14 @@ def docker_build(
if target:
build_args.extend(["--target", target])

if build.context:
build_args.append(build.context) # push the docker build context onto the path.
if not build.dockerfile_content:
if build.context:
build_args.append(build.context) # push the docker build context onto the path.

if build.dockerfile_content:
build_args.extend(['-'])

return run_command_that_must_succeed("docker", build_args, log_resource, env=build.env)
return run_command_that_must_succeed("docker", build_args, log_resource, stdin=build.dockerfile_content, env=build.env)


class LoginResult:
Expand Down