Use bind mounts with Amazon ECS
With bind mounts, a file or directory on a host, such as an Amazon EC2 instance, is mounted
into a container. Bind mounts are supported for tasks that are hosted on both Fargate and
Amazon EC2 instances. Bind mounts are tied to the lifecycle of the container that uses them.
After all of the containers that use a bind mount are stopped, such as when a task is
stopped, the data is removed. For tasks that are hosted on Amazon EC2 instances, the data can be
tied to the lifecycle of the host Amazon EC2 instance by specifying a host and
optional sourcePath value in your task definition. For more information, see
Bind mounts
The following are common use cases for bind mounts.
-
To provide an empty data volume to mount in one or more containers.
-
To mount a host data volume in one or more containers.
-
To share a data volume from a source container with other containers in the same task.
-
To expose a path and its contents from a Dockerfile to one or more containers.
Considerations when using bind mounts
When using bind mounts, consider the following.
-
By default, tasks that are hosted on Amazon Fargate using platform version
1.4.0or later (Linux) or1.0.0or later (Windows) receive a minimum of 20 GiB of ephemeral storage for bind mounts. You can increase the total amount of ephemeral storage up to a maximum of 200 GiB by specifying theephemeralStorageparameter in your task definition. -
To expose files from a Dockerfile to a data volume when a task is run, the Amazon ECS data plane looks for a
VOLUMEdirective. If the absolute path that's specified in theVOLUMEdirective is the same as thecontainerPaththat's specified in the task definition, the data in theVOLUMEdirective path is copied to the data volume. In the following Dockerfile example, a file that's namedexamplefilein the/var/log/exporteddirectory is written to the host and then mounted inside the container.FROM public.ecr.aws/amazonlinux/amazonlinux:latest RUN mkdir -p/var/log/exportedRUN touch/var/log/exported/examplefileVOLUME ["/var/log/exported"]By default, the volume permissions are set to
0755and the owner asroot. You can customize these permissions in the Dockerfile. The following example defines the owner of the directory asnode.FROM public.ecr.aws/amazonlinux/amazonlinux:latest RUN yum install -y shadow-utils && yum clean all RUN useraddnodeRUN mkdir -p /var/log/exported && chownnode:node/var/log/exported RUN touch /var/log/exported/examplefile USERnodeVOLUME ["/var/log/exported"] -
For tasks that are hosted on Amazon EC2 instances, when a
hostandsourcePathvalue aren't specified, the Docker daemon manages the bind mount for you. When no containers reference this bind mount, the Amazon ECS container agent task cleanup service eventually deletes it. By default, this happens three hours after the container exits. However, you can configure this duration with theECS_ENGINE_TASK_CLEANUP_WAIT_DURATIONagent variable. For more information, see Amazon ECS container agent configuration. If you need this data to persist beyond the lifecycle of the container, specify asourcePathvalue for the bind mount.