跳至內容

Possibility to use a docker volume in ECS task with Ec3 with ecs.Scope.TASK

0

In all, is it possible to use a dockervolume with the ecs.Scope.TASK on an EC2 task? I have a task with 3 containers. All 3 containers have to share one volume or path. After the task restart it is strongly needed that the data in the volume or mounted path is empty, fresh. If I use ecs.Scope.SHARED the volume or path will keep alive after the task restarts.

I add to the the task execution role: MyContainerExecutionRole.addToPolicy( new PolicyStatement({ effect: Effect.ALLOW, resources: ['*'], actions: [ "ecs:CreateVolume", "ecs:DeleteVolume", "ecs:DescribeVolumes"] }) ); My volume I set in taskdefinition to: this.taskDefinition = new TaskDefinition(this, ${baseId}/taskDefinition, { networkMode: NetworkMode.AWS_VPC, taskRole: taskRole, executionRole: executionRole, memoryMiB: memoryLimit.toString(), cpu: cpu.toString(), compatibility: Compatibility.EC2, volumes: [ { "name": "shareddata", "dockerVolumeConfiguration": { "scope": Scope.TASK, "driver": "local", }
} ] });

But this will fail the container to start. Inside the container I have: volumes: [{ "name": "shareddata", "path": "/tmp", "readonly": false }],

Is this approach correct?

已提問 2 年前檢視次數 229 次

1 個回答
1

Yes, it is possible to use a Docker volume with the scope set to TASK on an EC2 task in Amazon ECS. Your approach is generally correct, but there are a few adjustments and clarifications to consider:

  1. Docker volumes with scope set to TASK are indeed suitable for your use case. They are automatically provisioned when the task starts and destroyed when the task stops, ensuring that the data is fresh and empty after each task restart.

  2. Your task definition configuration for the volume is correct. Using Scope.TASK will create a new volume for each task instance, which is exactly what you need.

  3. The policy you've added to the task execution role is not necessary for using Docker volumes. These permissions are typically used for EBS volumes, not Docker volumes. You can safely remove this policy addition.

  4. In your container definition, you're using a "volumes" property, which is not the correct way to mount a Docker volume. Instead, you should use "mountPoints". Here's how you should define it:

    mountPoints: [{ sourceVolume: "shareddata", containerPath: "/tmp", readOnly: false }]

  5. Make sure you're defining this mountPoint for all three containers that need to share the volume.

  6. The "driver": "local" in your volume configuration is correct for EC2 tasks. This is the only supported driver for Windows containers, but it works for Linux containers as well.

If your containers are still failing to start after making these adjustments, the issue might be elsewhere in your configuration. Check the container logs or ECS task status for more specific error messages.

Remember that while all three containers can access this shared volume, the data will only persist for the duration of the task. Once the task stops, the volume and its data will be destroyed, providing you with a fresh, empty volume when the task restarts.
Sources
Specify a Docker volume in an Amazon ECS task definition - Amazon Elastic Container Service
Docker volume examples - Amazon Elastic Container Service
Amazon ECS task definition parameters - Amazon Elastic Container Service
DockerVolumeConfiguration - Amazon Elastic Container Service

已回答 2 年前

AWS
專家

已審閱 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。