This topic describes how to mount an emptyDir volume to an elastic container instance (ECI) for temporary storage and data sharing between containers.
Usage notes
An emptyDir volume provides temporary storage. Data in an emptyDir volume is permanently deleted if the ECI instance is deleted or restarted.
Configuration (API)
When you call the CreateContainerGroup API operation to create an elastic container instance, you can use Volume-related parameters to specify volumes, and then use Container.VolumeMount-related parameters to mount the volumes to containers. The relevant parameters are described below. For more information, see CreateContainerGroup.
Declare a volume
When you use Volume-related parameters to specify volumes, you must first specify the types and names of the volumes. Then, you must configure other Volume-related parameters based on the value of the Volume.N.Type parameter.
|
Parameter |
Type |
Example |
Description |
|
Volume.N.Name |
String |
emptydir-demo |
The name of the volume. |
|
Volume.N.Type |
String |
EmptyDirVolume |
Specifies the volume type. Set to |
|
Volume.N.EmptyDirVolume.Medium |
String |
Memory |
The storage medium for the emptyDir volume. If you leave this parameter empty, the host's file system is used by default. Valid values:
|
|
Volume.N.EmptyDirVolume.SizeLimit |
String |
256Mi |
The size of the emptyDir volume. You must include a unit in the value, such as |
For an emptyDir volume with Medium set to Memory, you can set SizeLimit to modify the shm size of the instance. After you set the Medium and SizeLimit parameters, you must mount the emptyDir volume to the /dev/shm directory of the container.
Mount volumes
After you specify volumes, you can use VolumeMount-related parameters to mount the volumes to containers.
Parameter | Type | Example | Description |
Container.N.VolumeMount.N.Name | String | test-volume | The name of volume N to be mounted to container N. This value is the value of Volume.N.Name. |
Container.N.VolumeMount.N.MountPath | String | /usr/share | The path within the container at which to mount the volume. Data in this directory is obscured by the data on the volume. |
Container.N.VolumeMount.N.SubPath | String | /usr/sub | The subdirectory of the volume. This parameter specifies different subdirectories of the same volume that the instance can mount to different subdirectories of containers. |
Container.N.VolumeMount.N.ReadOnly | Boolean | false | Specifies whether the mount directory is read-only. Default value: false. |
Container.N.VolumeMount.N.MountPropagation | String | None | The mount propagation setting of volume N. Mount propagation allows you to share volumes that are mounted to one container to other containers in the same elastic container instance, or even to other elastic container instances on the same host. Valid values:
Default value: None. |
Volumes can also be mounted to init containers. The required parameters are similar to the parameters in the preceding table. You only need to change Container to InitContainer.
Configuration (console)
When you create an elastic container instance on the Elastic Container Instance buy page, you can specify volumes in the Container Group Configurations section and then mount the volumes to containers in the Container Configurations section.
Declare a volume
-
In the Container Group Configurations section, expand Advanced Settings.
-
On the Temporary Directories tab of the Storage section, click Add.
-
Enter a name for the emptyDir volume.
For example, enter
emptydir-demoin the Name field.
Mount a volume
-
In the Container Configurations section, select a container and expand its advanced settings.
-
Turn on the Storage switch and click Add.
You can add only the volumes that you declared when you configured the container group.
Select the storage with the name emptydir-demo and type Temporary Directories, and set the mount path in the container to
/data.
If you want to mount volumes to multiple containers, select more containers and repeat the preceding operations.
Examples
The following examples show the parameters for calling the CreateContainerGroup operation to create an ECI instance and mount an emptyDir volume to the instance. For more information, see CreateContainerGroup.
-
Example 1: Share data between containers
ContainerGroupName=test-emptydir # Declare a volume Volume.1.Name=emptydir-demo Volume.1.Type=EmptyDirVolume # Mount the volume to container 1 Container.1.Name=nginx Container.1.Image=registry-vpc.cn-hangzhou.aliyuncs.com/eci_open/nginx:1.14.2 Container.1.VolumeMount.1.Name=emptydir-demo Container.1.VolumeMount.1.MountPath=/data1 # Mount the volume to container 2 Container.2.Name=busybox Container.2.Image=registry-vpc.cn-hangzhou.aliyuncs.com/eci_open/busybox:1.30 Container.2.Command.1=sleep Container.2.Arg.1=999999 Container.2.VolumeMount.1.Name=emptydir-demo Container.2.VolumeMount.1.MountPath=/data2In this example, Container 1 and Container 2 share an emptyDir volume. The
/data1directory in Container 1 and the/data2directory in Container 2 are initially empty. Any files that you add to the emptyDir volume from one container can be accessed by the other container. -
Example 2: Modify the shm size of an instance
ContainerGroupName=test-emptydir # Declare a volume Volume.1.Name=emptydir-shm Volume.1.Type=EmptyDirVolume Volume.1.EmptyDirVolume.Medium=Memory Volume.1.EmptyDirVolume.SizeLimit=256Mi # Mount the volume to container 1 Container.1.Name=nginx Container.1.Image=registry-vpc.cn-hangzhou.aliyuncs.com/eci_open/nginx:1.14.2 Container.1.VolumeMount.1.Name=emptydir-shm Container.1.VolumeMount.1.MountPath=/dev/shmIn this example, setting
EmptyDirVolume.MediumtoMemoryandEmptyDirVolume.SizeLimitto256Mi, and then mounting the emptyDir volume to the/dev/shmdirectory, changes theshmsize of the ECI instance to 256 MiB.