Issue
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
Solution
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
TL;DR
From the error message, it is quite clear that the myuser
was unable to create a local repository. This is because when you created the new user using the useradd
command, it did not create the home directory for that user. Hence the /home/myuser
directory was missing in the first place.
-m
option in the useradd
command will create the home directory if not present and -s
option sets the home directory on the login shell.
Ref: https://unix.stackexchange.com/a/182324/522304
Answered By - Kapil Khandelwal
Answer Checked By - Cary Denson (JavaFixing Admin)