All Products
Search
Document Center

Create container groups from the private image repository

Last Updated: Aug 29, 2019

Create private images

Create a namespace, create private images, and set a password for Container Registry. In this example, we will create a BusyBox image repository and an NGINX image repository, and then run the docker push command to upload images.

More information: Container Registry documentation

The following URLs of image repositories in a VPC are used:

  1. registry-vpc.cn-hangzhou.aliyuncs.com/xxx/busybox
  2. registry-vpc.cn-hangzhou.aliyuncs.com/xxx/nginx

Create, view, and delete container groups

The Python SDK is used as an example.

Run the following constant.py to define constants:

  1. AK_ID = 'xxxx'
  2. AK_SECRET = 'xxxx'
  3. SECURITY_GROUP_ID = 'sg-xxxx'
  4. VSWITCH_ID = 'vsw-xxxx'
  5. REGION_ID = 'cn-xxxx'
  6. EIP_ID = 'eip_xxxx'

Run the following eci_test.py to pull images from private image repositories, mount volumes, bind EIPs, initiate containers, and create containers.

  1. # -*- coding:utf-8 -*-
  2. #! /usr/bin/python
  3. import json
  4. from aliyunsdkcore.client import AcsClient
  5. from aliyunsdkeci.request.v20180808.CreateContainerGroupRequest import CreateContainerGroupRequest
  6. from aliyunsdkeci.request.v20180808.DescribeContainerGroupsRequest import DescribeContainerGroupsRequest
  7. from aliyunsdkeci.request.v20180808.DeleteContainerGroupRequest import DeleteContainerGroupRequest
  8. from aliyunsdkeci.request.v20180808.DescribeContainerLogRequest import DescribeContainerLogRequest
  9. # Your own AccessKey ID and AccessKey Secret.
  10. from constant import AK_ID
  11. from constant import AK_SECRET
  12. # The selected security group, VSwitch, and EIP must be in the same VPC.
  13. from constant import SECURITY_GROUP_ID
  14. from constant import VSWITCH_ID
  15. from constant import EIP_ID
  16. # The region in which resources reside.
  17. from constant import REGION_ID
  18. def createContainerGroup():
  19. client = AcsClient(
  20. AK_ID,
  21. AK_SECRET,
  22. REGION_ID,
  23. )
  24. request = CreateContainerGroupRequest()
  25. request.set_ContainerGroupName('test-python-sdk')
  26. request.set_RestartPolicy('Always')
  27. request.set_SecurityGroupId(SECURITY_GROUP_ID)
  28. request.set_VSwitchId(VSWITCH_ID)
  29. request.set_EipInstanceId(EIP_ID)
  30. # Volumes. The first one is an emptyDir volume used for data sharing among containers, and the second one is an NFS volume that runs Alibaba Cloud NAS.
  31. volume1 = {
  32. 'Name': 'default-volume1-empty',
  33. 'Type': 'EmptyDirVolume',
  34. }
  35. volume2 = {
  36. 'Name': 'default-volume2-nfs',
  37. 'Type': 'NFSVolume',
  38. 'NFSVolume.Path': '/',
  39. 'NFSVolume.Server': '042144aa0d-wga99.cn-hangzhou.nas.aliyuncs.com',
  40. 'NFSVolume.ReadOnly': False,
  41. }
  42. # Install volumes in the container group
  43. request.set_Volumes([volume1, volume2])
  44. #tags
  45. tag1 = {
  46. 'Key': 'test',
  47. 'Value': 'sdk-python',
  48. }
  49. tag2 = {
  50. 'Key': 'app',
  51. 'Value': 'nginx',
  52. }
  53. # Add a tag for the container group
  54. request.set_Tags([tag1, tag2])
  55. # Mount volumes of the container group to directories of containers
  56. volume_mount1 = {
  57. 'Name': 'default-volume1-empty',
  58. 'MountPath': '/usr/share',
  59. 'ReadOnly': False,
  60. }
  61. volume_mount2 = {
  62. 'Name': 'default-volume2-nfs',
  63. 'MountPath': '/home',
  64. 'ReadOnly': False,
  65. }
  66. volume_mount3 = {
  67. 'Name': 'default-volume1-empty',
  68. 'MountPath': '/usr/share/nginx/html',
  69. 'ReadOnly': False,
  70. }
  71. #environment variable
  72. env ={
  73. 'Key': 'MESSAGE',
  74. 'Value': 'hello world',
  75. }
  76. #container ports
  77. port = {
  78. 'Protocol':'TCP',
  79. 'Port': 80,
  80. }
  81. # Enter authentication credentials for the private image repositories as required
  82. ImageRegistryCredential = {
  83. 'Server': 'registry-vpc.cn-hangzhou.aliyuncs.com',
  84. 'UserName': 'username',
  85. 'Password': 'password',
  86. }
  87. request.set_ImageRegistryCredentials([ImageRegistryCredential])
  88. #initContainers
  89. initContainer = {
  90. # Enter the URL of the required image
  91. 'Image': 'registry-vpc.cn-hangzhou.aliyuncs.com/xxx/busybox:latest',
  92. 'Name': 'init-busybox',
  93. 'Cpu': 1.0,
  94. 'Memory': 2,
  95. 'VolumeMounts': [volume_mount1, volume_mount2],
  96. 'Commands': ['/bin/sh'],
  97. 'Args': ['-c', 'cp -r /home/ /usr/share/'],
  98. 'EnvironmentVars': [env],
  99. 'Ports': [port],
  100. }
  101. # containers
  102. container1 = {
  103. # Enter the URL of the required image
  104. 'Image': 'registry-vpc.cn-hangzhou.aliyuncs.com/xxx/busybox:latest',
  105. 'Name': 'busybox',
  106. 'Cpu': 0.1891,
  107. 'Memory': 0.55,
  108. 'ImagePullPolicy': 'Always',
  109. 'Commands':['/bin/sh'],
  110. 'VolumeMounts': [volume_mount1, volume_mount2],
  111. }
  112. container2 = {
  113. # Enter the URL of the required image
  114. 'Image': 'registry-vpc.cn-hangzhou.aliyuncs.com/xxx/nginx:latest',
  115. 'Name': 'nginx',
  116. 'Cpu': 0.8109,
  117. 'Memory': 1.45,
  118. 'ImagePullPolicy': 'Always',
  119. 'VolumeMounts': [volume_mount3],
  120. 'EnvironmentVars': [env],
  121. 'Ports': [port],
  122. }
  123. request.set_InitContainers([initContainer])
  124. request.set_Containers([container1, container2])
  125. response = client.do_action_with_exception(request)
  126. print response
  127. def describeContainerGroup(containerGroupIds):
  128. client = AcsClient(
  129. AK_ID,
  130. AK_SECRET,
  131. REGION_ID,
  132. )
  133. request = DescribeContainerGroupsRequest()
  134. # Note: The containerGroupIds array must be encoded in JSON format.
  135. request.set_ContainerGroupIds(json.dumps(containerGroupIds))
  136. # Tags can be specified to filter query results.
  137. tag = {
  138. 'Key': 'test',
  139. 'Value': 'sdk-python',
  140. }
  141. request.set_Tags([tag])
  142. response = client.do_action_with_exception(request)
  143. print response
  144. def describeContainerLog(containerGroupId, name):
  145. client = AcsClient(
  146. AK_ID,
  147. AK_SECRET,
  148. REGION_ID,
  149. )
  150. request = DescribeContainerLogRequest()
  151. request.set_ContainerGroupId(containerGroupId)
  152. request.set_ContainerName(name)
  153. response = client.do_action_with_exception(request)
  154. print response
  155. def deleteContainerGroup(containerGroupId):
  156. client = AcsClient(
  157. AK_ID,
  158. AK_SECRET,
  159. REGION_ID,
  160. )
  161. request = DeleteContainerGroupRequest()
  162. request.set_ContainerGroupId(containerGroupId)
  163. response = client.do_action_with_exception(request)
  164. print response
  165. if __name__ == '__main__':
  166. createContainerGroup()
  167. #describeContainerGroup(['eci-xxx'])
  168. #describeContainerLog('eci-xxx', 'nginx')
  169. #deleteContainerGroup('eci-xxx')