Quickstart for Endpoints on Compute Engine with Docker

This page shows you how to configure, deploy, and send requests against a sample API running in a Docker container in Google Compute Engine.

Before you begin

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Select or create a Cloud Platform project.

    Go to the Projects page

  3. Enable billing for your project.

    Enable billing

  4. Note the project ID, because you'll need it later.
  5. Install cURL for testing purposes. On Windows, this tutorial uses PowerShell's built in WebRequest support.
  6. Download the Google Cloud SDK.
  7. Update the Cloud SDK and install the Endpoints components.
    gcloud components update
  8. Make sure that Cloud SDK (gcloud) is authorized to access your data and services on Google Cloud Platform:
    gcloud auth login
    A new browser tab opens and you are prompted to choose an account.
  9. Set the default project to your project ID.
    gcloud config set project [YOUR-PROJECT-ID]

    Replace [YOUR-PROJECT-ID] with your project ID. Do not include the square brackets. If you have other Cloud Platform projects, and you want to use gcloud to manage them, see Managing Cloud SDK Configurations.

Getting the sample code

Java

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/java-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd java-docs-samples/endpoints/getting-started
Python

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/python-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd python-docs-samples/endpoints/getting-started
Go

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    go get -u -d github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
  2. Change to the directory that contains the sample code:
    cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
PHP

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/php-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd php-docs-samples/endpoints/getting-started
Ruby

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd ruby-docs-samples/endpoints/getting-started
NodeJS

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd nodejs-docs-samples/endpoints/getting-started

Configuring Endpoints

To configure Endpoints, replace YOUR-PROJECT-ID with your own project ID in the openapi.yaml configuration file:

Deploying the sample API

To deploy the sample application, invoke the following command:

    gcloud service-management deploy openapi.yaml

Creating a Compute Engine instance

To create a Compute Engine instance to run the sample API server and the required Extensible Service Proxy:

  1. Open the Create VM Instance page in the Google Cloud Platform Console.

    Create VM instance

  2. Check Allow HTTP traffic and Allow HTTPS traffic to the Firewall rules.

  3. Leave all other default values as they are.

  4. Click Create to create the instance.

  5. Make a note of the instance name, zone, and external IP address because you'll need them later. It can take a few minutes before the external IP address appears.

Running the API and Extensible Service Proxy in a Docker container

The Extensible Service Proxy is an nginx-based proxy that sits in front of your backend code. It processes incoming traffic to provide auth, API Key management, logging, and other Endpoints API Management features.

To install and run the sample API and the Extensible Service Proxy in a Docker container:

  1. Connect to your instance using SSH from the console.
    List instances and connect

    1. Click the SSH icon for your instance to open a terminal window into the instance:

    Connect to your instance using SSH

  2. Install Docker in the instance.

    sudo apt-get update
    sudo apt-get install docker.io
    
  3. Run the sample Echo server that serves the sample API:

Java

sudo docker run -d --name=echo gcr.io/google-samples/echo-java:1.0

Python

sudo docker run -d --name=echo gcr.io/google-samples/echo-python:1.0

Go

sudo docker run -d --name=echo gcr.io/google-samples/echo-go:1.0

PHP

sudo docker run -d --name=echo gcr.io/google-samples/echo-php:1.0

Ruby

sudo docker run -d --name=echo gcr.io/google-samples/echo-ruby:1.0

NodeJS

sudo docker run -d --name=echo gcr.io/google-samples/echo-node:1.0

  1. Display the service configuration ID and service name with the following command:
    gcloud service-management configs list --service=echo-api.endpoints.[PROJECT-ID].cloud.goog
    

    Replace [PROJECT-ID]with your project ID. Do not include the square brackets.

  2. Run the pre-packaged public Extensible Service Proxy Docker container:
    sudo docker run --name=esp -d -p 80:8080 --link=echo:echo gcr.io/endpoints-release/endpoints-runtime:1 -s [SERVICE_NAME] -v [SERVICE_CONFIG_ID] -a echo:8080
    

    Replace [SERVICE_NAME] and [SERVICE_CONFIG_ID] with the service name and service configuration ID returned from the previous step. Do not include the square brackets.

Sending a request to the sample API

After the sample API and the Extensible Service Proxy are running on the Compute Engine instance, you can send requests to the API.

To send a request to the API

  1. Create an API key in the API credentials page.

    Create an API Key.

    1. Click Create credentials, then select API key.
    2. Copy the key, then paste it into the following environment variable statement:

      • In Linux or Mac OS: export ENDPOINTS_KEY=AIza...
      • In Windows PowerShell: $Env:ENDPOINTS_KEY="AIza..."
  2. Send an HTTP request using curl or Invoke-WebRequest:

    • In Linux or Mac OS:
      curl -d '{"message":"hello world"}' -H "content-type:application/json" "http://[IP_ADDRESS]:80/echo?key=${ENDPOINTS_KEY}"
    • In Windows PowerShell:
      (Invoke-WebRequest -Method POST -Body '{"message": "hello world"}' -Headers @{"content-type"="application/json"} -URI "http://[IP_ADDRESS]:80/echo?key=$Env:ENDPOINTS_KEY").Content

    Replace [IP_ADDRESS] with the external IP address of your instance. Do not include the square brackets.

  3. Look at the activity graphs for your API in the Endpoints page.
    View Endpoints activity graphs
    It may take a few moments for the request to be reflected in the graphs.

  4. Look at the request logs for your API in the Logs Viewer page.
    View Endpoints request logs

You just deployed and tested an API in Cloud Endpoints!

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

See Deleting an API and API Instances for information on stopping the services used by this quickstart.

What's next

Send feedback about...

Cloud Endpoints