HTTPConnectionPool Max retries exceeded with url within flask request app

Macos: 14.3.1
MacBook Pro 1.4 GHz Quad-Core Intel Core i5, 8 GB memory
Docker Desktop App Version: 4.30.0 (149282) Engine: 26.1.1
Settings:
CPU Limit: 8
Memory Limit: 6gb
Swap: 2 gb
Virtual Disk: 64gb
Docker Disk Available: 46gb of 62gb
Ram 5.12gb used
CPU used: 5.39%
Signed in

Main issue:

HTTPConnectionPool(host='app.backy.localhost', port=80): Max retries exceeded with url: /posty (Caused by NameResolutionError(

I have a flask app requesting from a fastapi app, both on the same traefik network via host urls.
This setup below mirrors to the best of my abilites the clients current setup. When I attempt a hard coded request for simplicitys sake, I get the above error. I have tried all forms of things and editing my hosts file in their app does give me a different result of not found, but for my task here, just solving it without hosts would be nice.
Note, I CAN curl into it and get the result expected, and I can get the result out with a simple request only script not apart of the docker project. So its something configurable I just don’t understand yet.
Thank you.

backserver dockerfile

FROM python:3.11-alpine
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
COPY . /app
WORKDIR /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
CMD python -m uvicorn main:app --host 0.0.0.0 --port 8000

backserver main.py

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
    CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],
)

@app.get("/getball")
def read_root():
    return {"Hi": "yo"}

@app.post("/posty")
async def create_item(item: Item):
    print("in posty")
    items.append(item)
    return {"message": "Item created successfully", "item": item}

frontserver dockerfile

FROM python:3.11-alpine
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
COPY . /app
WORKDIR /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
ENTRYPOINT [ "python" ]
CMD ["main.py"]

frontserver main.py

from flask import Flask, request, render_template, redirect, url_for, jsonify
import requests
import pdb
app = Flask(__name__)

@app.route('/getball', methods=['GET'])
def getball():
    try:
        response = requests.get('http://app.backy.localhost/getball')
        response.raise_for_status()  # Raise an HTTPError for bad responses
        data = response.json()
    except requests.exceptions.RequestException as e:
        data = {"error": str(e)}

    print(data)
    return (data)

@app.route('/post', methods=['POST'])
def submit():
    print("in submit two")
    try:
        response = requests.post(f"http://app.backy.localhost/posty", json={
            "name": "fiiiiish",
            "price": 7
        })

        if response.status_code == 200:
            print("Success!")
            print(response.json())
            return response.text
        else:
            print(f"Failed to retrieve data. Status code: {response.status_code}")
            print(response.text)
            return response.text
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}")

    return "¿¿"

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

docker-compose

services:
  frontserver:
    container_name: frontserver
    build:
      context: ./fakeserver
      dockerfile: ./Dockerfile
    restart: always
    networks:
      - narf-network
    ports:
      - "5000:5000"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.frontserver.rule=Host(`app.fronty.localhost`)"
      - "traefik.http.routers.frontserver.entrypoints=web"
      - "traefik.http.services.frontserver.loadbalancer.server.port=5000"


  backserver:
    container_name: backserver
    build:
      context: ./backserver
      dockerfile: ./Dockerfile
    restart: always
    networks:
      - narf-network
    ports:
      - "8000:8000"
    command: python -m uvicorn main:app --host 0.0.0.0 --port 8000
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.backserver.rule=Host(`app.backy.localhost`)"
      - "traefik.http.routers.backserver.entrypoints=web"
      - "traefik.http.services.backserver.loadbalancer.server.port=8000"


  traefik:
    image: "traefik:v2.10"
    container_name: "traefik"
    command:
      - "--api=true"
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--api.dashboard=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.rule=Host(`traefik.narf.localhost`)"
      - "traefik.http.routers.traefik.entrypoints=web"
      - "traefik.docker.network=narf-network"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    networks:
      - narf-network
# volumes:
#   backend-db-data:
#   test-backend-db-data:

networks:
  narf-network:
    driver: bridge
    # external: true

Logs with and /etc/hosts config
POST

2024-06-13 02:05:15 An error occurred: HTTPConnectionPool(host='app.backy.localhost', port=80): Max retries exceeded with url: /posty (Caused by NameResolutionError("<urllib3.connection.HTTPConnection object at 0x7f40b5719610>: Failed to resolve 'app.backy.localhost' ([Errno -2] Name does not resolve)"))
2024-06-13 02:05:15 172.28.0.3 - - [13/Jun/2024 06:05:15] "POST /submit HTTP/1.1" 200 -

GET

2024-06-13 02:05:18 {'error': 'HTTPConnectionPool(host=\'app.backy.localhost\', port=80): Max retries exceeded with url: /getball (Caused by NameResolutionError("<urllib3.connection.HTTPConnection object at 0x7f40b5718a90>: Failed to resolve \'app.backy.localhost\' ([Errno -2] Name does not resolve)"))'}
2024-06-13 02:05:18 172.28.0.3 - - [13/Jun/2024 06:05:18] "GET /getball HTTP/1.1" 200 -

/etc/hosts

127.0.0.1 app.fronty.localhost
127.0.0.1 app.backy.localhost

It was solved by super co-worker Mr Wo!!
The internal at least for these settings communicate via the container name AND port
so in this case

http//backserver:8000/getball

that simple but not obvious even after reading the docs. That said, that would be troublesome if it cant reach the otherwise world, but thats a test for another book…