0
0
FastAPIframework~20 mins

Docker containerization in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Docker FastAPI Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when running this FastAPI app inside a Docker container?

Consider this FastAPI app code running inside a Docker container. What will the response be when accessing http://localhost:8000/hello?

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get('/hello')
async def say_hello():
    return {'message': 'Hello from Docker!'}
A{"message": "Hello from Docker!"}
B404 Not Found error
C500 Internal Server Error
DEmpty response with status 200
Attempts:
2 left
💡 Hint

Think about what the FastAPI route returns as JSON.

📝 Syntax
intermediate
1:30remaining
Which Dockerfile snippet correctly exposes the FastAPI app port?

You want to expose port 8000 in your Dockerfile for a FastAPI app. Which snippet correctly does this?

AEXPOSE port 8000
BEXPOSE '8000'
CEXPOSE 8000
DEXPOSE:8000
Attempts:
2 left
💡 Hint

Check the Dockerfile syntax for exposing ports.

🔧 Debug
advanced
2:30remaining
Why does the FastAPI app inside Docker fail to start with this command?

Given this Dockerfile CMD, the FastAPI app fails to start inside the container. What is the cause?

FastAPI
CMD ["uvicorn", "main:app"]
Auvicorn is not installed in the container
BMissing the --host 0.0.0.0 option to listen on all interfaces
CCMD should be a shell command string, not a JSON array
DThe app should be run with python main.py instead
Attempts:
2 left
💡 Hint

Think about how Docker networking works and what IP the app listens on.

state_output
advanced
2:00remaining
What is the value of the environment variable inside the FastAPI app running in Docker?

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?

A500 Internal Server Error
B{"greeting": null}
C{"greeting": ""}
D{"greeting": "Hi"}
Attempts:
2 left
💡 Hint

Consider how environment variables set in Dockerfile are accessed in the app.

🧠 Conceptual
expert
3:00remaining
Which statement best explains why multi-stage builds are used in Docker for FastAPI apps?

Multi-stage builds help optimize Docker images. Which option correctly explains their main benefit for FastAPI apps?

AThey separate build dependencies from runtime, reducing final image size
BThey allow running multiple FastAPI apps in one container
CThey enable automatic scaling of FastAPI apps inside Docker
DThey provide a GUI to manage Docker containers
Attempts:
2 left
💡 Hint

Think about how build tools and runtime requirements differ.