Configuring containers

Learn how to configure the container port and the container entrypoint command and arguments.

When Knative serving starts a container, it runs the image's default entrypoint command and default command arguments. If you want to override the image's default entrypoint and command arguments, you can use the command and args fields in the container configuration. The command field specifies the actual command run by the container. The args field specifies the arguments passed to that command.

Configuring the container port

Any configuration change leads to the creation of a new revision. Subsequent revisions will also automatically get this configuration setting unless you make explicit updates to change it.

The container should listen on the port defined by the PORT environment variable rather than a specific hardcoded port. However, if this is not possible, you can configure on which port requests are sent to the container:

Console

  1. Go to Knative serving in the Google Cloud console:

    Go to Knative serving

  2. Click Create Service if you are configuring a new service you are deploying to. If you are configuring an existing service, click on the service, then click Edit & Deploy New Revision.

  3. Under Advanced settings, click Container.

  4. Specify the port you want requests to be sent to, if not the default value of 8080. This also sets the PORT environment variable.

  5. Click Next to continue to the next section.

  6. In the Configure how this service is triggered section, select which connectivity you would like to use to invoke the service.

  7. Click Create to deploy the image to Knative serving and wait for the deployment to finish.

Command line

  • For existing services, update a port setting by running the gcloud run services update command with the following parameters:

    gcloud run services update SERVICE --port PORT

    Replace:

    • SERVICE with the name of the service.
    • PORT with the port to send requests to. Note that the default port is 8080.
  • For new services, set the port by running the gcloud run deploy command with the --port parameter:

    gcloud run deploy SERVICE --image=IMAGE_URL --port PORT

    Replace:

    • SERVICE with the name of the service.
    • IMAGE_URL with a reference to the container image, for example, gcr.io/cloudrun/hello.
    • PORT with the port to which you want to send requests. The default port is 8080.

YAML

You can download the configuration of an existing service into a YAML file with the gcloud run services describe command by using the --format=export flag. You can then modify that YAML file and deploy those changes with the gcloud run services replace command. You must ensure that you modify only the specified attributes.

  1. Download the configuration of your service into a file named service.yaml on local workspace:

    gcloud run services describe SERVICE --format export > service.yaml

    Replace SERVICE with the name of your Knative serving service.

  2. In your local file, update the containerPort: attribute:

    apiVersion: serving.knative.dev/v1
    kind: Service
    spec:
      template:
        spec:
          containers:
          - image: IMAGE_URL
            ports:
            - containerPort: PORT
    

    Replace

    • IMAGE_URL with a reference to the container image, for example, gcr.io/cloudrun/hello.
    • PORT with the port to which you want to send requests.
  3. Replace the service with its new configuration using the following command:

    gcloud run services replace service.yaml

Configuring the container entrypoint command and arguments

Any configuration change leads to the creation of a new revision. Subsequent revisions will also automatically get this configuration setting unless you make explicit updates to change it.

When Knative serving starts a container, it runs the image's default entrypoint command. To specify or override the existing entrypoint command and arguments that are defined in the container image, you can configure the command and args settings in the Knative serving container configuration.

You can choose to configure entrypoint commands, arguments, or both. The command that you specify in Knative serving overrides any entrypoint command that is defined in the container image. If you choose to specify only arguments, those arguments are passed to and run by the entrypoint command defined in the container image.

In Knative serving, you can configure entrypoint commands and arguments using the Google Cloud console, the Google Cloud CLI, or a YAML file when you deploy a new service, update an existing service, or deploy a revision:

Console

  1. Go to Knative serving in the Google Cloud console:

    Go to Knative serving

  2. Click Create Service if you are configuring a new service you are deploying to. If you are configuring an existing service, click on the service, then click Edit & Deploy New Revision.

  3. Under Advanced settings, click Container.

  4. Specify the entrypoint command and arguments that you want the container to run during startup.

  5. Click Next to continue to the next section.

  6. In the Configure how this service is triggered section, select which connectivity you would like to use to invoke the service.

  7. Click Create to deploy the image to Knative serving and wait for the deployment to finish.

Command line

Command parameter options

  • To specify an argument that contains a comma (,), you must escape each ARG with a different delimiter. For example, if you use @:
    --args "^@^arg,with,commas@anotherarg@ARG3..."
  • To specify several sets of key-value pairs, you can specify multiple parameters for readability. Example:
    [...]
    --args "ARG1" \
    --args "ARG2" \
    --args "ARG3"
  • To use equal signs (=) in your arguments, you must specify each argument using the following format:
    gcloud run services ...  \
      --args "--repo-allowlist=github.com/example/example_demo" \
      --args "--gh-webhook-secret=XX"

  • For existing services, update the entrypoint command by running the gcloud run services update command with the following parameters:

    gcloud run services update SERVICE --command COMMAND --args ARG1,ARG2,ARG-N

    Replace:

    • SERVICE with the name of the service.
    • Optional: COMMAND with the command that you want the container to run during startup.
    • Optional: ARG1 with one or more arguments for the command that is run during startup. Use a comma delimited list for multiple arguments. How to format your arguments.
  • For new services, set the entrypoint command by running the gcloud run deploy command with the --command parameter:

    gcloud run deploy SERVICE --image=IMAGE_URL --command COMMAND --args ARG1,ARG2,ARG-N

    Replace:

    • SERVICE with the name of the service.
    • IMAGE_URL with a reference to the container image, for example, gcr.io/cloudrun/hello.
    • Optional: COMMAND with the command that you want the container to run during startup.
    • Optional: ARG1 with one or more arguments for the command that is run during startup. Use a comma delimited list for multiple arguments. How to format your arguments.

YAML

You can download the configuration of an existing service into a YAML file with the gcloud run services describe command by using the --format=export flag. You can then modify that YAML file and deploy those changes with the gcloud run services replace command. You must ensure that you modify only the specified attributes.

  1. Download the configuration of your service into a file named service.yaml on local workspace:

    gcloud run services describe SERVICE --format export > service.yaml

    Replace SERVICE with the name of your Knative serving service.

  2. In your local file, update the command and args attributes:

    apiVersion: serving.knative.dev/v1
    kind: Service
    spec:
      template:
        spec:
          containers:
          - image: IMAGE_URL
            command:
            - COMMAND
            args:
            - "ARG1"
            - "ARG-N"
    

    Replace:

    • IMAGE_URL with a reference to the container image, for example, gcr.io/cloudrun/hello.
    • Optional: COMMAND with the command that you want the container to run during startup.
    • Optional: ARG1 with one or more arguments for the command that is run during startup. Use a comma delimited list for more than one argument.
  3. Replace the service with its new configuration using the following command:

    gcloud run services replace service.yaml