Stay organized with collections
Save and categorize content based on your preferences.
This page describes a few things you need to know to get started in developing
a service for Knative serving.
Code requirements
You must meet the following requirements when you develop a service:
The service must listen for requests.
You can configure the port
on which requests are sent.
Inside Knative serving container instances, the value of the PORT
environment variable always reflects the port to which requests are sent.
Your code should check for the existence of this PORT environment
variable and if it is present, should listen on it to maximize portability.
The service must be stateless. It cannot rely on a persistent local state.
The service must not perform background activities outside the scope of request
handling.
Knative serving allows you to write code in the programming language of
your choice.
Using a web server
You can use a web server to listen on the required port, and to process and
route incoming requests. For example, Node.js developers can use
Express.js, Python developers can use
Flask, Ruby developers can use
Sinatra, and so forth.
Containerizing your code
To deploy to Knative serving, you need to provide a container image.
A container image is a packaging format that includes your code, its
packages, any needed binary dependencies, the operating system to use, and
anything else needed to run your service.
A file named Dockerfile is
commonly used to declare how to build the container image.
Dockerfiles very often start from a base image (e.g. FROM golang:1.11).
You can find base images maintained by OS and language authors on
Docker Hub. Cloud Build checks for cached images before pulling from Docker Hub. If you use a third-party build
tool, you can configure your Docker daemon to check for images in the same
cache. You can also find base images managed by Google in the
Google Cloud Marketplace.
If you bring your own binaries, make sure they are compiled for Linux ABI x86_64.
These resources provide further information on Dockerfiles:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-25 UTC."],[[["\u003cp\u003eKnative services must listen for requests on a specified port, and the \u003ccode\u003ePORT\u003c/code\u003e environment variable should be checked to ensure maximum portability.\u003c/p\u003e\n"],["\u003cp\u003eServices deployed to Knative must be stateless, meaning they cannot rely on a persistent local state.\u003c/p\u003e\n"],["\u003cp\u003eKnative serving supports development in any programming language, and web servers like Express.js, Flask, or Sinatra can be used to manage requests.\u003c/p\u003e\n"],["\u003cp\u003eDeployment to Knative serving requires a container image, often built using a Dockerfile, which packages the code, dependencies, and the operating system needed to run the service.\u003c/p\u003e\n"],["\u003cp\u003eUsing a base image like those found on docker hub is a common practice when creating a Dockerfile.\u003c/p\u003e\n"]]],[],null,["# Developing your service\n\nThis page describes a few things you need to know to get started in developing\na service for Knative serving.\n\nCode requirements\n-----------------\n\nYou must meet the following requirements when you develop a service:\n\n- The service must listen for requests. You can [configure the port](/anthos/run/archive/docs/configuring/containers#configure-port) on which requests are sent. Inside Knative serving container instances, the value of the `PORT` environment variable always reflects the port to which requests are sent. Your code should check for the existence of this `PORT` environment variable and if it is present, should listen on it to maximize portability.\n- The service must be *stateless* . It cannot rely on a persistent **local** state.\n- The service must not perform background activities outside the scope of request handling.\n\nYou can find more details about these constraints in the [Container Runtime Contract](/anthos/run/archive/docs/reference/container-contract).\n\nProgramming language support\n----------------------------\n\nKnative serving allows you to write code in the programming language of\nyour choice.\n\nUsing a web server\n------------------\n\nYou can use a web server to listen on the required port, and to process and\nroute incoming requests. For example, Node.js developers can use\n[Express.js](https://expressjs.com/), Python developers can use\n[Flask](http://flask.pocoo.org/), Ruby developers can use\n[Sinatra](http://sinatrarb.com/), and so forth.\n\nContainerizing your code\n------------------------\n\nTo deploy to Knative serving, you need to provide a *container image*.\nA container image is a packaging format that includes your code, its\npackages, any needed binary dependencies, the operating system to use, and\nanything else needed to run your service.\n\nA file named [Dockerfile](https://docs.docker.com/engine/reference/builder/) is\ncommonly used to declare how to build the container image.\n\nDockerfiles very often start from a base image (e.g. `FROM golang:1.11`).\nYou can find base images maintained by OS and language authors on\n[Docker Hub](https://hub.docker.com/). Cloud Build checks for [cached images](/container-registry/docs/pulling-cached-images) before pulling from Docker Hub. If you use a third-party build\ntool, you can configure your Docker daemon to check for images in the same\ncache. You can also find base images managed by Google in the\n[Google Cloud Marketplace](https://console.cloud.google.com/marketplace/browse?filter=solution-type:container&filter=category:os).\n| **Note:** As of November 1, 2020, Docker Hub\n| [rate limits](https://www.docker.com/blog/scaling-docker-to-serve-millions-more-developers-network-egress/) apply to unauthenticated or authenticated pull requests on the\n| Docker Free plan. To avoid disruptions and have greater control over your\n| software supply chain, you can migrate your dependencies to [Artifact Registry](/artifact-registry/docs/docker/migrate-external-containers).\n\nIf you bring your own binaries, make sure they are compiled for Linux ABI x86_64.\n\nThese resources provide further information on Dockerfiles:\n\n- Learn Dockerfile syntax through the [Dockerfile reference](https://docs.docker.com/engine/reference/builder).\n- Learn how Dockerfiles fit together through the tips in [Best practices for writing Dockerfiles](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/).\n\nWhat's next\n-----------\n\n- Once you have your service code and Dockerfile, you should [build a container image](/anthos/run/archive/docs/building/containers) then continue iterating in [local testing](/anthos/run/archive/docs/testing/local).\n- If you are migrating an existing web application, see [Migrating Your Service to Knative serving](/anthos/run/archive/docs/migrating).\n- For best practices for designing, implementing, testing, and deploying a service in Knative serving, see the [Development tips](/anthos/run/archive/docs/tips/general)."]]