skip to Main Content

I have Dockerfiles with some script, what should I do to run the script as non-root user?

FROM python:3.9
WORKDIR /
COPY . /
RUN pip install --no-cache-dir --upgrade -r /requirements.txt
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "80"]

2

Answers


  1. Chosen as BEST ANSWER

    ok I did this by adding

    RUN groupadd -r user && useradd -r -g user user
    USER user
    

    before running command


  2. You have to use the USER keyword to switch from the default root user.

    Before the line with the RUN, you can put a line like USER XXX with XXX being the wanted user.

    (link to doc : https://docs.docker.com/engine/reference/builder/#user)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search