'How to run docker-compose as non root user?

By default docker use a root user in a Dockerfile. If I use a root user (docker default user), I can build flask image with (docker build and docker run) or docker-compose up --build and everything works fine apart from having a warning about Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager.

To avoid the warning, I then decided to add a non root user in the Dockerfile. The warning disappeared indeed, I can also build flask image with (docker build and docker run) that works fine. But unfortunately it doesn't work anymore with docker-compose up --build

So the question is: how to avoid the warning above and still have docker-compose up --build working fine

Here are my Dockerfile and docker-compose.yml with non root user

FROM python:3.10

RUN useradd -ms /bin/bash myuser
USER myuser
WORKDIR /home/myuser
RUN pip install --upgrade --user pip
COPY --chown=myuser:myuser ./code/* ./   
RUN  pip install -r requirements.txt --user --no-warn-script- 
location
ENV PATH="/home/myuser/.local/bin:${PATH}"


version: '3.9'

services:
  flask:
    build: .
    container_name: flask_container
    command: python main.py
    # user: 1000:1000
    ports:
      - 5000:5000
    volumes:
      - ./code:/home/myuser

After running docker-compose up --build I'm getting the following error(same error when using user: 1000:1000): ModuleNotFoundError: No module named 'flask'

Collecting Flask
  Downloading Flask-2.0.3-py3-none-any.whl (95 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 95.6/95.6 KB 2.1 
MB/s eta 0:00:00
Collecting itsdangerous>=2.0
  Downloading itsdangerous-2.1.1-py3-none-any.whl (15 kB)
Collecting Werkzeug>=2.0
  Downloading Werkzeug-2.0.3-py3-none-any.whl (289 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 289.2/289.2 KB 4.4 
MB/s eta 0:00:00
Collecting Jinja2>=3.0
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.6/133.6 KB 3.0 
MB/s eta 0:00:00
Collecting click>=7.1.2
  Downloading click-8.0.4-py3-none-any.whl (97 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.5/97.5 KB 2.7 
MB/s eta 0:00:00
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.1-cp310-cp310- 
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Installing collected packages: Werkzeug, MarkupSafe, 
itsdangerous, click, Jinja2, Flask
Successfully installed Flask-2.0.3 Jinja2-3.0.3 MarkupSafe-2.1.1 
Werkzeug-2.0.3 click-8.0.4 itsdangerous-2.1.1
Removing intermediate container 79206447dcf5
 ---> 51797f14d971
Step 8/8 : ENV PATH="/home/myuser/.local/bin:${PATH}"
 ---> Running in bc4b778bac5d
Removing intermediate container bc4b778bac5d
 ---> 195f721ef077
Successfully built 195f721ef077
Successfully tagged docker-dev_flask:latest
Creating flask_container ... done
Attaching to flask_container
flask_container | Traceback (most recent call last):
flask_container |   File "/home/myuser/main.py", line 1, in 
<module>
flask_container |     from flask import Flask
flask_container | ModuleNotFoundError: No module named 'flask'
flask_container exited with code 1

But everything works fine when using root user

FROM python:3.10

WORKDIR /code
COPY ./code/* ./   
RUN  pip install -r requirements.txt


version: '3.9'

services:
  flask:
    build: .
    container_name: flask_container
    command: python main.py
    ports:
      - 5000:5000
    volumes:
      - ./code:/code

docker-compose up --build doesn't fail but gives a warning about using a root user.

Collecting Flask
  Downloading Flask-2.0.3-py3-none-any.whl (95 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 95.6/95.6 KB 1.8 
MB/s eta 0:00:00
Collecting click>=7.1.2
  Downloading click-8.0.4-py3-none-any.whl (97 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.5/97.5 KB 1.5 
MB/s eta 0:00:00
Collecting Jinja2>=3.0
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.6/133.6 KB 1.8 
MB/s eta 0:00:00
Collecting Werkzeug>=2.0
  Downloading Werkzeug-2.0.3-py3-none-any.whl (289 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 289.2/289.2 KB 1.8 
MB/s eta 0:00:00
Collecting itsdangerous>=2.0
  Downloading itsdangerous-2.1.1-py3-none-any.whl (15 kB)
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.1-cp310-cp310- 
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Installing collected packages: Werkzeug, MarkupSafe, 
itsdangerous, click, Jinja2, Flask
Successfully installed Flask-2.0.3 Jinja2-3.0.3 MarkupSafe- 
2.1.1 Werkzeug-2.0.3 click-8.0.4 itsdangerous-2.1.1

WARNING: Running pip as the 'root' user can result in broken 
permissions and conflicting behaviour with the system package 
manager. It is recommended to use a virtual environment 
instead: https://pip.pypa.io/warnings/venv

Removing intermediate container b9626dc2cd44
 ---> 42f016cc5667
Successfully built 42f016cc5667
Successfully tagged docker-dev_flask:latest
Creating flask_container ... done
Attaching to flask_container
flask_container |  * Serving Flask app 'main' (lazy loading)
flask_container |  * Environment: production
flask_container |    WARNING: This is a development server. Do 
not use it in a production deployment.
flask_container |    Use a production WSGI server instead.
flask_container |  * Debug mode: off
flask_container |  * Running on all addresses.
flask_container |    WARNING: This is a development server. Do 
not use it in a production deployment.
flask_container |  * Running on http://172.18.0.2:5000/ (Press 
CTRL+C to quit)

How can I solve this issue ? Thanks for your help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source