Pulumi Docker Build

0.0.15 · active · verified Thu Apr 16

Pulumi Docker Build is a Pulumi provider for building modern Docker images. It leverages Docker Buildx and BuildKit to provide advanced image building capabilities, including multi-platform builds and caching. The current version is 0.0.15, and it typically sees minor updates every few weeks to months, often reflecting updates to underlying Docker components or bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to build a Docker image using `pulumi-docker-build`. It creates a simple `Dockerfile` in the current directory, then defines an `Image` resource to build and optionally push the image to Docker Hub. Remember to replace 'yourusername' with your actual Docker Hub username and set `DOCKER_USERNAME` and `DOCKER_PASSWORD` environment variables for pushing.

import pulumi
import pulumi_dockerbuild as dockerbuild
import os

# For this quickstart, we'll create a simple Dockerfile temporarily.
# In a real project, this Dockerfile would be part of your source code.
with open("Dockerfile", "w") as f:
    f.write("""
FROM alpine:latest
RUN echo "Building with Pulumi Docker Build!" > /tmp/build-status.txt
CMD ["cat", "/tmp/build-status.txt"]
""")

# Define a Docker image resource. This will build the image.
# To push to Docker Hub, replace 'yourusername' with your Docker Hub username
# and ensure DOCKER_USERNAME and DOCKER_PASSWORD environment variables are set.
# For local builds only, you can omit the 'registry' argument.
image = dockerbuild.Image("my-first-image",
    context=".",
    dockerfile="Dockerfile",
    image_name=f"yourusername/my-first-image:v1.0.0", # IMPORTANT: Replace 'yourusername'
    registry=dockerbuild.ImageRegistryArgs(
        server="docker.io", # Default for Docker Hub
        username=os.environ.get("DOCKER_USERNAME", ""),
        password=os.environ.get("DOCKER_PASSWORD", ""),
    ),
    # If you want to export the image to a local tarball instead of pushing:
    # exports=[dockerbuild.ImageExportArgs(
    #     local=dockerbuild.ImageExportLocalArgs(
    #         dest="./my-first-image.tar"
    #     )
    # )]
)

# Export the resulting image name
pulumi.export("image_name", image.image_name)

# Note: Remember to delete the 'Dockerfile' created by this quickstart
# after you are done, as it's a temporary artifact for demonstration.

view raw JSON →