• Cloud Container Engine

cce
  1. Help Center
  2. Cloud Container Engine
  3. User Guide 2.0
  4. Application Management
  5. Setting Container Startup Command

Setting Container Startup Command

When creating an application, you usually specify the processes running in the container using an image.

By default, the image runs the default command. To run a specific command or rewrite the default image value, you must perform the following settings:

  • Working directory: Specifies the working directory of the command.
    NOTE:

    If the working directory is not specified in the image or on the console, the default value is /.

  • Running command: Controls the running of an image.
  • Running parameters: Parameters transferred to the running command.

Commands and Parameters Used to Run a Container

A Docker image has metadata related to the stored image information. If no lifecycle command or parameter is set, the container runs the default command and parameter provided during image creation. The Docker defines the two fields as Entrypoint and CMD. For details, see Entrypoint Description and CMD Description in Docker.

If the commands and parameters used to run a container are set during application creation, the default commands Entrypoint and CMD are overwritten during image building. The rules are as follows:

Table 1 Commands and parameters used to run a container

Image Entrypoint

Image CMD

Command to Run a Container

Parameters to Run a Container

Command Executed

[touch]

[/root/test]

Not set

Not set

[touch /root/test]

[touch]

[/root/test]

[mkdir]

Not set

[mkdir]

[touch]

[/root/test]

Not set

[/opt/test]

[touch /opt/test]

[touch]

[/root/test]

[mkdir]

[/opt/test]

[mkdir /opt/test]

Setting the Startup Command

  1. Log in to the CCE console. Expand Lifecycle when creating an application or task.
  2. Enter the running command and parameters, as shown in Table 2.

    NOTE:
    • The current startup command is provided as a string array and corresponds to the ENTRYPOINT startup command of the Docker. The format is as follows: ["executable", "param1", "param2",..]. For details about how to start the Kubernetes container, click here.
    • The lifecycle of the container is the same as that of the startup command. That is, the lifecycle of the container ends after the command is executed.
    Table 2 Container startup command

    Command Mode

    Procedure

    Running a command

    Enter an executable command, for example, /run/server.

    If there are multiple commands, separate them with spaces. If the command contains a space, you need to add a quotation mark ("").

    NOTE:

    In the case of multiple commands, you are advised to run the /bin/sh or other shell commands. Other commands are used as parameters, as shown in Figure 1.

    Running a parameter

    Enter the parameter that controls the container running command, for example, --port=8080.

    If there are multiple parameters, separate them in different lines.

    Figure 1 Multi-command

Example YAML for Setting the Container Lifecycle

This section uses an Nginx application as an example to describe how to deploy an application on a specified node using kubectl.

Prerequisites

You have configured the kubectl commands and connected an ECS to the cluster.

Procedure

See Creating a Stateless Application Using kubectl or Creating a Stateful Application Using kubectl. For details about how to set container lifecycle parameters, see the Kubernetes official documentation.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx 
        command:
        - sleep 3600                        #Startup command
        imagePullPolicy: Always
        lifecycle:
          postStart:
            exec:
              command:
              - /bin/bash
              - install.sh                  #Post-start command
          preStop:
            exec:
              command:
              - /bin/bash
              - uninstall.sh                 #Pre-stop command
        name: nginx
      imagePullSecrets:
      - name: default-secret