Skip to content

Commit

Permalink
Adds Docker Build provider examples using Docker Build Cloud (#1633)
Browse files Browse the repository at this point in the history
Adds two new examples using the Docker Build provider to build a Docker
image using Docker Build Cloud (DBC) in Typescript.

`dockerbuildcloud-ts` - Builds a Docker Image from an NGINX local
Dockerfile. This template prompts the user for an existing DBC builder.

`aws-ts-containers-dockerbuildcloud` - This is a slight variation of the
existing `aws-ts-containers` but with the new Docker Build provider and
Docker Build Cloud.

These examples are (will be) referenced in an upcoming blogpost.

---------

Co-authored-by: diana esteves <[email protected]>
Co-authored-by: Josh Kodroff <[email protected]>
  • Loading branch information
3 people authored May 13, 2024
1 parent 2f3ac94 commit 5817bbe
Show file tree
Hide file tree
Showing 14 changed files with 383 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ Gopkg.lock
.project
.classpath
target/
*-test/
12 changes: 12 additions & 0 deletions aws-ts-containers-dockerbuildcloud/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: ${PROJECT}
description: ${DESCRIPTION}
runtime: nodejs

template:
description: An example that builds a Docker image in DBC and deploys a container to AWS Fargate.
config:
builder:
description: Your existing Docker Build Cloud builder. (e.g., cloud-pulumi-my-cool-builder)
aws:region:
description: The AWS region to deploy into
default: us-west-2
54 changes: 54 additions & 0 deletions aws-ts-containers-dockerbuildcloud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Deploy a container with a DBC-built image on AWS Fargate

Deploys a AWS Fargate service. The service uses a Docker image that is build with Docker Build Cloud (DBC). The image is pushed to AWS ECR. This template prompts the user for an existing DBC builder.

Last revision: May 2024.

## 📋 Pre-requisites

- [Docker Build Cloud (DBC) builder](https://build.docker.com/)
- 🚨 You **must** complete the [DBC builder setup steps](https://docs.docker.com/build/cloud/setup/#steps) 🚨
- Docker Desktop / CLI
- [Pulumi CLI](https://www.pulumi.com/docs/get-started/install/)
- *Recommended* [Pulumi Cloud account](https://app.pulumi.com/signup)
- [npm](https://www.npmjs.com/get-npm)
- AWS account and local credentials configured

## 👩‍🏫 Get started

This Pulumi example is written as a template. It is meant to be copied via `pulumi new` as follows:

```bash
$ pulumi new https://github.com/pulumi/examples/tree/master/aws-ts-containers-dbc
$ npm install
```

Once copied to your machine, feel free to edit as needed.

Alternatively, click the button below to use [Pulumi Deployments](https://www.pulumi.com/docs/pulumi-cloud/deployments/) to deploy this app:

[![Deploy](../.buttons/deploy-with-pulumi-dark.svg)](https://app.pulumi.com/new?template=https://github.com/pulumi/examples/blob/master/aws-ts-containers-dbc)
[![Deploy](../.buttons/deploy-with-pulumi-light.svg)](https://app.pulumi.com/new?template=https://github.com/pulumi/examples/blob/master/aws-ts-containers-dbc)

## 🎬 How to run

To deploy your infrastructure, run:

```bash
$ pulumi up
# select 'yes' to confirm the expected changes
# wait a bit for everything to get deployed
# ...
# confirm your service is up and running
$ curl $(pulumi stack output url)
# 🎉 Ta-Da!
```

## 🧹 Clean up

To clean up your infrastructure, run:

```bash
$ pulumi destroy
# select 'yes' to confirm the expected changes
```
2 changes: 2 additions & 0 deletions aws-ts-containers-dockerbuildcloud/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM nginx
COPY content /usr/share/nginx/html
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions aws-ts-containers-dockerbuildcloud/app/content/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head><meta charset="UTF-8">
<title>Hello, Pulumi!</title></head>
<body>
<p>Hello, containers!</p>
<p>Made with ❤️ with <a href="https://pulumi.com">Pulumi</a></p>
</body></html>
151 changes: 151 additions & 0 deletions aws-ts-containers-dockerbuildcloud/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright 2024, Pulumi Corporation. All rights reserved.

// Pulumi program to build with DBC and push a Docker image
// to AWS ECR and deploys it in AWS Fargate with an ALB.

// Pre-requisites:
// - AWS Credentials
// - Docker Build Cloud (DBC)
// - Docker Desktop / CLI
// - Pulumi CLI (https://www.pulumi.com/docs/get-started/install/)
// - *Recommended* Pulumi Cloud account (https://app.pulumi.com/signup)

// This Pulumi template is meant to be copied via:
// $ pulumi new https://github.com/pulumi/examples/tree/master/aws-ts-containers-dbc
// Once copied to your machine, feel free to edit as needed.

// As a good practice, update any dependencies to the latest version.
// $ npm update --save

// How to run this program in your terminal:
// $ pulumi up

// Import required libraries, update package.json if you add more.
// (Recommended to run `npm update --save` after adding more libraries)
import * as aws from "@pulumi/aws"; // Required for ECS
import * as awsx from "@pulumi/awsx";
import * as dockerBuild from "@pulumi/docker-build";
import * as pulumi from "@pulumi/pulumi"; // Required for Config and interpolation

// Read the current stack configuration, see Pulumi.<STACK>.yaml file
// Configuration values can be set with the pulumi config set command
// OR by editing the Pulumi.<STACK>.yaml file.
// OR during the pulumi new wizard.
const config = new pulumi.Config();
// Docker Build Cloud (DBC) builder name
const builder = config.require("builder"); // Example, "cloud-pulumi-my-cool-builder"

// An ECS cluster to deploy into.
const cluster = new aws.ecs.Cluster("cluster", {});

// An ALB to serve the container endpoint to the internet.
const loadbalancer = new awsx.lb.ApplicationLoadBalancer("loadbalancer", {});

// An ECR repository to store our application's container image.
const ecr = new awsx.ecr.Repository("repo", {
forceDelete: true,
});

// Grab auth credentials for ECR.
const auth = aws.ecr.getAuthorizationTokenOutput({
registryId: ecr.repository.registryId,
});

// Build and publish our application's container image from ./app to the ECR repository.
// This image will be built with Docker Build Cloud (DBC) and pushed to ECR.
// It uses the Docker Build provider
const image = new dockerBuild.Image("image", {
// ____ _ ____ _ _ _
// | _ \ ___ ___| | _____ _ __ | __ ) _ _(_) | __| |
// | | | |/ _ \ / __| |/ / _ \ '__| | _ \| | | | | |/ _` |
// | |_| | (_) | (__| < __/ | | |_) | |_| | | | (_| |
// |____/ \___/ \___|_|\_\___|_| |____/ \__,_|_|_|\__,_|
// / ___| | ___ _ _ __| |
// | | | |/ _ \| | | |/ _` |
// | |___| | (_) | |_| | (_| |
// \____|_|\___/ \__,_|\__,_|
// Enable exec to run a custom docker-buildx binary with support
// for Docker Build Cloud (DBC).
exec: true,
// Configures the name of your existing buildx builder to use.
builder: {
name: builder, // Example, "cloud-pulumi-my-cool-builder",
},
// _ _
// ___ __ _ ___| |__ (_)_ __ __ _
// / __/ _` |/ __| '_ \| | '_ \ / _` |
// | (_| (_| | (__| | | | | | | | (_| |
// \___\__,_|\___|_| |_|_|_| |_|\__, |
// |___/
// Use the pushed image as a cache source.
cacheFrom: [{
registry: {
ref: pulumi.interpolate`${ecr.repository.repositoryUrl}:cache`,
},
}],
cacheTo: [{
registry: {
imageManifest: true,
ociMediaTypes: true,
ref: pulumi.interpolate`${ecr.repository.repositoryUrl}:cache`,
},
}],
// (Learn more about interpolation with Pulumi)
// https://www.pulumi.com/docs/concepts/inputs-outputs/all/#using-string-interpolation

// _ _ _ _ _ __
// _ __ ___ _ _| | |_(_) _ __ | | __ _| |_ / _| ___ _ __ _ __ ___
// | '_ ` _ \| | | | | __| |_____| '_ \| |/ _` | __| |_ / _ \| '__| '_ ` _ \
// | | | | | | |_| | | |_| |_____| |_) | | (_| | |_| _| (_) | | | | | | | |
// |_| |_| |_|\__,_|_|\__|_| | .__/|_|\__,_|\__|_| \___/|_| |_| |_| |_|
// |_|
// Build multi-platforms
platforms: [
dockerBuild.Platform.Linux_amd64,
// add more as needed
],
// _ _
// _ __ ___ __ _(_)___| |_ _ __ _ _
// | '__/ _ \/ _` | / __| __| '__| | | |
// | | | __/ (_| | \__ \ |_| | | |_| |
// |_| \___|\__, |_|___/\__|_| \__, |
// |___/ |___/
push: true,
// Provide our ECR credentials.
registries: [{
address: ecr.repository.repositoryUrl,
password: auth.password,
username: auth.userName,
}],
//
// Other parameters
//
// Tag our image with our ECR repository's address.
tags: [pulumi.interpolate`${ecr.repository.repositoryUrl}:latest`],
// The Dockerfile resides in the app directory for this example.
context: {
location: "app",
},
});

// Deploy an ECS Service on Fargate to host the application container.
const service = new awsx.ecs.FargateService("service", {
cluster: cluster.arn,
assignPublicIp: true,
taskDefinitionArgs: {
container: {
name: "service-container",
image: image.ref,
cpu: 128,
memory: 512,
essential: true,
portMappings: [{
containerPort: 80,
targetGroup: loadbalancer.defaultTargetGroup,
}],
},
},
});

// The URL at which the container's HTTP endpoint will be available.
export const url = pulumi.interpolate`http://${loadbalancer.loadBalancer.dnsName}`;
13 changes: 13 additions & 0 deletions aws-ts-containers-dockerbuildcloud/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "aws-ts-containers-dockerbuildcloud",
"main": "index.ts",
"dependencies": {
"@pulumi/aws": "^6.33.1",
"@pulumi/awsx": "^2.9.0",
"@pulumi/docker-build": "^0.0.2",
"@pulumi/pulumi": "^3.115.2"
},
"devDependencies": {
"@types/node": "^20.12.11"
}
}
10 changes: 10 additions & 0 deletions dockerbuildcloud-ts/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Configure project names and descriptions with values obtained from `pulumi new`
name: ${PROJECT}
description: ${DESCRIPTION}
runtime: nodejs

template:
description: An example that builds a Dockerfile in Docker Build Cloud.
config:
builder:
description: Your existing (and configured) cloud builder. (e.g., cloud-pulumi-my-cool-builder)
48 changes: 48 additions & 0 deletions dockerbuildcloud-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Example to build a Docker image with Docker Build Cloud (DBC)

Builds a Docker Image from an NGINX local Dockerfile. This template prompts the user for an existing DBC builder.

Last revision: May 2024.

## 📋 Pre-requisites

- [Docker Build Cloud (DBC) builder](https://build.docker.com/)
- 🚨 You **must** complete the [DBC builder setup steps](https://docs.docker.com/build/cloud/setup/#steps) 🚨
- Docker Desktop / CLI
- [Pulumi CLI](https://www.pulumi.com/docs/get-started/install/)
- *Recommended* [Pulumi Cloud account](https://app.pulumi.com/signup)
- [npm](https://www.npmjs.com/get-npm)

## 👩‍🏫 Get started

This Pulumi example is written as a template. It is meant to be copied via `pulumi new` as follows:

```bash
$ pulumi new https://github.com/pulumi/examples/tree/master/dockerbuild-ts-dbc
$ npm install
```
Once copied to your machine, feel free to edit as needed.

Alternatively, click the button below to use [Pulumi Deployments](https://www.pulumi.com/docs/pulumi-cloud/deployments/) to deploy this app:

[![Deploy](../.buttons/deploy-with-pulumi-dark.svg)](https://app.pulumi.com/new?template=https://github.com/pulumi/examples/blob/master/dockerbuild-ts-dbc/README.md#gh-light-mode-only)
[![Deploy](../.buttons/deploy-with-pulumi-light.svg)](https://app.pulumi.com/new?template=https://github.com/pulumi/examples/blob/master/dockerbuild-ts-dbc/README.md#gh-dark-mode-only)

## 🎬 How to run

To deploy your infrastructure, run:

```bash
$ pulumi up
# select 'yes' to confirm the expected changes
# 🎉 Ta-Da!
```

## 🧹 Clean up

To clean up your infrastructure, run:

```bash
$ pulumi destroy
# select 'yes' to confirm the expected changes
```
1 change: 1 addition & 0 deletions dockerbuildcloud-ts/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM nginx
57 changes: 57 additions & 0 deletions dockerbuildcloud-ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2024, Pulumi Corporation. All rights reserved.

// Pulumi program to build a docker image with Docker Build Cloud (DBC)

// This Pulumi template is meant to be copied via:
// $ pulumi new https://github.com/pulumi/examples/tree/master/dockerbuild-ts-dbc
// Once copied to your machine, feel free to edit as needed.

// How to run this program in your terminal:
// $ pulumi up

// Pre-requisites:
// - Docker Build Cloud (DBC) builder (https://build.docker.com/)
// !! You *must* complete the DBC builder setup steps @ https://docs.docker.com/build/cloud/setup/#steps
// - Docker Desktop / CLI
// - Pulumi CLI (https://www.pulumi.com/docs/get-started/install/)
// - *Recommended* Pulumi Cloud account (https://app.pulumi.com/signup)
// - npm (https://www.npmjs.com/get-npm)


// Import required libraries, update package.json if you add more.
// (Recommended to run `npm update --save` after adding more libraries)
import * as dockerBuild from "@pulumi/docker-build";
import * as pulumi from "@pulumi/pulumi"; // Required for Config

// Read the current stack configuration, see Pulumi.<STACK>.yaml file
// Configuration values can be set with the pulumi config set command
// OR by editing the Pulumi.<STACK>.yaml file.
// OR during the pulumi new wizard.
const config = new pulumi.Config();
// Docker Build Cloud (DBC) builder name
const builder = config.require("builder"); // Example, "cloud-pulumi-my-cool-builder"

const image = new dockerBuild.Image("image", {
// Enable exec to run a custom docker-buildx binary with support
// for Docker Build Cloud (DBC).
exec: true,
// Configures the name of your existing buildx builder to use.
builder: {
name: builder, // Example, "cloud-pulumi-my-cool-builder",
},
push: false,
// Silence warning: "No exports were specified so the build
// will only remain in the local build cache."
exports: [{
cacheonly: {},
}],
//
// Other parameters
//
// Tag our image
tags: [`nginx:latest`],
// The Dockerfile resides in the app directory for this example.
context: {
location: "app",
},
});
11 changes: 11 additions & 0 deletions dockerbuildcloud-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "dockerbuildcloud-ts",
"main": "index.ts",
"devDependencies": {
"@types/node": "^20.12.11"
},
"dependencies": {
"@pulumi/docker-build": "^0.0.2",
"@pulumi/pulumi": "^3.115.2"
}
}
Loading

0 comments on commit 5817bbe

Please sign in to comment.