I created a user with uid 1000 in the Dockerfile and created a directory /app, changing the owner of /app to uid 1000. Spring Boot also starts using the user with uid 1000.
RUN adduser --uid 1000 -D appuser
RUN mkdir /app
RUN chown -R appuser:appuser /app
USER 1000
ENTRYPOINT [ "./docker-entrypoint.sh" ]
When Spring Boot starts, it creates the directory /app/portal/logs and writes logs to the logs directory. Before using Docker Compose to mount the logs to a directory on the host with a volume, the entire path /app/portal/logs had uid 1000 as the owner, and there was no issue.
However, when I created a volume to mount /app/portal/logs to /data on the host, the owner of the app and logs directories inside the container remained uid 1000, but the owner of the intermediate directory, portal, became root.
services:
portal:
image: myapp:latest
volumes:
- /data:/app/portal/logs
user: "1000:1000"
/app $ ls -al / | grep app
drwxr-xr-x 1 appuser appuser 83 Jun 7 16:13 app
/app $ ls -al /app | grep portal
drwxr-xr-x 3 root root 18 Jun 7 16:13 portal
/app $ ls -al /app/portal/
drwxr-xr-x 3 root root 18 Jun 7 16:13 .
drwxr-xr-x 1 appuser appuser 83 Jun 7 16:13 ..
drwxr-xr-x 2 appuser appuser 4096 Jun 7 14:54 logs
This causes Spring Boot to have insufficient permissions when creating other directories in /app/portal. How can I change the owner of the entire path created by the volume to uid 1000?
I tried adding user: “1000:100” in the Docker Compose YAML, but it didn’t work. Pre-creating /app/portal and changing the owner of the entire path to uid 1000 in the Dockerfile beforehand can solve this issue, but it seems a bit odd to do so.