Fixing Permissions In Docker Container
There are two ways to fix permissions in a docker container.
Create User and Group in Container (first way)
Dockerfile:FROM ubuntu
ARG WWW_USER_UID=1000
ARG WWW_GROUP_GID=1000
RUN addgroup -gid $WWW_GROUP_GID www
RUN adduser www \
-uid $WWW_USER_UID \
--disabled-login \
--ingroup www \
--no-create-home \
--quiet \
--system
RUN mkdir /app
RUN chown www:www /app
RUN mkdir /home/www
RUN chown www:www /home/www
USER www
WORKDIR /app
WWW_USER_UID and WWW_GROUP_GID must be equal to the user of the host machine, i.e. your current user outside the container.
Fix permissions with ACL (second way)
Dockerfile:FROM alpine:latest as alpine
RUN apk add acl make
WORKDIR app
CMD ["tail", "-f", "/dev/null"]
docker-compose.yaml:
version: "3.4"
services:
alpine:
build:
context: .
target: alpine
stop_grace_period: 0s
volumes:
- .:/app
Makefile:
fix-permissions:
setfacl -dR -m u:$(uid):rwX .
setfacl -R -m u:$(uid):rwX .
Run command:
docker-compose exec alpine make fix-permissions uid=$(id -u)
ACL is an add-on to the standard system of rights and permissions, that is, if the current user does not have the rights and permissions to read / write / execute a file or directory, then using ACL gives such feature by using setfacl command.