You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to add a directory where non-root user can write #427
I would like to build my own image based on distroless java, using a Dockerfile, and add a directory where my application can write when it runs as a non-root user.
I don't seem to able to do this. I'm probably missing something obvious, but I just don't see it right now. So I'm hoping somebody might be able to point me in the right direction.
To be a bit more specific I would like to have an image based in distroless where my application runs as (normal, non-root) user 'java' and can write files in a directory '/opt'.
I tried creating a multi-stage docker file where I first create the user and the directory with the right permissions and then copy those to the distroless environment. But in that image the directory /opt will be owned by root and my java user does not have permission to write a new file.
FROM busybox as source
RUN addgroup -S java \
&& adduser -u 10001 -S -G java java --shell /sbin/nologin \
&& mkdir /opt \
&& chown java:java /opt
FROM gcr.io/distroless/java:11
COPY --from=source /etc/passwd /etc/passwd
USER java
COPY --from=source /opt /opt
WORKDIR /opt
@jugatsu Is this user also present in passwd file in distroless Java?
When I refer to user 'nonroot' as user I get linux spec user: unable to find user nonroot: no matching entries in passwd file when starting my container
I was able to build the image, but then when I tried to start it (using docker-compose) I got the 'unable to find user'.
When I use my own custom 'java' user I do not have that problem
FROM openjdk:11-jdk-slim AS build-env
ADD . /app/examples
WORKDIR /app
RUN javac examples/*.java
RUN jar cfe main.jar examples.HelloJava examples/*.class
FROM gcr.io/distroless/java:11
COPY --from=build-env --chown=nonroot:nonroot /app /app
WORKDIR /app
USER nonroot
CMD ["main.jar"]
docker run --rm hello-java
Hello world
It works for me. @fhoeben Could you provide your Dockerfile please.
Reacted by Brendan Kamp, Ramakrishnan E.P. and John McGrath
@jugatsu I must apologise. It's embarrassing but I found why it didn't work for me. It was totally my own fault.
I created my own base image (with the java user) and copied its /etc/passws into the distroless image. That, of course, overwrote the existing passwd file so no wonder the nonroot user no longer worked....
Activity
jugatsu commentedon Oct 30, 2019
You can use
--chownflag https://docs.docker.com/engine/reference/builder/#copyCOPY --from=source --chown=java:java /opt /optfhoeben commentedon Oct 30, 2019
Thanks! Works like a charm (once I also copied /etc/group)
jugatsu commentedon Oct 30, 2019
@fhoeben You don't need to create separate user - distroless image is already included
nonrootuser (65532:65532).COPY --from=source --chown=65532:65532 /opt /optfhoeben commentedon Oct 30, 2019
@jugatsu Is this user also present in passwd file in distroless Java?
When I refer to user 'nonroot' as user I get
linux spec user: unable to find user nonroot: no matching entries in passwd filewhen starting my containerjugatsu commentedon Oct 30, 2019
Yes, user is present. Do you successfully build image with
nonrootuser?fhoeben commentedon Oct 30, 2019
I was able to build the image, but then when I tried to start it (using docker-compose) I got the 'unable to find user'.
When I use my own custom 'java' user I do not have that problem
jugatsu commentedon Oct 31, 2019
It works for me. @fhoeben Could you provide your Dockerfile please.
fhoeben commentedon Oct 31, 2019
@jugatsu I must apologise. It's embarrassing but I found why it didn't work for me. It was totally my own fault.
I created my own base image (with the java user) and copied its
/etc/passwsinto the distroless image. That, of course, overwrote the existingpasswdfile so no wonder thenonrootuser no longer worked....jugatsu commentedon Oct 31, 2019
@fhoeben Is everything working as expected?
fhoeben commentedon Oct 31, 2019
Yes. Thanks a lot for your support!