A FastAPI container runs, but the browser gets no response on the mapped port
The page never loads at http://localhost:8000, no error in the browser or in docker logsShort answer
The container is up, but the request never reaches the app. Either the server listens on 127.0.0.1 inside the container, where the published port cannot reach it, or the -p mapping points at a port the app does not use. Bind to 0.0.0.0 and map the app's real port.
Fix it fast
1Find out which cause you have
Read the start-up line in the container log. It prints the address and port the server listens on.
docker ps docker logs my-container --tail 202Match what you see, then jump to its fix
What does the error look like?
The page never loads at http://localhost:8000, no error in the browser or in docker logsThe app works with fastapi dev on the host but not in the container
Cause 1 of 2
The server listens on 127.0.0.1 inside the container
1Check
fastapi dev, and Uvicorn by default, listen on 127.0.0.1, which is the machine talking to itself. Inside a container that means only the container. fastapi run listens on 0.0.0.0.
grep -n -E 'CMD|ENTRYPOINT' Dockerfile2Fix
Start the server with fastapi run, or give Uvicorn the address 0.0.0.0. Use the exec form for CMD.
CMD ["fastapi", "run", "app/main.py", "--port", "8000"]
# or with Uvicorn
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]3Verify
Rebuild and request the app from the host.
docker build -t my-app .
docker run -d -p 8000:8000 my-app
curl -i http://localhost:8000/Confirmed by fastapi.tiangolo.com
Cause 2 of 2
The published port does not match the port the app uses
1Check
-p takes HOST_PORT:CONTAINER_PORT. The container side must be the port in your CMD (the FastAPI Docker example sets --port 80 and runs the container with -p 80:80).
docker port my-container
docker logs my-container --tail 52Fix
Make the container side of -p the same as the --port in the command, and open the host side in the browser.
docker run -d --name my-container -p 8000:8000 my-app3Verify
The host port answers.
curl -i http://localhost:8000/Confirmed by fastapi.tiangolo.com
How do I stop it coming back?
- Use fastapi run, not fastapi dev, as the container command.
- Keep the app port in one place (an ARG or an environment variable) and use it for both --port and -p.
- Check docker logs for the listening address on the first run of any new image.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Servers
Uvicorn --reload never restarts when you edit code in a Docker container
Edits show on the host, but the container serves the old code
Uvicorn can only reload files that change inside the container.
4 causes, each with a fixServers
A Docker container port is not reachable from the host or network
The service answers inside the container but not from the host
A container port is closed to the outside until you publish it with -p or a ports entry.
3 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
pip install cryptography tries to compile and fails in a Docker build
fatal error: 'openssl/aes.h' file not found
cryptography ships prebuilt wheels.
3 causes, each with a fixServers
Docker build fails with apk: not found in a Python or Ubuntu image
/bin/sh: 1: apk: not found
apk is the package manager of Alpine Linux only.
2 causes, each with a fixServers
Docker cannot find manage.py or Permission denied on '/manage.py'
PermissionError: [Errno 13] Permission denied: '/manage.py'
The container is looking for manage.py in a directory where the file is not.
3 causes, each with a fix