All Products
Search
Document Center

Configure startup commands and arguments for a container

Last Updated: May 07, 2021

Elastic Container Instance starts a container by using predefined arguments in the container image. If you did not configure startup commands or arguments when you created an image or if you want to modify them, you can configure the commands or arguments when you create an elastic container instance. This topic describes how to configure the commands and arguments that are run after a container is started.

Background information

If you want to override the default startup settings of an image, such as the working directory and startup commands and arguments, you can set the following fields in the configuration file of an elastic container instance:

  • WorkingDir

    When you create an image, you can set the WORKDIR field to specify the working directory of a container. The startup commands of the container are run in the specified directory. For more information, see WORKDIR.

    You can override the WORKDIR value provided by the image by setting the WorkingDir field in the configuration file of an elastic container instance.

    Note

    • If the WORKDIR field is not specified in the image and the WorkingDir field is not specified in the elastic container instance, the working directory is set to the root directory.

    • If the specified working directory does not exist, the system creates the directory.

  • Command and Arg

    When you create an image, you can set the CMD and ENTRYPOINT fields to specify the commands and arguments that are run after the container is started. For more information, see ENTRYPOINT and CMD.

    You can override the CMD and ENTRYPOINT values provided by the image by setting the Command and Arg fields in the configuration file of an elastic container instance. The following table describes the validity rules.

    ENTRYPOINT value

    CMD value

    Command value

    Arg value

    Command that is run

    Description

    [mkdir]

    [/data/backup]

    Not specified

    Not specified

    [mkdir /data/backup]

    Neither of Command and Arg is specified. The commands and arguments specified by CMD and ENTRYPOINT are run.

    [mkdir]

    [/data/backup]

    [cd]

    Not specified

    [cd]

    Command is specified but Arg is not. Only commands specified by Command is run, and ENTRYPOINT and CMD are ignored.

    [mkdir]

    [/data/backup]

    Not specified

    [/opt/backup]

    [mkdir /opt/backup]

    Arg is specified but Command is not. The commands and arguments specified by Arg and ENTRYPOINT are run.

    [mkdir]

    [/data/backup]

    [cd]

    [/opt/backup]

    [cd /opt/backup]

    Both Command and Arg are specified, and the commands and arguments specified by Command and Arg are run.

    Notice

    The values of Command and Arg must be supported by the container image. Otherwise, the container fails to start.

Kubernetes mode

When you use the Kubernetes mode to create an elastic container instance, you can use the workingDir field to specify the working directory. You can also use the command and args fields to specify the startup commands and arguments. Example:

apiVersion: v1
kind: Pod
metadata:
  name: command-demo
spec:
  containers:
  - name: busybox
    image: busybox
    workingDir: /work
    command: ["printenv"]
    args: ["HOSTNAME", "KUBERNETES_PORT"]
  restartPolicy: OnFailure

For more information, see Define a Command and Arguments for a Container.

API mode

When you call the CreateContainerGroup operation to create an elastic container instance, you can use the WorkingDir parameter to specify the working directory. You can also use the Command and Arg parameters to specify the startup commands and arguments. The following table describes the parameters for the CreateContainerGroup operation. For more information, see CreateContainerGroup.

Parameter

Type

Required

Example

Description

Container.N.WorkingDir

String

No

/usr/local/

The working directory of the container.

Container.N.Command.N

RepeatList

No

sleep

The startup commands of the container. You can specify up to 20 commands.

Container.N.Arg.N

RepeatList

No

100

The arguments corresponding to the startup commands of the container. You can specify up to 10 arguments.

Console mode

When you create an elastic container instance on the Elastic Container Instance buy page, you can configure startup commands and arguments for containers by setting parameters in the Container Configurations section, as shown in the following figure.

Configure startup commands for containers