Python Forum

Full Version: Docker Flask App on windows system
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!

I'm trying to build a flask chat app but facing following issue (on windows)

sending post and get requests works fine locally through
127.0.0.1 - - [26/Oct/2024 16:54:56] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [26/Oct/2024 16:55:17] "POST /send-message HTTP/1.1" 200 -


but as soon as the app is made into a docker container
the app opens through localhost:5000 and 127.0.0.1:5000 but port 172.17.0.1:5000 refuses to connect.
when request is run through localhost:5000 or 127.0.0.1:5000 the docker app automatically routes it through
172.17.0.1 - - [26/Oct/2024 16:54:56] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [26/Oct/2024 16:55:17] "POST /send-message HTTP/1.1" 200 -
which does not get the response.

is there a way to get the 172.17.0.1 to connect or a way to route the post and get through 127.0.0.1 how to change the docker default

from
172.17.0.1 - - [26/Oct/2024 16:54:56] "GET / HTTP/1.1" 200 -

172.17.0.1 - - [26/Oct/2024 16:55:17] "POST /send-message HTTP/1.1" 200 -

Dockerfile
FROM python:3.10-slim

#container workdir
WORKDIR /app

#install chromium and dependencies
RUN apt-get update && apt-get install -y \
    chromium \
    chromium-driver \
    libglib2.0-0 \
    libnss3 \
    libx11-xcb1 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxi6 \
    libxrandr2 \
    libxrender1 \
    libdbus-glib-1-2 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libfontconfig1 \
    libx11-6 \
    libx11-xcb-dev \
    libxcb1 \
    libxss1 \
    fonts-liberation \
    libappindicator3-1 \
    xdg-utils \
    wget \
    unzip \
    && rm -rf /var/lib/apt/lists/*

RUN chmod +x /usr/bin/chromium && chmod +x /usr/bin/chromedriver

#copy dir contents to container
COPY . /app

#install packages
#RUN pip install -r requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

#expose app port

ENV HOST=0.0.0.0
ENV LISTEN_PORT 5000
EXPOSE 5000

#headless
ENV CHROME_BIN=/usr/bin/chromium
ENV CHROMEDRIVER_PATH=/usr/bin/chromedriver

#run
ENV FLASK_APP=app_local_new.py
ENV FLASK_ENV=production

#tested commands 
CMD ["gunicorn", "-b", "0.0.0.0:5000",  "app_local_new:app"]
#CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "--preload", "app_local_new:app"]
#CMD gunicorn -b 0.0.0.0:${PORT:-5000} "app_local_new:app"
docker-compose.yml
version: "3"
services:
  app_local_new:
    build: .
    ports:
      - "5000:5000"
    networks:
      - app-networks

networks:
  app-networks:
    driver: bridge
app_local_new.py
@app.route('/send-message', methods=['POST'])
def send_message():
    data = request.get_json()
    user_message = data.get('userMessage', '')
    print(user_message)