Consider this FastAPI app code running inside a Docker container. What will the response be when accessing http://localhost:8000/hello?
from fastapi import FastAPI app = FastAPI() @app.get('/hello') async def say_hello(): return {'message': 'Hello from Docker!'}
Think about what the FastAPI route returns as JSON.
The FastAPI app defines a GET endpoint at '/hello' that returns a JSON with key 'message' and value 'Hello from Docker!'. When accessed correctly, the container serves this JSON response.
You want to expose port 8000 in your Dockerfile for a FastAPI app. Which snippet correctly does this?
Check the Dockerfile syntax for exposing ports.
The correct Dockerfile syntax to expose a port is EXPOSE 8000 without quotes or extra words.
Given this Dockerfile CMD, the FastAPI app fails to start inside the container. What is the cause?
CMD ["uvicorn", "main:app"]
Think about how Docker networking works and what IP the app listens on.
By default, uvicorn listens on 127.0.0.1 inside the container, which is not accessible externally. Adding --host 0.0.0.0 makes it listen on all interfaces, allowing external access.
The Dockerfile sets ENV GREETING=Hi. Inside the FastAPI app, this code runs:
import os
from fastapi import FastAPI
app = FastAPI()
@app.get('/greet')
async def greet():
return {"greeting": os.getenv('GREETING')}What will the endpoint return?
Consider how environment variables set in Dockerfile are accessed in the app.
The environment variable GREETING is set in the Dockerfile and is accessible inside the container. The app reads it with os.getenv and returns its value.
Multi-stage builds help optimize Docker images. Which option correctly explains their main benefit for FastAPI apps?
Think about how build tools and runtime requirements differ.
Multi-stage builds let you install heavy build tools in one stage, then copy only the needed runtime files to the final image. This keeps the image small and efficient.