Lab 2 of 8 · Containers with Docker
Package your app as a Docker image
Write a Dockerfile, build an image of your app, run it as a container and check its logs.
- Time
- 45–60 minutes
- Cost
- Free.
What you learn
A container packages your app with everything it needs, so it runs the same on your laptop, a CI server and the cloud. Almost every modern deployment starts with an image.
- Images, containers and layers
- Writing a Dockerfile that runs as a non-root user
- Ports, logs and stopping containers
- Keeping images small with .dockerignore
In the AI era: AI models and AI apps are shipped as container images too. The same skills you learn here are how teams package model servers.
Before you start
Finish lab 1.
Install Docker Desktop (Windows: turn on the WSL integration). Check with
docker --version.
Step by step
1 Write the Dockerfile
Each line is a step in building the image. The app runs as a normal user, not as root: a basic security habit.
FROM python:3.12-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY app ./app RUN useradd --create-home appuser USER appuser EXPOSE 8000 CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]2 Keep junk out of the image
Without this file, Docker would copy your virtual environment and Git history into the build.
.venv .git __pycache__ .pytest_cache tests3 Build the image
The
-tflag gives the image a name and version tag. The dot means "build from this folder".docker build -t ai-status-app:1.0 . docker images ai-status-appYou should see:A line for
ai-status-appwith tag1.0.4 Run it as a container
-druns it in the background and-p 8000:8000connects port 8000 on your computer to port 8000 in the container.docker run -d --name ai-status -p 8000:8000 ai-status-app:1.0 curl http://localhost:8000/healthYou should see:{"status":"ok"}.5 Look inside
Logs are the first thing you check when something breaks.
docker execruns a command inside the running container.docker logs ai-status docker exec ai-status whoami docker psYou should see:Uvicorn startup lines, then
appuser, then your container in the list.6 Stop and remove it
Containers are disposable. The image stays, so you can start a new one any time.
docker stop ai-status docker rm ai-status7 Commit and push
Your repository now describes how to build the app anywhere.
git add Dockerfile .dockerignore git commit -m "Add Dockerfile" git push
You are done when
docker runserves{"status":"ok"}on port 8000.docker exec ai-status whoamiprintedappuser, notroot.The Dockerfile is on GitHub.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.