On my windows pc in docker desktop the app can write to its mounted folders.
On ec2 amazon linux, I build the containers using docker-compose up
they run ok but when they try to write a file to the mounted folder it gets permission denied
,
I used sudo docker exec -it containerId sh
and saw that:
whoami
returns app
ls -l MountedFolder/
shows the folder I’m to write to with this properties
drwxr-xr-x 2 root root
my compose.yaml:
services:
webapp1:
build:
context: ./Store
target: final
ports:
- 8082:8080
volumes:
- ./volumes/App_Data/:/app/App_Data
container_name: webapp1
Dockerfile
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
COPY . /source
WORKDIR /source/WebUI
ARG TARGETARCH
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages
dotnet publish -a ${TARGETARCH/amd64/x64} --use-current-runtime --self-contained false -o /app
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS final
WORKDIR /app
COPY --from=build /app .
USER $APP_UID
ENTRYPOINT ["dotnet", "WebUI.dll"]
2
Answers
Not sure if it is a good solution, but this what I have for now.
I give permissions to all on the folders that I will mount:
and after this I call
and the docker container app doesn't get the permission denied error anymore
Your Docker container is facing a permissions issue when trying to write to a mounted folder on an Amazon EC2 instance running Amazon Linux:
The
MountedFolder
in your EC2 instance is owned byroot
with permissionsdrwxr-xr-x
(read and execute for group and others, but write permission only for the owner). When your container runs, it switches to a non-root user (app
), which does not have write permissions on the mounted folder.You would need to adjust the permissions of the
MountedFolder
on your EC2 instance to allow theapp
user in the container to write to it. That can be done by either changing the folder’s ownership to match the UID of theapp
user inside the container or by adjusting the folder’s permissions to be more permissive (not recommended).The preferred option is to use the
chown
command on your EC2 instance to change the ownership of theMountedFolder
. You need to set the owner to the UID of theapp
user inside the container. First, find out the UID of theapp
user inside the container withid -u app
, then apply it to the mounted folder:You also need to make sure your Dockerfile correctly sets up the user. Since you are switching to a user
$APP_UID
, make sure this environment variable is properly set to match the UID you want to use. IfAPP_UID
is not defined, you need to add a line in your Dockerfile to create theapp
user with a specific UID that matches the host’s folder ownership or use an existing UID.