Home > other >  Build and Run Docker image as non-root user
Build and Run Docker image as non-root user

Time:06-15

I have created a docker file and running it as a root user - Following is my docker file. The below works fine and I can run the image as well.

FROM maven:3.6-jdk-11-slim
WORKDIR /app
COPY . .
RUN mvn clean dependency:go-offline
RUN chmod  x run.sh
ENTRYPOINT ["./run.sh"]

I want to run it as non-root user. The run.sh is as follows

mvn clean spring-boot:run

I tried to change the dockerfile to

FROM maven:3.6-jdk-11-slim
WORKDIR /app
COPY . .
RUN mvn clean dependency:go-offline
RUN groupadd -r -g 2000 mygrp && useradd -u 2000 -r -g mygrp myuser
RUN chown -R myuser:mygrp .

RUN chmod -R 700 run.sh

USER myuser

RUN chmod u x run.sh
ENTRYPOINT ["./run.sh"]

The images builds fine but when I try to run - this is the error

[ERROR] Could not create local repository at /home/myuser/.m2/repository -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LocalRepositoryNotAccessibleException

CodePudding user response:

You might need to update the groupadd instruction.

Replace:

RUN groupadd -r -g 2000 mygrp && useradd -u 2000 -r -g mygrp myuser

With this in the Dockerfile:

RUN groupadd -r -g 2000 mygrp && useradd -m -d /home/myuser/ -s /bin/bash -u 2000 -r -g mygrp myuser

  • Related