Skip to content

Commit 5817bbe

Browse files
destevesdiana estevesjkodroff
authored
Adds Docker Build provider examples using Docker Build Cloud (#1633)
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]>
1 parent 2f3ac94 commit 5817bbe

File tree

14 files changed

+383
-0
lines changed

14 files changed

+383
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ Gopkg.lock
2929
.project
3030
.classpath
3131
target/
32+
*-test/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: ${PROJECT}
2+
description: ${DESCRIPTION}
3+
runtime: nodejs
4+
5+
template:
6+
description: An example that builds a Docker image in DBC and deploys a container to AWS Fargate.
7+
config:
8+
builder:
9+
description: Your existing Docker Build Cloud builder. (e.g., cloud-pulumi-my-cool-builder)
10+
aws:region:
11+
description: The AWS region to deploy into
12+
default: us-west-2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Deploy a container with a DBC-built image on AWS Fargate
2+
3+
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.
4+
5+
Last revision: May 2024.
6+
7+
## 📋 Pre-requisites
8+
9+
- [Docker Build Cloud (DBC) builder](https://build.docker.com/)
10+
- 🚨 You **must** complete the [DBC builder setup steps](https://docs.docker.com/build/cloud/setup/#steps) 🚨
11+
- Docker Desktop / CLI
12+
- [Pulumi CLI](https://www.pulumi.com/docs/get-started/install/)
13+
- *Recommended* [Pulumi Cloud account](https://app.pulumi.com/signup)
14+
- [npm](https://www.npmjs.com/get-npm)
15+
- AWS account and local credentials configured
16+
17+
## 👩‍🏫 Get started
18+
19+
This Pulumi example is written as a template. It is meant to be copied via `pulumi new` as follows:
20+
21+
```bash
22+
$ pulumi new https://github.com/pulumi/examples/tree/master/aws-ts-containers-dbc
23+
$ npm install
24+
```
25+
26+
Once copied to your machine, feel free to edit as needed.
27+
28+
Alternatively, click the button below to use [Pulumi Deployments](https://www.pulumi.com/docs/pulumi-cloud/deployments/) to deploy this app:
29+
30+
[![Deploy](../.buttons/deploy-with-pulumi-dark.svg)](https://app.pulumi.com/new?template=https://github.com/pulumi/examples/blob/master/aws-ts-containers-dbc)
31+
[![Deploy](../.buttons/deploy-with-pulumi-light.svg)](https://app.pulumi.com/new?template=https://github.com/pulumi/examples/blob/master/aws-ts-containers-dbc)
32+
33+
## 🎬 How to run
34+
35+
To deploy your infrastructure, run:
36+
37+
```bash
38+
$ pulumi up
39+
# select 'yes' to confirm the expected changes
40+
# wait a bit for everything to get deployed
41+
# ...
42+
# confirm your service is up and running
43+
$ curl $(pulumi stack output url)
44+
# 🎉 Ta-Da!
45+
```
46+
47+
## 🧹 Clean up
48+
49+
To clean up your infrastructure, run:
50+
51+
```bash
52+
$ pulumi destroy
53+
# select 'yes' to confirm the expected changes
54+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM nginx
2+
COPY content /usr/share/nginx/html
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<head><meta charset="UTF-8">
3+
<title>Hello, Pulumi!</title></head>
4+
<body>
5+
<p>Hello, containers!</p>
6+
<p>Made with ❤️ with <a href="https://pulumi.com">Pulumi</a></p>
7+
</body></html>
+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Copyright 2024, Pulumi Corporation. All rights reserved.
2+
3+
// Pulumi program to build with DBC and push a Docker image
4+
// to AWS ECR and deploys it in AWS Fargate with an ALB.
5+
6+
// Pre-requisites:
7+
// - AWS Credentials
8+
// - Docker Build Cloud (DBC)
9+
// - Docker Desktop / CLI
10+
// - Pulumi CLI (https://www.pulumi.com/docs/get-started/install/)
11+
// - *Recommended* Pulumi Cloud account (https://app.pulumi.com/signup)
12+
13+
// This Pulumi template is meant to be copied via:
14+
// $ pulumi new https://github.com/pulumi/examples/tree/master/aws-ts-containers-dbc
15+
// Once copied to your machine, feel free to edit as needed.
16+
17+
// As a good practice, update any dependencies to the latest version.
18+
// $ npm update --save
19+
20+
// How to run this program in your terminal:
21+
// $ pulumi up
22+
23+
// Import required libraries, update package.json if you add more.
24+
// (Recommended to run `npm update --save` after adding more libraries)
25+
import * as aws from "@pulumi/aws"; // Required for ECS
26+
import * as awsx from "@pulumi/awsx";
27+
import * as dockerBuild from "@pulumi/docker-build";
28+
import * as pulumi from "@pulumi/pulumi"; // Required for Config and interpolation
29+
30+
// Read the current stack configuration, see Pulumi.<STACK>.yaml file
31+
// Configuration values can be set with the pulumi config set command
32+
// OR by editing the Pulumi.<STACK>.yaml file.
33+
// OR during the pulumi new wizard.
34+
const config = new pulumi.Config();
35+
// Docker Build Cloud (DBC) builder name
36+
const builder = config.require("builder"); // Example, "cloud-pulumi-my-cool-builder"
37+
38+
// An ECS cluster to deploy into.
39+
const cluster = new aws.ecs.Cluster("cluster", {});
40+
41+
// An ALB to serve the container endpoint to the internet.
42+
const loadbalancer = new awsx.lb.ApplicationLoadBalancer("loadbalancer", {});
43+
44+
// An ECR repository to store our application's container image.
45+
const ecr = new awsx.ecr.Repository("repo", {
46+
forceDelete: true,
47+
});
48+
49+
// Grab auth credentials for ECR.
50+
const auth = aws.ecr.getAuthorizationTokenOutput({
51+
registryId: ecr.repository.registryId,
52+
});
53+
54+
// Build and publish our application's container image from ./app to the ECR repository.
55+
// This image will be built with Docker Build Cloud (DBC) and pushed to ECR.
56+
// It uses the Docker Build provider
57+
const image = new dockerBuild.Image("image", {
58+
// ____ _ ____ _ _ _
59+
// | _ \ ___ ___| | _____ _ __ | __ ) _ _(_) | __| |
60+
// | | | |/ _ \ / __| |/ / _ \ '__| | _ \| | | | | |/ _` |
61+
// | |_| | (_) | (__| < __/ | | |_) | |_| | | | (_| |
62+
// |____/ \___/ \___|_|\_\___|_| |____/ \__,_|_|_|\__,_|
63+
// / ___| | ___ _ _ __| |
64+
// | | | |/ _ \| | | |/ _` |
65+
// | |___| | (_) | |_| | (_| |
66+
// \____|_|\___/ \__,_|\__,_|
67+
// Enable exec to run a custom docker-buildx binary with support
68+
// for Docker Build Cloud (DBC).
69+
exec: true,
70+
// Configures the name of your existing buildx builder to use.
71+
builder: {
72+
name: builder, // Example, "cloud-pulumi-my-cool-builder",
73+
},
74+
// _ _
75+
// ___ __ _ ___| |__ (_)_ __ __ _
76+
// / __/ _` |/ __| '_ \| | '_ \ / _` |
77+
// | (_| (_| | (__| | | | | | | | (_| |
78+
// \___\__,_|\___|_| |_|_|_| |_|\__, |
79+
// |___/
80+
// Use the pushed image as a cache source.
81+
cacheFrom: [{
82+
registry: {
83+
ref: pulumi.interpolate`${ecr.repository.repositoryUrl}:cache`,
84+
},
85+
}],
86+
cacheTo: [{
87+
registry: {
88+
imageManifest: true,
89+
ociMediaTypes: true,
90+
ref: pulumi.interpolate`${ecr.repository.repositoryUrl}:cache`,
91+
},
92+
}],
93+
// (Learn more about interpolation with Pulumi)
94+
// https://www.pulumi.com/docs/concepts/inputs-outputs/all/#using-string-interpolation
95+
96+
// _ _ _ _ _ __
97+
// _ __ ___ _ _| | |_(_) _ __ | | __ _| |_ / _| ___ _ __ _ __ ___
98+
// | '_ ` _ \| | | | | __| |_____| '_ \| |/ _` | __| |_ / _ \| '__| '_ ` _ \
99+
// | | | | | | |_| | | |_| |_____| |_) | | (_| | |_| _| (_) | | | | | | | |
100+
// |_| |_| |_|\__,_|_|\__|_| | .__/|_|\__,_|\__|_| \___/|_| |_| |_| |_|
101+
// |_|
102+
// Build multi-platforms
103+
platforms: [
104+
dockerBuild.Platform.Linux_amd64,
105+
// add more as needed
106+
],
107+
// _ _
108+
// _ __ ___ __ _(_)___| |_ _ __ _ _
109+
// | '__/ _ \/ _` | / __| __| '__| | | |
110+
// | | | __/ (_| | \__ \ |_| | | |_| |
111+
// |_| \___|\__, |_|___/\__|_| \__, |
112+
// |___/ |___/
113+
push: true,
114+
// Provide our ECR credentials.
115+
registries: [{
116+
address: ecr.repository.repositoryUrl,
117+
password: auth.password,
118+
username: auth.userName,
119+
}],
120+
//
121+
// Other parameters
122+
//
123+
// Tag our image with our ECR repository's address.
124+
tags: [pulumi.interpolate`${ecr.repository.repositoryUrl}:latest`],
125+
// The Dockerfile resides in the app directory for this example.
126+
context: {
127+
location: "app",
128+
},
129+
});
130+
131+
// Deploy an ECS Service on Fargate to host the application container.
132+
const service = new awsx.ecs.FargateService("service", {
133+
cluster: cluster.arn,
134+
assignPublicIp: true,
135+
taskDefinitionArgs: {
136+
container: {
137+
name: "service-container",
138+
image: image.ref,
139+
cpu: 128,
140+
memory: 512,
141+
essential: true,
142+
portMappings: [{
143+
containerPort: 80,
144+
targetGroup: loadbalancer.defaultTargetGroup,
145+
}],
146+
},
147+
},
148+
});
149+
150+
// The URL at which the container's HTTP endpoint will be available.
151+
export const url = pulumi.interpolate`http://${loadbalancer.loadBalancer.dnsName}`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "aws-ts-containers-dockerbuildcloud",
3+
"main": "index.ts",
4+
"dependencies": {
5+
"@pulumi/aws": "^6.33.1",
6+
"@pulumi/awsx": "^2.9.0",
7+
"@pulumi/docker-build": "^0.0.2",
8+
"@pulumi/pulumi": "^3.115.2"
9+
},
10+
"devDependencies": {
11+
"@types/node": "^20.12.11"
12+
}
13+
}

dockerbuildcloud-ts/Pulumi.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Configure project names and descriptions with values obtained from `pulumi new`
2+
name: ${PROJECT}
3+
description: ${DESCRIPTION}
4+
runtime: nodejs
5+
6+
template:
7+
description: An example that builds a Dockerfile in Docker Build Cloud.
8+
config:
9+
builder:
10+
description: Your existing (and configured) cloud builder. (e.g., cloud-pulumi-my-cool-builder)

dockerbuildcloud-ts/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Example to build a Docker image with Docker Build Cloud (DBC)
2+
3+
Builds a Docker Image from an NGINX local Dockerfile. This template prompts the user for an existing DBC builder.
4+
5+
Last revision: May 2024.
6+
7+
## 📋 Pre-requisites
8+
9+
- [Docker Build Cloud (DBC) builder](https://build.docker.com/)
10+
- 🚨 You **must** complete the [DBC builder setup steps](https://docs.docker.com/build/cloud/setup/#steps) 🚨
11+
- Docker Desktop / CLI
12+
- [Pulumi CLI](https://www.pulumi.com/docs/get-started/install/)
13+
- *Recommended* [Pulumi Cloud account](https://app.pulumi.com/signup)
14+
- [npm](https://www.npmjs.com/get-npm)
15+
16+
## 👩‍🏫 Get started
17+
18+
This Pulumi example is written as a template. It is meant to be copied via `pulumi new` as follows:
19+
20+
```bash
21+
$ pulumi new https://github.com/pulumi/examples/tree/master/dockerbuild-ts-dbc
22+
$ npm install
23+
```
24+
Once copied to your machine, feel free to edit as needed.
25+
26+
Alternatively, click the button below to use [Pulumi Deployments](https://www.pulumi.com/docs/pulumi-cloud/deployments/) to deploy this app:
27+
28+
[![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)
29+
[![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)
30+
31+
## 🎬 How to run
32+
33+
To deploy your infrastructure, run:
34+
35+
```bash
36+
$ pulumi up
37+
# select 'yes' to confirm the expected changes
38+
# 🎉 Ta-Da!
39+
```
40+
41+
## 🧹 Clean up
42+
43+
To clean up your infrastructure, run:
44+
45+
```bash
46+
$ pulumi destroy
47+
# select 'yes' to confirm the expected changes
48+
```

dockerbuildcloud-ts/app/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM nginx

dockerbuildcloud-ts/index.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2024, Pulumi Corporation. All rights reserved.
2+
3+
// Pulumi program to build a docker image with Docker Build Cloud (DBC)
4+
5+
// This Pulumi template is meant to be copied via:
6+
// $ pulumi new https://github.com/pulumi/examples/tree/master/dockerbuild-ts-dbc
7+
// Once copied to your machine, feel free to edit as needed.
8+
9+
// How to run this program in your terminal:
10+
// $ pulumi up
11+
12+
// Pre-requisites:
13+
// - Docker Build Cloud (DBC) builder (https://build.docker.com/)
14+
// !! You *must* complete the DBC builder setup steps @ https://docs.docker.com/build/cloud/setup/#steps
15+
// - Docker Desktop / CLI
16+
// - Pulumi CLI (https://www.pulumi.com/docs/get-started/install/)
17+
// - *Recommended* Pulumi Cloud account (https://app.pulumi.com/signup)
18+
// - npm (https://www.npmjs.com/get-npm)
19+
20+
21+
// Import required libraries, update package.json if you add more.
22+
// (Recommended to run `npm update --save` after adding more libraries)
23+
import * as dockerBuild from "@pulumi/docker-build";
24+
import * as pulumi from "@pulumi/pulumi"; // Required for Config
25+
26+
// Read the current stack configuration, see Pulumi.<STACK>.yaml file
27+
// Configuration values can be set with the pulumi config set command
28+
// OR by editing the Pulumi.<STACK>.yaml file.
29+
// OR during the pulumi new wizard.
30+
const config = new pulumi.Config();
31+
// Docker Build Cloud (DBC) builder name
32+
const builder = config.require("builder"); // Example, "cloud-pulumi-my-cool-builder"
33+
34+
const image = new dockerBuild.Image("image", {
35+
// Enable exec to run a custom docker-buildx binary with support
36+
// for Docker Build Cloud (DBC).
37+
exec: true,
38+
// Configures the name of your existing buildx builder to use.
39+
builder: {
40+
name: builder, // Example, "cloud-pulumi-my-cool-builder",
41+
},
42+
push: false,
43+
// Silence warning: "No exports were specified so the build
44+
// will only remain in the local build cache."
45+
exports: [{
46+
cacheonly: {},
47+
}],
48+
//
49+
// Other parameters
50+
//
51+
// Tag our image
52+
tags: [`nginx:latest`],
53+
// The Dockerfile resides in the app directory for this example.
54+
context: {
55+
location: "app",
56+
},
57+
});

dockerbuildcloud-ts/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "dockerbuildcloud-ts",
3+
"main": "index.ts",
4+
"devDependencies": {
5+
"@types/node": "^20.12.11"
6+
},
7+
"dependencies": {
8+
"@pulumi/docker-build": "^0.0.2",
9+
"@pulumi/pulumi": "^3.115.2"
10+
}
11+
}

0 commit comments

Comments
 (0)