A basic FastAPI "Hello World" app packaged into a Docker image. Learn how to write a Dockerfile, build an image, and run a container.
cd 1-appBuild the image using the Dockerfile and run it locally (maps port 8080 on your machine to port 8080 in the container):
docker build . -t app
docker run -p 8080:8080 appOpen http://localhost:8080 in your browser to see the app.
To publish the image to a registry, tag it with a registry name and version, then push:
docker tag app <your-username>/app:v1
docker push <your-username>/app:v1Same app, but now it serves static files (HTML + images) from a www/ folder. A bind mount maps a folder on your host into the container, so changes you make to the files on your machine are immediately reflected inside the container — no rebuild needed.
cd 2-persistenceBuild and run with a bind mount (-v links the local www/ directory to /app/www inside the container):
docker build . -t app2
docker run -p 8080:8080 -v "$(pwd)"/www:/app/www app2Open http://localhost:8080, then try editing www/index.html on your host and refresh the page.
Introduces Docker Compose to run multiple containers together. This example sets up two services: the FastAPI app and an Nginx reverse proxy that forwards traffic to it. Compose handles building, networking, and starting both containers with a single command.
cd 3-composeStart both services (Compose builds the images automatically on the first run):
docker compose upOpen http://localhost:8081 — the request hits Nginx, which proxies it to the app container. Notice how Compose creates a shared network so Nginx can reach the app by its service name (app).
docker network lsStop and remove the container services once you're done:
docker compose downBuilds on the previous example by adding environment variables and Docker configs. The app reads an env var (my-env-variable) and injects its value into the HTML page. Nginx is configured using a Docker configs object instead of a custom Dockerfile.
cd 4-configStart the services:
docker compose upOpen http://localhost:8081 — you should see the value of hello from the compose file! rendered on the page. Try changing the value in docker-compose.yml and restarting to see the effect.
Stop and remove the container services once you're done:
docker compose downAdds SSL/TLS to the setup using Docker secrets to securely provide certificates to the Nginx container. Secrets are mounted as files inside the container and are never exposed in image layers or environment variables.
cd 5-secretsFirst, generate a self-signed certificate:
sh create_cert.shThen start the services:
docker compose upOpen https://localhost:4431 (your browser will warn about the self-signed certificate — that's expected). HTTP still works on http://localhost:8081.
Stop and remove the container services once you're done:
docker compose downCleanup your Docker system, removing all dangling containers, unused images, volumes and networks
docker system prune --all
- Docker Docs
- Docker Compose Section
- Lab Docker do Prof. João Paulo Barraca