Image Builder uses an image template to customize image content. You can create an image template by specifying image components or by defining commands directly. An image component consists of one or more commands, making a command the most basic unit. This topic describes the syntax of Image Builder commands and provides examples.
Command syntax
Image Builder supports commands in two formats: YAML (for Linux and Windows) and Dockerfile (for Linux only).
Dockerfile commands offer less flexibility and have operating system limitations. We recommend that you use the YAML format.
The following syntax is required for the YAML and Dockerfile formats:
YAML: You must define the
ParametersandTaskssections. The syntax is as follows:Parameters: # Optional. Defines custom parameters. param1: # Required. The name of the parameter. Type: String # Required. The type of the parameter. Valid values: String, Number, and Boolean. DefaultValue: defaultValue # Optional. The default value of the parameter. If no default value is specified, you must explicitly pass a value when you use the image component in an image template. Tasks: # Required. Defines a list of tasks for the image component. The tasks are run in sequence. At least one task must be included. - Name: taskName1 # Required. The name of the task. Action: RunShellCommand # Required. The action for the task. For more information, see the YAML-format component commands. Properties: # The properties of the action. The available properties depend on the specified action. commandContent: echo {{ param1 }} # References a custom parameter. - Name: taskName2 Action: action2 Properties: action2Property1: {{ taskName1.stdout }} # References the output of taskName1.NoteYou can reference defined parameters in the properties of a task as follows:
{{taskName1.stdout}}Dockerfile
Each line contains one command.
A single command can span multiple lines. To continue a command on the next line, add an escape character (\) at the end of the current line.
Commands supported by Image Builder
This section describes the command formats that Image Builder supports for creating an image component in the console or via the API.
Component commands
YAML format
Command | Syntax | Description | Output |
RunShellCommand | |
| |
RunPowerShellCommand | |
| |
InvokeCommand | | Runs a common command. | |
OSSDownload | |
| None |
OSSUpload | |
| |
WebDownload | | Downloads a file from a web URL. | |
Reboot | | Reboots the instance. |
Dockerfile format
Command | Syntax | Description |
RESTART |
|
|
RUN | |
|
ENV |
|
Note When you use the |
WORKDIR |
| Sets the working directory. |
COPY |
| Copies files. Note All network files are downloaded using |
USER |
| Sets the user for subsequent Note The |
LABEL | |
|
CMD |
| Specifies the command to run on startup. |
ENTRYPOINT |
| Specifies the command to run on startup. Note The |
Template commands
Command | Syntax | Description |
COMPONENT |
| Specifies an image component. You can specify a system or custom image component.
|
RESTART |
|
|
RUN | |
|
ENV |
|
Note When you use the |
WORKDIR |
|
|
COPY |
|
Note All network files are downloaded by using |
USER |
|
Note The |
LABEL | |
|
CMD |
|
|
ENTRYPOINT |
|
Note The |
Command examples
This section provides examples of component and template commands for specific use cases.
Component commands
Example 1: Use custom parameters
In this example, the function (3x+2y+z)+(3x+2y+z) is configured in an image component. This lets you assign different values to x, y, and z each time you use the component in an image template. The following code shows an example of the component command. For an example of the corresponding template command, see Example 1: Associate a template with a component and assign values to its custom parameters.
Parameters:
x:
Type: String
y:
Type: String
z:
Type: String
Tasks:
- Name: count
Action: RunShellCommand
Properties:
commandContent: echo $((3 * {{x}} + 2 * {{y}} + {{z}}))
- Name: result
Action: RunShellCommand
Properties:
commandContent: echo $(({{count.stdout}}+{{count.stdout}}))Example 2: Configure a script from OSS
You can use an image component to build an image from a Linux script stored in an OSS bucket. This lets you create multiple ECS instances with identical configurations from the same image. Because the image component uses the OSSDownload and OSSUpload actions, you do not need to hardcode an AccessKey pair in the component.
Tasks:
- Name: ossdownload
Action: OSSDownload
Properties:
bucketName: <your_oss_bucket_name>
destinationDir: /home
objectName: <your_script_object_name>
- Name: setpermissions
Action: RunShellCommand
Properties:
commandContent: sudo chmod +x /home/<your_script_object_name>
- Name: createfile
Action: RunShellCommand
Properties:
commandContent: sudo touch /home/output.txt
- Name: setfilepermissions
Action: RunShellCommand
Properties:
commandContent: sudo chmod 666 /home/output.txt
- Name: result
Action: RunShellCommand
Properties:
commandContent: sudo bash /home/<your_script_object_name> &> /home/output.txt
- Name: ossupload
Action: OSSUpload
Properties:
bucketName: <your_oss_bucket_name>
fileToUpload: /home/output.txt
objectName: output.txtTemplate commands
Example 1: Associate a component and assign parameters
This example assigns values to the x, y, and z parameters from Example 1: Use custom parameters in a component. You can assign different values each time you use the template command.
COMPONENT i-xxxxxxxxx --x 1 --y 2 --z 3Example 2: Use the latest component version
If a component has multiple versions, such as 1.0.1, 1.0.2, and 1.0.3, you can specify a version pattern such as 1.0.* in the template command. This associates the template with the latest matching version (1.0.3 in this case).
COMPONENT acs:ecs:cn-hangzhou:<alibaba_cloud_account_id>:imagecomponent/<your_component_name>/1.0.*