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.
- Install Cloud Code for VS Code or a JetBrains IDE.
- Follow the instructions for locally developing and debugging within your IDE.
- VS Code: Locally developing and debugging
- IntelliJ: Locally developing and debugging
Running locally using Docker
To test your container image locally using Docker:
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/myproject/my-image:latest
.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.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:
Refer to the instructions for setting up a service account key.
The following Docker run flags inject the credentials and configuration from your local system into the local container:
- Use the
--volume
(-v
) flag to inject the credential file into the container (assumes you have already set yourGOOGLE_APPLICATION_CREDENTIALS
environment variable on your machine):-v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/FILE_NAME.json:ro
- Use the
--environment
(-e
) flag to set theGOOGLE_APPLICATION_CREDENTIALS
variable inside the container:-e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/FILE_NAME.json
- Use the
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
shown in the example above is a reasonable location to place your credentials inside the container./tmp/keys/FILE_NAME.json
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.