All Products
Search
Document Center

CloudOps Orchestration Service:Create a Docker image, upload it to Container Registry, and deploy it to ECS

Last Updated:Mar 25, 2025

This sample template is used to pull code from GitHub to create a Docker image, upload it to Container Registry, and then deploy it on multiple Elastic Compute Service (ECS) instances. This is applicable to scenarios where you need to create an image, upload the image to an image repository, and deploy the image on multiple ECS instances. You can run this template on an existing ECS instance or run this template to create a temporary ECS instance. After the image is created and uploaded, the temporary ECS instance is automatically released. In addition, this template allows you to upload images to and pull images from Container Registry Enterprise Edition or Container Registry Personal Edition based on your business requirements. You are charged for using Container Registry Enterprise Edition. You can use Container Registry Personal Edition free of charge.

Prerequisites

Sample template

image
  1. You specify the code source, such as Object Storage Service (OSS) or GitHub. CloudOps Orchestration Service (OOS) generates a temporary URL for the code source to pull code from the code source.

  2. OOS automatically pulls code to install Docker and executes a script to create an image. After the image is created, OOS uploads the image to the specified image repository in Container Registry.

  1. Pull the image created in the previous step and deploy the image as a Docker container on multiple ECS instances at a time.

Example

Prepare the code source

The sample code of a Spring Boot project is used in this example. The code is uploaded to Gitee and GitHub. If you want to use the code, you must fork the code to your own repository. Code address:

  • Gitee: Sample code (recommended for users in the Chinese mainland)

  • GitHub:Sample code (recommended for users in the Chinese mainland)

Create a message template

  1. Log on to the CloudOps Orchestration Service console.

  2. In the left-side navigation pane, choose Automated Task > Custom Template and click Create Template.

  3. In the Build and Deploy section, select ACS-ECS-ExampleDockerBuildAndDeployFromGit and click Next Step.

  4. On the Process Configuration tab, set the parameters and click Create Template.

    1. Specify the code source to generate a temporary authorization link.

      In this example, Gitee is used as the code source. Set the following parameters: Owner, Organization, Repository, and Branch.

      Note
      • If Alibaba Cloud is not authorized to access your GitHub or Gitee repository, click Grant the required permissions.

      • If you have forked the sample code, all repositories that belong to your account are automatically displayed in the Repository drop-down list. In this case, select the repository to which you forked the code.

      2024-12-16_11-42-41.png

    2. Create a Docker image and upload it to Container Registry.

      1. Specify the code source and an ECS instance, and specify the configuration script as needed.

        构建镜像docker.png

        • Set CodeResource to git and use the default value authorizedUrl for CodeResourceUrl.

        • Choose Building Ecs: Select an ECS instance. You can select Existing ECS or New ECS.

          Note

          If you select New ECS, you are charged on a pay-as-you-go basis. OOS automatically checks whether Docker is installed on the ECS instance. If Docker is not installed, OOS automatically installs Docker Community Edition on the ECS instance.

        • Build Configuration: provides build commands as needed. All builds in the sample code are completed in the Dockerfile. No additional preparation is required.

      2. Create a Docker image and upload it to Container Registry.

        OOS automatically runs the docker build command to create an image. You must specify the path of the Dockerfile and the context path in which the docker build command is run.

        ACR.png

        • Dockerfile Path: the path of the Dockerfile relative to the repository root directory. As shown in the following figure, Dockerfile is stored in the root directory of the pulled code. Therefore, you can directly specify the name of the Dockerfile.

          image

        • Context Path: Set Context Path to the execution context of the docker build command. Enter a path relative to the code root directory. If you do not specify this parameter, the path is the same as the Dockerfile directory.

        • ACR Type: Select Enterprise Edition (charged) or Personal Edition (free of charge).

          ACR Type

          Description

          References

          Personal Edition

          You need to specify the username, password, and the name of the repository to which the image is uploaded.

          Create a Container Registry Personal Edition instance

          Enterprise Edition

          You need to specify the Container Registry instance ID, namespace, and the name of the repository to which the image is uploaded

          Create a Container Registry Enterprise Edition instance

          Specify the image version. The default value consists of the current date and the execution ID. You can specify the image version of your project. Check whether the image is uploaded over a virtual private cloud (VPC). For more information, see Configure a VPC ACL.

    3. Pull the image from Container Registry and deploy it.

      1. Select the ECS instances on which you want to deploy the image.

        image

        • Target Instance: You can select one or more ECS instances at a time.

        • Image Repository: To pull the image uploaded to Container Registry in the previous step, select the edition of Container Registry. The following parameters are automatically referenced from the previous outputs: imageVersion, username, password, acrId, namespace, and repoName.

          Note

          For more information about how to pull an image by using a VPC, see Configure a VPC ACL.

      2. Specify the running options of Docker.

        For example, if you enter -p 8080:8080, these options will be added to the docker run command and will be executed.

        image

        docker run -p 8080:8080 <image_name>
        Note

        The --name option is not available because OOS assigns a default name that corresponds to the Container Registry repository to the container. Each time before OOS runs the docker run command to use the image of the current version, OOS first checks whether any image with the same name is running. If so, it stops the container and then runs the docker run command to use the current image version. This way, the next time you upload the latest version of the image to the Container Registry repository, you can directly execute the template again without manually stopping the container corresponding to the previous version of the image.

      3. Optional. Set the concurrency control parameters to control the concurrency rate of multiple ECS instances.

    4. Click Create Template.

Execute the template

  1. Log on to the CloudOps Orchestration Service console.

  2. In the left-side navigation pane, choose Automated Task > Custom Template.

  3. On the Custom Template page, find the template and click Create Execution in the Actions column.

  4. In the Basic Information step, set the parameters and click Next Step: Parameter Settings.

  5. In the Parameter Settings step, click OK if no parameters are available.

  6. Click Create.

    On the Task Execution Management page, if the execution status is Success, the execution is complete.

    执行模板结果-zh.png

  7. Log on to the ECS console.

  8. Find the ECS instance and connect to the instance.

    For more information, see Use Workbench to connect to a Linux instance over SSH.

  9. Run the following command to check whether the deployment is successful.

    curl http://localhost:8080/hello

    If the following message is returned, the deployment is successful.image

    You can also run the docker ps command to view the corresponding container.image

What to do next

If your code is modified, you only need to upload the new code to the Git repository and re-execute the CI/CD template. The template stops the current container, regenerates an image, and pushes it to Container Registry. Then, the latest version of the image is deployed on the specified ECS instances in batches. You can also modify the template if needed and execute the template again.