I am beginner on docker and i am trying to run a flask python app and I have the following problem when running docker-compose up
, throws error:
ModuleNotFoundError: No module named 'sqlalchemy'
This is a picture of the error: docker-compose up error
This is the files in my current directory my local directory
this is the content of my dockerfile
FROM python:3.8
WORKDIR /app
COPY . /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]
this is the content of my docker-compose.yml
version: '3'
services:
helloworld:
build: ./
ports:
- "5000:5000"
volumes:
- ./:/app
this is my requirements.txt
click==8.1.2
Flask==2.1.1
Flask-SQLAlchemy==2.5.1
greenlet==1.1.2
importlib-metadata==4.11.3
itsdangerous==2.1.2
Jinja2==3.1.1
MarkupSafe==2.1.1
mysqlclient==2.1.0
SQLAlchemy==1.4.35
Werkzeug==2.1.1
zipp==3.8.0
this is my app.py
from distutils.log import debug
from flask import Flask
from sqlalchemy.engine import create_engine
from sqlalchemy.orm import sessionmaker
from models import Base
import os
app = Flask(__name__)
@app.route("/")
def hello_world():
return "Flask sss"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
I tried tearing it down with docker-compose down
and when running docker-compose up
again same error.
Can someone point me to the right direction of how i can run flask app successfully ?
CodePudding user response:
Try with the following:
FROM python:3.8
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "./app.py" ]
And for your docker-compose:
version: '3'
services:
helloworld:
build: ./
ports:
- "5000:5000"
volumes:
- ./:/usr/src/app
To build up your container, you can directly run this inliner:
docker-compose down; docker-compose up --build --remove-orphans
If the above doesn't help, I'd inspection the container directly from inside. You can access it with the following:
docker-compose exec helloworld sh
From there, navigates to the directory with the files and make sure everything is where it's supposed to be; If files are messed up or missing, make sure your Dockerfile
and docker-compose.yml
(on your local pc) are in the root directory of your project.
That should help, but reach out in the comments if not!