All Products
Search
Document Center

Elastic Container Instance:Mount an emptyDir volume to an elastic container instance

Last Updated:Aug 15, 2024

An emptyDir volume is an empty directory that is used to temporarily store data and facilitate data sharing between containers. This topic describes how to mount an emptyDir volume to an elastic container instance.

Precautions

emptyDir volumes are used to temporarily store data. When the elastic container instance to which an emptyDir volume is mounted is deleted or restarted, the data stored in the emptyDir volume is cleared.

Configuration description (API mode)

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 following part describes the relevant parameters. For more information, see CreateContainerGroup.

Specify the volume to be mounted

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 volume N.

Volume.N.Type

String

EmptyDirVolume

The value of EmptyDirVolume indicates that an emptyDir volume is to be created.

Volume.N.EmptyDirVolume.Medium

String

Memory

The storage media of emptyDir volume N. This parameter is empty by default, indicating that the node file system is used as the storage media. Valid values:

Volume.N.EmptyDirVolume.SizeLimit

String

256Mi

The storage size of emptyDir volume N. When you specify this parameter, a unit is required. We recommend that you use Gi or Mi as the unit.

Note

If you set Volume.N.EmptyDirVolume.Medium to Memory, you can set Volume.N.EmptyDirVolume.SizeLimit to modify the shm size of the instance. After you set both 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 directory to which volume N is mounted.

Data under this directory is overwritten by the data on the volume. Make sure that this value is correct.

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:

  • None: The volume mount does not receive subsequent mounts that are mounted to this volume or its subdirectories.

  • HostToContainer: The volume mount receives all subsequent mounts that are mounted to this volume or its subdirectories.

  • Bidirectional: This value has a similar effect as HostToContainer. The volume mount receives all subsequent mounts that are mounted to this volume or its subdirectories. In addition, all volume mounts created by container N are propagated back to the host and to all containers of all elastic container instances that use the same volume.

Default value: None.

Note

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.

Configurations (console mode)

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.

Specify the volume to be mounted

  1. In the Container Group Configurations section of the Basic Settings step of the Create Container Group wizard, click Advanced Settings.

  2. In the Storage section, click the Temporary Directories tab and then click Add.

  3. In the Name field, enter a name for the emptyDir volume.

    临时目录

Mount the volume

  1. In the Container Configurations section of the Basic Settings step of the Create Container Group wizard, select a container and click Advanced Settings of the container.

  2. Turn on Storage and click Add.

    You can only add the volumes that you specified in the Container Group Configurations section.

    临时目录2

Note

If you want to mount volumes to multiple containers, select more containers and repeat the preceding operations.

Configuration examples

The following code provides examples of the parameters that you need to configure when you call the CreateContainerGroup API operation to create an elastic container instance and mount an emptyDir volume to the instance. For more information, see CreateContainerGroup.

  • Example 1: Share data between containers

    ContainerGroupName=test-emptydir
    # Specify the volume to be mounted.
    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=/data2

    In the preceding example, the emptyDir volume is shared between Container 1 and Container 2. The /data1 directory of Container 1 and the /data2 directory of Container 2 are empty by default. Files added to the emptyDir volume in one container can be accessed from the other container.

  • Example 2: Modify the shm size of an instance

    ContainerGroupName=test-emptydir
    # Specify the volume to be mounted.
    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/shm

    In the preceding example, EmptyDirVolume.Medium is set to Memory, EmptyDirVolume.SizeLimit is set to 256Mi, and the emptyDir volume is mounted to the /dev/shm directory. This way, the shm size of the elastic container instance is modified to 256 MiB.