having on running flask app on docker container : Error: python: can't open file '//run.py': [Errno 2] No such file or directory Docker Error

Issue

please see this link to see the folder structure:
https://drive.google.com/file/d/1KceimcgGMN68Z0gDet2G6SzZIQUhkQ2G/view?usp=sharing

Hi, I am having issue with running my flask app on docker container, please help.
this is the Dockerfile code:

from alpine:latest

RUN apk add --no-cache python3.9.7-dev \
    && apk add --no-cache py3-pip \
    && pip install --upgrade pip


WORKDIR /NewBackend


COPY . /NewBackend

RUN pip3 --no-cache-dir install -r requirements.txt
FROM python:3.9.7-slim

RUN apt-get update \
    && apt-get -y install libpq-dev gcc \
    && pip install psycopg2

EXPOSE 5000


#ENTRYPOINT ["python"]
CMD ["python","run.py"]

Solution

You’re copying the files into the alpine container, which is not the final image. Your final image has only libpq-dev, gcc and psycopg2 installed.

Every time you use FROM, you’re starting the image creation from scratch and only the last one is exposed. Multi-stage builds are used in order to avoid build dependencies on the final image and you may copy files from them using COPY.

I suggest you try the following Dockerfile:

FROM python:3.9.7-slim


WORKDIR /NewBackend


COPY . /NewBackend

RUN pip3 --no-cache-dir install -r requirements.txt


RUN apt-get update \
    && apt-get -y install libpq-dev gcc \
    && pip install psycopg2

EXPOSE 5000


#ENTRYPOINT ["python"]
CMD ["python","run.py"]

If you’d really like to use multi-stage builds to avoid cluttering your image, I suggest you check how the files are moved from a intermediate container into the final image in this link: https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds

Answered By – Mateus Terra

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published