Local testing

During development, you can run and test your container image locally, prior to deploying. You can use Cloud Code or Docker installed locally to run and test locally, including running locally with access to Google Cloud services.

Running in a Knative serving emulator

The Cloud Code plugin for VS Code and JetBrains IDEs lets you locally run and debug your container image in a Knative serving emulator within your IDE. The emulator allows you configure an environment that is representative of your service running on Knative serving.

You can configure properties like CPU and memory allocation, specify environment variables, and set Cloud SQL database connections.

  1. Install Cloud Code for VS Code or a JetBrains IDE.
  2. Follow the instructions for locally developing and debugging within your IDE.

Running locally using Docker

To test your container image locally using Docker:

  1. Use the Docker command:

    PORT=8080 && docker run -p 9090:${PORT} -e PORT=${PORT} IMAGE_URL

    Replace IMAGE_URL with a reference to the container image, for example, gcr.io/cloudrun/hello.

    The PORT environment variable specifies the port your application will use to listen for HTTP or HTTPS requests. This is a requirement from the Container Runtime Contract. In this example, we use port 8080.

  2. Open http://localhost:9090 in your browser.

If you are new to working with containers, you may want to review the Docker Getting Started guide. To learn more about Docker commands, refer to the Docker documentation.

Running locally using Docker with access to Google Cloud services

If you are using Google Cloud client libraries to integrate your application with Google Cloud services, and have not yet secured those services to control external access, you can set up your local container to authenticate with Google Cloud services using Application Default Credentials.

To run locally:

  1. Refer to Getting Started with Authentication for instructions on generating, retrieving, and configuring your Service Account credentials.

  2. The following Docker run flags inject the credentials and configuration from your local system into the local container:

    1. Use the --volume (-v) flag to inject the credential file into the container (assumes you have already set your GOOGLE_APPLICATION_CREDENTIALS environment variable on your machine):
      -v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/FILE_NAME.json:ro
    2. Use the --environment (-e) flag to set the GOOGLE_APPLICATION_CREDENTIALS variable inside the container:
      -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/FILE_NAME.json
  3. Optionally, use this fully configured Docker run command:

    PORT=8080 && docker run \
    -p 9090:${PORT} \
    -e PORT=${PORT} \
    -e K_SERVICE=dev \
    -e K_CONFIGURATION=dev \
    -e K_REVISION=dev-00001 \
    -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/FILE_NAME.json \
    -v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/FILE_NAME.json:ro \
    IMAGE_URL

    Note that the path

    /tmp/keys/FILE_NAME.json
    shown in the example above is a reasonable location to place your credentials inside the container.

    However, other directory locations will also work. The crucial requirement is that the GOOGLE_APPLICATION_CREDENTIALS environment variable must match the bind mount location inside the container.

    Note also, that with some Google Cloud services, you may want to use an alternate configuration to isolate local troubleshooting from production performance and data.

What's next

To learn how to deploy your built containers, follow Deploying Services.