- Newest
- Most votes
- Most comments
When you set readonlyRootFilesystem: true in your ECS task definition, the container's entire root filesystem becomes read-only, including the /tmp directory. This is a security best practice to prevent unauthorized modifications to the container's filesystem.
Your approach of mounting a volume at /tmp is correct, but there are a few issues with your current configuration:
-
You've defined a volume named
tmp-volume, but you haven't specified what type of volume it is. For Fargate tasks, you need to define it properly. -
The volume needs to be writable by your
appuser. In multi-container tasks with different users, Amazon ECS automatically manages group permissions to allow specified users to write to volumes, but you need to ensure youruserparameter is properly configured.
Here are some solutions:
- Complete your volume definition by specifying it as an ephemeral storage volume:
Volumes:
- Name: tmp-volume
DockerVolumeConfiguration:
Scope: task
Autoprovision: true
Driver: local
-
Ensure your container definition includes the
userparameter that matches the user ID of yourappuserin the container image. -
Your alternative approach of changing
TMPDIRto/app/tmpcould also work if your application allows for this configuration and the/appdirectory is writable by yourappuser. This would avoid the need for a separate volume mount. -
Another option is to use an environment variable to redirect temporary files to a writable location:
Environment:
- Name: TMPDIR
Value: /app/tmp
Remember to create the /app/tmp directory in your Dockerfile and ensure it has the proper permissions for your appuser.
Sources
Amazon ECS task definition parameters for Amazon EC2 - Amazon Elastic Container Service
Amazon ECS task definition parameters for Amazon ECS Managed Instances - Amazon Elastic Container Service
Container can't write to Amazon EBS volume - Amazon Elastic Container Service
Amazon ECS task definition parameters for Fargate - Amazon Elastic Container Service
ecs-containers-readonly-access - AWS Config
answered a year ago
Ok, I made some changes. But still when ECS mount /app/tmp it is owned by root and the appuser cannot write to it. I am lost here.
TaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: !Ref Name
NetworkMode: awsvpc
Memory: 2048
Cpu: 1024
Volumes:
- Name: tmp-volume
RequiresCompatibilities:
- FARGATE
ExecutionRoleArn: !Ref ExecutionRole
TaskRoleArn: !Ref TaskRole
ContainerDefinitions:
- Name: !Ref Name
Image: !Ref Image
User: "appuser:appuser"
#readonlyRootFilesystem: true
MountPoints:
- SourceVolume: tmp-volume
ContainerPath: /app/tmp
ReadOnly: false
Here's the Dockerfile
RUN useradd -m appuser
WORKDIR /app
RUN mkdir -p /app/tmp && chown -R appuser:appuser /app/tmp
ENV TMPDIR=/app/tmp
VOLUME ["/app/tmp"]
RUN chown -R appuser:appuser /app
USER appuser
CMD ["node dist/main"]
answered a year ago
Hi, I am facing the same issue. Were you able to find a solution for this
Perhaps I need to add this to my Dockerfile, which I have not, if I am to go with using /app/tmp as the default os tmpdir via TMPDIR environmental variable.
VOLUME ["/app/tmp"]
answered a year ago
There is a very similar question I answered the other day - please take a look. And, yes, you do need to update dockerfile.
Refer: https://repost.aws/questions/QU_3NaRXH0RKewjMYc-hWJTw
Relevant content
asked 3 years ago
asked 5 months ago

I actually tried just now removing the Volume and MountPoint parameters entirely from my Task Definition and instead do this in my Dockerfile to use /app/tmp as the application tmpdir. And it works up until we again enable readonlyRootFilesystem. Then the app starts raising errors like: Uncaught exception EROFS: read-only file system, open '/app/tmp/c1.wav'
RUN mkdir -p /app/tmp && chown -R appuser:appuser /app/tmp ENV TMPDIR=/app/tmp RUN chown -R appuser:appuser /app USER appuser WORKDIR /app CMD ["node dist/main"]