This is a minimal example of running a FastAPI application inside a Docker container. The app exposes a single endpoint that returns a simple JSON message.
main.py
: FastAPI application source codeDockerfile
: Image build instructionsrequirements.txt
: Python dependencies
docker build -t fastapi-example .
docker run -d -p 8000:8000 --name fastapi-example fastapi-example
The app will be available at http://localhost:8000
You can test the root endpoint with curl or your browser:
curl http://localhost:8000/
Expected response:
{"message": "Hello World"}
To view the logs from the running FastAPI container, use:
docker logs fastapi-example
docker stop fastapi-example
docker rm fastapi-example