Docker Flask App routing on windows system

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)

Please, format your post according to the following guide: How to format your forum posts
In short: please, use </> button to share codes, terminal outputs, error messages or anything that can contain special characters which would be interpreted by the MarkDown filter. Use the preview feature to make sure your text is formatted as you would expect it and check your post after you have sent it so you can still fix it.

Example code block:

```
services:
  service1:
    image: image1
```

After fixing your post, please send a new comment so people are notified about the fixed content.


1 Like

The indentation of your code is still wrong. But regarding the IP address, since the container is in a virtual machine, you can’t access the container IP. I wrote about Dcoker networking here

https://dev.to/rimelek/docker-network-and-network-namespaces-in-practice-5h9#running-a-web-browser-in-a-net-namespace-in-a-vm-docker-desktop

and I refer to it on other pages of the original source of that tutorial too

Note

Since Docker Desktop runs containers in a virtual machine, Container IP addresses are not available from the host. You can run curl in a container on host network:

docker run --rm -it --net host curlimages/curl $IP:8080
1 Like

Hello, thanks for the materials. Read them and tried a few methods .

when running the app with:

docker run -d -p 5000:5000 flask-app

the app runs. Then after running

docker run --rm -it --net host curlimages/curl http://172.17.0.2:5000

the terminal returns the full site html from index.html. when curling

winpty docker run --rm -it --net host curlimages/curl -X POST http://172.17.0.1:5000/send-message -H "Content-Type: application/json" -d '{ "userMessage": "test message" }'

the site returns true as reply from the post.
docker terminal message:

2024-10-31 13:32:40 172.17.0.1 - - [31/Oct/2024 12:32:40] "POST /send-message HTTP/1.1" 200 -`Preformatted text`

however when running the script locally the app runs normally and post returns the answer

2024-10-31 13:32:40 127.0.0.1 - - [31/Oct/2024 12:32:40] "POST /send-message HTTP/1.1" 200 -

Besides the accidentally included “Preformatted text” string, I don’t see any difference. Of course the source IP could be different when you run it from different environments. I never used the experimental Docker Desktop terminal