Track 3: Managing Multiple Containers
Step-by-Step Guide
Open Terminal:
Launch PowerShell if not default
Create a New Project Directory:
Navigate to the location where you want to create your project directory.
Create a new directory for your project:
mkdir fastapi-docker
cd fastapi-docker
code .This will open a new VScode window
Open the terminal in VSCode (Ctrl + `).
Create the
srcDirectory:Inside your project directory, create a
srcdirectory:
mkdir srcCreate
main.pyInsidesrc:Navigate into the
srcdirectory and create themain.pyfile:
Add Code to
main.py:Open
main.pyin VSCode and add the following code:
import redis
from fastapi import FastAPI
app = FastAPI()
# Connect to Redis
redis_client = redis.Redis(host='redis', port=6379, db=0)
@app.get("/")
def read_root():
redis_client.incr('hits')
hits = redis_client.get('hits').decode('utf-8')
return {"message": "Hello World", "hits": hits}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
redis_client.set(f'item:{item_id}', q)
stored_value = redis_client.get(f'item:{item_id}').decode('utf-8')
return {"item_id": item_id, "stored_value": stored_value}Create
requirements.txt:Go back to the root of your project directory and create a
requirements.txtfile:
cd ..
touch requirements.txtAdd the following dependencies to
requirements.txt:
fastapi
uvicorn
redisCreate the
Dockerfile:In the root of your project directory, create a
Dockerfile: DockerfileAdd the following content to the
Dockerfile:
#Get the latest image Pythton:3.14-slim
FROM python:3.13-slim
WORKDIR /code
#Copy requirements file
COPY ./requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY ./src ./src
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "80", "--reload"]Automate Docker creation using YML
Create docker-compose.yml file in parent directory
services:
app:
build: .
container_name: python-cont
command: uvicorn src.main:app --host 0.0.0.0 --port 80 --reload
ports:
- 80:80
volumes:
- .:/code
redis:
image: "redis:latest"
container_name: redis-cont
ports:
- "6379:6379"Run docker compose
docker compose up --build -dAccess the Application
Open your web browser and go to http://localhost:80. Any changes you make to main.py in the src directory will be automatically reflected in the running container.
Last updated