When creating a workload or job, you can use an image to specify the processes running in the container.
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:
If the working directory is not specified in the image or on the console, the default value is /.
After a container is started, do not modify configurations in the container. If configurations in the container are modified (for example, passwords, certificates, and environment variables of a containerized application are added to the container), the configurations will be lost after the container restarts and container services will become abnormal. An example scenario of container restart is pod rescheduling due to node anomalies.
Configurations must be imported to a container as arguments. Otherwise, configurations will be lost after the container restarts.
A Docker image has metadata that stores image information. If lifecycle commands and arguments are not set, CCE runs the default commands and arguments, that is, Docker instructions ENTRYPOINT and CMD, provided during image creation.
If the commands and arguments used to run a container are set during application creation, the default commands ENTRYPOINT and CMD are overwritten during image build. The rules are as follows:
Image Entrypoint |
Image CMD |
Command to Run a Container |
Args 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] |
Configuration Item |
Procedure |
---|---|
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:
If there are multiple commands, you are advised to run the /bin/sh or other shell commands. Other commands are used as parameters. |
Args |
Enter the argument that controls the container running command, for example, --port=8080. If there are multiple arguments, separate them in different lines. |
The following uses Nginx as an example to describe three typical application scenarios of the container startup command:
nginx -c nginx.conf
Example YAML file:
command: - nginx args: - '-c' - nginx.conf
A command must be enclosed in double quotes. If no double quotes are added, the command is split into multiple commands based on space character.
Example YAML file:
command: - nginx -c nginx.conf args:
If the container startup command is not added to the system path, run the /bin/sh command to execute the container startup command. The container startup command must be enclosed in double quotes.
Example YAML file:
command: - /bin/sh args: - '-c' - '"nginx -c nginx.conf"'
This section uses Nginx as an example to describe how to set container startup commands using kubectl.
Use kubectl to connect to the cluster. For details, see Connecting to a Cluster Using kubectl. See Using kubectl to create a Deployment or Using kubectl to create a StatefulSet. For more details on how to set container startup commands, see official Kubernetes documentation.
apiVersion: apps/v1 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