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](https://docs.docker.com/engine/reference/builder/#entrypoint)
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](https://console.cloud.google.com/kubernetes/run)
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`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/update.md.txt)
  command with the following parameters:

  ```bash
  gcloud run services update SERVICE --port PORT
  ```

  Replace:
  - <var translate="no">SERVICE</var> with the name of the service.
  - <var translate="no">PORT</var> 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`](https://docs.cloud.google.com/sdk/gcloud/reference/run/deploy.md.txt#--port)
  parameter:

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

  Replace:
  - <var translate="no">SERVICE</var> with the name of the service.
  - <var translate="no">IMAGE_URL</var> with a reference to the container image, for example, `gcr.io/cloudrun/hello`.
  - <var translate="no">PORT</var> with the port to which you want to send requests. The default port is `8080`.

### YAML

> [!CAUTION]
> **Caution:** Deploying configuration changes using YAML files replaces the configuration of your existing services. Since a YAML file completely overwrites all configurations, you should avoid using multiple methods to modify your services. For example, do not use YAML files in conjunction with the Google Cloud console or `gcloud` commands.

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`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/describe) 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:

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

   Replace <var translate="no">SERVICE</var> with the name of your
   Knative serving service.
2. In your local file, update the `containerPort:` attribute:

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

   Replace
   - <var translate="no">IMAGE_URL</var> with a reference to the container image, for example, `gcr.io/cloudrun/hello`.
   - <var translate="no">PORT</var> with the port to which you want to send requests.
3. Replace the service with its new configuration using the following command:

   ```bash
   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](https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#notes).
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](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/deploying.md.txt#service), update an
existing service, or deploy a [revision](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/deploying.md.txt#revision):

### Console


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

   [Go to Knative serving](https://console.cloud.google.com/kubernetes/run)
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](https://docs.cloud.google.com/sdk/gcloud/reference/topic/escaping) each `ARG` with a different delimiter. For example, if you use `@`:

  ```bash
  --args "^@^arg,with,commas@anotherarg@ARG3..."
  ```
- To specify several sets of key-value pairs, you can specify multiple parameters for readability. Example:

  ```bash
  [...]
  --args "`ARG`1" \
  --args "`ARG`2" \
  --args "`ARG`3"
  ```
- To use equal signs (`=`) in your arguments, you must specify each argument using the following format:

  ```bash
  gcloud run services ...  \
    --args "--repo-allowlist=github.com/example/example_demo" \
    --args "--gh-webhook-secret=XX"
  ```
<button class="devsite-dialog-close">OK</button>

<br />

- For existing services, update the entrypoint command by running the
  [`gcloud run services update`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/update.md.txt)
  command with the following parameters:

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

  Replace:
  - <var translate="no">SERVICE</var> with the name of the service.
  - Optional: <var translate="no">COMMAND</var> with the command that you want the container to run during startup.
  - Optional: <var translate="no">ARG1</var> 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.](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/configuring/containers.md.txt#command-line)
- For new services, set the entrypoint command by running the
  `gcloud run deploy` command with the
  [`--command`](https://docs.cloud.google.com/sdk/gcloud/reference/run/deploy.md.txt#--command)
  parameter:

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

  Replace:
  - <var translate="no">SERVICE</var> with the name of the service.
  - <var translate="no">IMAGE_URL</var> with a reference to the container image, for example, `gcr.io/cloudrun/hello`.
  - Optional: <var translate="no">COMMAND</var> with the command that you want the container to run during startup.
  - Optional: <var translate="no">ARG1</var> 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.](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/configuring/containers.md.txt#command-line)

### YAML

> [!CAUTION]
> **Caution:** Deploying configuration changes using YAML files replaces the configuration of your existing services. Since a YAML file completely overwrites all configurations, you should avoid using multiple methods to modify your services. For example, do not use YAML files in conjunction with the Google Cloud console or `gcloud` commands.

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`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/describe) 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:

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

   Replace <var translate="no">SERVICE</var> with the name of your
   Knative serving service.
2. In your local file, update the `command` and `args` attributes:

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

   Replace:
   - <var translate="no">IMAGE_URL</var> with a reference to the container image, for example, `gcr.io/cloudrun/hello`.
   - Optional: <var translate="no">COMMAND</var> with the command that you want the container to run during startup.
   - Optional: <var translate="no">ARG1</var> 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:

   ```bash
   gcloud run services replace service.yaml
   ```