Skip to content

Commit 5d61008

Browse files
authored
docs: add buildpacks/docker quickstart (#212)
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
1 parent 0bc1245 commit 5d61008

File tree

1 file changed

+93
-63
lines changed

1 file changed

+93
-63
lines changed

README.md

Lines changed: 93 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -36,91 +36,121 @@ handling logic.
3636

3737
> Watch [this video](https://youtu.be/yMEcyAkTliU?t=912) to learn more about the Node Functions Framework.
3838
39-
# Features
39+
## Features
4040

4141
- Spin up a local development server for quick testing
4242
- Invoke a function in response to a request
4343
- Automatically unmarshal events conforming to the
4444
[CloudEvents](https://cloudevents.io/) spec
4545
- Portable between serverless platforms
4646

47-
# Installation
47+
## Installation
4848

4949
Add the Functions Framework to your `package.json` file using `npm`.
5050

5151
```sh
5252
npm install @google-cloud/functions-framework
5353
```
5454

55-
# Quickstart: Hello, World on your local machine
55+
## Quickstarts
5656

57-
Create an `index.js` file with the following contents:
57+
### Quickstart: Hello, World on your local machine
5858

59-
```js
60-
exports.helloWorld = (req, res) => {
61-
res.send('Hello, World');
62-
};
63-
```
59+
1. Create an `index.js` file with the following contents:
6460

65-
Run the following command:
61+
```js
62+
exports.helloWorld = (req, res) => {
63+
res.send('Hello, World');
64+
};
65+
```
6666

67-
```sh
68-
npx @google-cloud/functions-framework --target=helloWorld
69-
```
67+
1. Run the following command:
7068

71-
Open http://localhost:8080/ in your browser and see _Hello, World_.
69+
```sh
70+
npx @google-cloud/functions-framework --target=helloWorld
71+
```
7272

73-
# Quickstart: Set up a new project
73+
1. Open http://localhost:8080/ in your browser and see _Hello, World_.
7474

75-
Create an `index.js` file with the following contents:
75+
### Quickstart: Set up a new project
7676

77-
```js
78-
exports.helloWorld = (req, res) => {
79-
res.send('Hello, World');
80-
};
81-
```
77+
1. Create a `package.json` file using `npm init`:
8278

83-
To run a function locally, first create a `package.json` file using `npm init`:
79+
```sh
80+
npm init
81+
```
8482

85-
```sh
86-
npm init
87-
```
83+
1. Create an `index.js` file with the following contents:
8884

89-
Now install the Functions Framework:
85+
```js
86+
exports.helloWorld = (req, res) => {
87+
res.send('Hello, World');
88+
};
89+
```
9090

91-
```sh
92-
npm install @google-cloud/functions-framework
93-
```
91+
1. Now install the Functions Framework:
9492

95-
Add a `start` script to `package.json`, with configuration passed via
96-
command-line arguments:
93+
```sh
94+
npm install @google-cloud/functions-framework
95+
```
9796

98-
```js
99-
"scripts": {
100-
"start": "functions-framework --target=helloWorld"
101-
}
102-
```
103-
104-
Use `npm start` to start the built-in local development server:
105-
106-
```sh
107-
npm start
108-
...
109-
Serving function...
110-
Function: helloWorld
111-
URL: http://localhost:8080/
112-
```
113-
114-
Send requests to this function using `curl` from another terminal window:
115-
116-
```sh
117-
curl localhost:8080
118-
# Output: Hello, World
119-
```
120-
121-
# Run your function on serverless platforms
97+
1. Add a `start` script to `package.json`, with configuration passed via
98+
command-line arguments:
12299

123-
## Google Cloud Functions
100+
```js
101+
"scripts": {
102+
"start": "functions-framework --target=helloWorld"
103+
}
104+
```
105+
106+
1. Use `npm start` to start the built-in local development server:
107+
108+
```sh
109+
npm start
110+
...
111+
Serving function...
112+
Function: helloWorld
113+
URL: http://localhost:8080/
114+
```
115+
116+
1. Send requests to this function using `curl` from another terminal window:
117+
118+
```sh
119+
curl localhost:8080
120+
# Output: Hello, World
121+
```
122+
123+
### Quickstart: Build a Deployable Container
124+
125+
1. Install [Docker](https://store.docker.com/search?type=edition&offering=community) and the [`pack` tool](https://buildpacks.io/docs/install-pack/).
126+
127+
1. Build a container from your function using the Functions [buildpacks](https://github.com/GoogleCloudPlatform/buildpacks):
128+
129+
```sh
130+
pack build \
131+
--builder gcr.io/buildpacks/builder:v1 \
132+
--env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \
133+
--env GOOGLE_FUNCTION_TARGET=helloWorld \
134+
my-first-function
135+
```
136+
137+
1. Start the built container:
138+
139+
```sh
140+
docker run --rm -p 8080:8080 my-first-function
141+
# Output: Serving function...
142+
```
143+
144+
1. Send requests to this function using `curl` from another terminal window:
145+
146+
```sh
147+
curl localhost:8080
148+
# Output: Hello, World!
149+
```
150+
151+
## Run your function on serverless platforms
152+
153+
### Google Cloud Functions
124154

125155
The
126156
[Node.js 10 runtime on Google Cloud Functions](https://cloud.google.com/functions/docs/concepts/nodejs-10-runtime)
@@ -132,17 +162,17 @@ After you've written your function, you can simply deploy it from your local
132162
machine using the `gcloud` command-line tool.
133163
[Check out the Cloud Functions quickstart](https://cloud.google.com/functions/docs/quickstart).
134164

135-
## Cloud Run/Cloud Run on GKE
165+
### Cloud Run/Cloud Run on GKE
136166

137167
Once you've written your function, added the Functions Framework and updated your `start` script in `package.json`, all that's left is to create a container image. [Check out the Cloud Run quickstart](https://cloud.google.com/run/docs/quickstarts/build-and-deploy) for Node.js to create a container image and deploy it to Cloud Run. You'll write a `Dockerfile` when you build your container. This `Dockerfile` allows you to specify exactly what goes into your container (including custom binaries, a specific operating system, and more).
138168

139169
If you want even more control over the environment, you can [deploy your container image to Cloud Run on GKE](https://cloud.google.com/run/docs/quickstarts/prebuilt-deploy-gke). With Cloud Run on GKE, you can run your function on a GKE cluster, which gives you additional control over the environment (including use of GPU-based instances, longer timeouts and more).
140170

141-
## Container environments based on Knative
171+
### Container environments based on Knative
142172

143173
Cloud Run and Cloud Run on GKE both implement the [Knative Serving API](https://www.knative.dev/docs/). The Functions Framework is designed to be compatible with Knative environments. Just build and deploy your container to a Knative environment.
144174

145-
# Configure the Functions Framework
175+
## Configure the Functions Framework
146176

147177
You can configure the Functions Framework using command-line flags or
148178
environment variables. If you specify both, the environment variable will be
@@ -164,7 +194,7 @@ For example:
164194
}
165195
```
166196
167-
# Enable Google Cloud Functions Events
197+
## Enable Google Cloud Functions Events
168198
169199
The Functions Framework can unmarshall incoming
170200
Google Cloud Functions [event](https://cloud.google.com/functions/docs/concepts/events-triggers#events) payloads to `data` and `context` objects.
@@ -186,7 +216,7 @@ For more details on this signature type, check out the Google Cloud Functions
186216
documentation on
187217
[background functions](https://cloud.google.com/functions/docs/writing/background#cloud_pubsub_example).
188218
189-
# Enable CloudEvents
219+
## Enable CloudEvents
190220
191221
The Functions Framework can unmarshall incoming
192222
[CloudEvents](http://cloudevents.io) payloads to a `cloudevent` object.
@@ -209,11 +239,11 @@ To enable CloudEvents, set the signature type to `cloudevent`. By default, the H
209239
210240
Learn how to use CloudEvents in this [guide](docs/cloudevents.md).
211241
212-
# Advanced Docs
242+
## Advanced Docs
213243
214244
More advanced guides and docs can be found in the [`docs/` folder](docs/).
215245
216-
# Contributing
246+
## Contributing
217247
218248
Contributions to this library are welcome and encouraged. See
219249
[CONTRIBUTING](CONTRIBUTING.md) for more information on how to get started.

0 commit comments

Comments
 (0)