type here
root@ip-172-31-40-102:~# docker logs 47821a8d780a
Traceback (most recent call last):
File "/app/app.py", line 1, in
from flask import Flask, render_template
ModuleNotFoundError: No module named ‘flask’
Use an official Python runtime as the base image
FROM python:3.9 AS backend-builder
Set the working directory in the container
WORKDIR /app
Copy the backend code into the container
COPY ./backend /app
Install Python dependencies, including Flask
RUN pip install -r requirements.txt
#——————Stage 1 Ends———————-
#——————Stage 2 Start———————
FROM python:3.9-slim-buster
Set the working directory in the container
WORKDIR /app
Copy the code from the backend-builder stage
COPY –from=backend-builder /app /app
Expose the port
EXPOSE 5000
Command to run the application
CMD ["python", "app.py"]
i want run the docker container but i not able to run contarinter i facing this error ModuleNotFoundError: No module named ‘flask’
please help to understand how to fix this issue
2
Answers
Use an official Python runtime as the base image
FROM python:3.8 AS backend-builder
Set the working directory in the container
WORKDIR /app
Copy the backend code into the container
COPY ./backend /app
Install Python dependencies, including Flask
RUN pip install -r requirements.txt
#------------------Stage 1 Ends---------------------- #------------------Stage 2 Start--------------------- FROM python:3.8-slim
Set the working directory in the container
WORKDIR /app
Copy the code from the backend-builder stage
COPY --from=backend-builder /app /app
Expose the port
EXPOSE 5000
Command to run the application
CMD ["python", "app.py"]
I also change the Python versions but not working Please check my docker file it is correct or not
You are only installing your requirements to the build stage, they are not available globally in the final stage.
What you have in the final stage is only what you copy with this
and since you are not using a virtual env but installing requirements globally, you are not copying whatever you install with
Solution 1
A solution that would work for this particular Dockerfile would be to not do a multistage build, since you are not installing anything heavy in the build stage, basically only stuff that you need for the final stage anyway.
So something like this should work:
Solution 2
If you have more stuff going on in the build stage that you don’t want in the final image, your solution needs a virtual env step so that requirements are installed locally.
See for example https://stackoverflow.com/a/48562835/6845813.