π§ͺ Lab β Working With Docker & Nginx Web Server
Duration: ~1 hour
Goal: Learn how to download container images, run containers, modify content inside them, and serve local files using bind mounts.
π Learning Objectives
By the end of this lab, you will be able to:
- Pull and compare Docker images from Docker Hub
- Run containers with port forwarding
- Inspect container logs and state
- Modify files inside a running container
- Replace container content from the host
- Understand why changes inside a container do not persist after removal
- Serve your own web content using Docker bind mounts
1οΈβ£ Install Docker on Windows (Docker Desktop)
1. Check Requirements
- OS: Windows 10 / 11, 64-bit
- Virtualization enabled in BIOS (for WSL 2 / HyperβV)
2. (Recommended) Enable WSL 2
-
Open PowerShell as Administrator.
-
Run:
wsl --install -
Restart your PC if asked.
-
Optionally open Microsoft Store and install a Linux distro (e.g., Ubuntu), if you want a full Linux shell later.
3. Download Docker Desktop for Windows
- Go to:
https://docs.docker.com/desktop/setup/install/windows-install/ - Click Download Docker Desktop for Windows.
- Save the installer (e.g.
Docker Desktop Installer.exe).
4. Install Docker Desktop
- Doubleβclick
Docker Desktop Installer.exe. - In the installer:
- Accept the license.
- Make sure βUse WSL 2 instead of HyperβVβ is checked (recommended).
- Click Install and wait for it to finish.
- Restart your PC if prompted.
5. Start Docker Desktop
- Open Start Menu β Docker Desktop.
- Wait until the Docker whale icon in the system tray stops animating (it means Docker is up).
- The first time you run Docker, you may need to accept some prompts (resources, WSL integration, etc.).
6. Verify Docker Works
Open PowerShell or Command Prompt:
docker --version
You should see a version line, for example:
Docker version 27.x.x, build ...
Then test with the official hello-world image:
docker run hello-world
If you see a βHello from Docker!β message, Docker is correctly installed.
π§© Part 1 β Working With Nginx Images and Containers
Nginx is one of the most widely used web servers in the world. Docker Hub provides multiple ready-to-use Nginx images, including full Debian-based images and lightweight Alpine images.
In this part, you will pull the images, compare them, run a container, and interact with it.
πΉ Step 1 β Download Nginx Images
docker pull nginx:1.27.4
docker pull nginx:1.27.4-alpine
πΉ Step 2 β Compare Image Sizes
docker image ls nginx
πΉ Step 3 β Run Nginx in the Background With Port Forwarding
docker run -d --name mynginx -p 8000:80 nginx:1.27.4
curl http://localhost:8000/
Expected output: βWelcome to nginx!β
πΉ Step 4 β Confirm That the Container Is Running
docker ps
πΉ Step 5 β View Container Logs
docker logs mynginx
πΉ Step 6 β Stop, Start, and Remove the Container
docker stop mynginx
docker start mynginx
docker stop mynginx
docker rm mynginx
π§© Part 2 β Modifying Content Inside and Outside the Container
πΉ Step 1 β Start the Nginx Container Again
docker run -d --name mynginx -p 8000:80 nginx:1.27.4
πΉ Step 2 β Edit the Page Inside the Container
docker exec -it mynginx bash
cd /usr/share/nginx/html
apt update
apt install -y nano
nano index.html # change first line to: Welcome to MY nginx!
save & exit
curl http://localhost:8000/
You should see now the line you changed
πΉ Step 3 β Replace the Page From the Host
curl http://localhost:8000/index.html -o index.html
docker cp index.html mynginx:/usr/share/nginx/html/index.html
curl http://localhost:8000/
πΉ Step 4 β Remove Container and Create New One
docker stop mynginx
docker rm mynginx
docker run -d --name mynginx -p 8000:80 nginx:1.27.4
curl http://localhost:8000/
Changes are gone because container files do not persist after removal.
π§© Part 3 β Serving Your Own HTML Page Using a Local Folder
πΉ Step 1 β Create Local Folder
mkdir -p ~/mynginx-site
cd ~/mynginx-site
cat > index.html << 'EOF'
<!DOCTYPE html>
<html>
<body>
<h1>Hello from my local folder!</h1>
<p>This page is served by Docker bind mounts.</p>
</body>
</html>
EOF
πΉ Step 2 β Run Nginx Serving Local Content
Here we mount our current directory that contains the index.html to /usr/share/nginx/html.
This is the location inside the container where Nginx serves static files from.
docker run -d --name mynginx-local -p 8000:80 -v "$PWD":/usr/share/nginx/html:ro nginx:1.27.4
πΉ Step 3 β Validate
curl http://localhost:8000/
π§© Part 4 β Building a Custom Python Web API Image (Flask)
Project Setup
mkdir -p ~/myflask-app
cd ~/myflask-app
Create app.py:
from flask import Flask, jsonify
import os
app = Flask(__name__)
@app.route("/")
def index():
app_name = os.getenv("APP_NAME", "My Flask App")
return f"<h1>Welcome to {app_name}!</h1><p>This is running inside a Docker container.</p>"
@app.route("/api/health")
def health():
return jsonify(status="ok")
@app.route("/api/info")
def info():
return jsonify(
app=os.getenv("APP_NAME", "My Flask App"),
environment=os.getenv("APP_ENV", "development"),
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Create requirements.txt:
flask==3.0.0
Dockerfile (More Complex Example)
FROM python:3.13.2-slim
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
APP_ENV=production \
APP_NAME="My Dockerized Flask App"
RUN useradd -m appuser
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py .
RUN chown -R appuser:appuser /app
USER appuser
EXPOSE 5000
CMD ["python", "app.py"]
Build the Image
docker build -t myflask-app:1.0 .
docker images myflask-app:1.0
Run the Container
docker run -d --name myflask \
-p 5000:5000 \
-e APP_NAME="Lab 8 Flask Service" \
-e APP_ENV="development" \
myflask-app:1.0
Test endpoints:
curl http://localhost:5000/
curl http://localhost:5000/api/health
curl http://localhost:5000/api/info
π³ Step-by-Step Guide: Publishing a Docker Image to Docker Hub
π§© Part 1 — Create a Docker Hub Account
- Go to https://hub.docker.com/
- Click Sign Up
- Fill in:
- Username\
- Email\
- Password\
- Verify your email via the confirmation link.
π§© Part 2 — Log In From Your Terminal
docker login
You will be prompted for your Docker Hub username and password.
π§© Part 3 — Tag Your Local Docker Image
Syntax:
docker tag SOURCE_IMAGE:TAG USERNAME/REPOSITORY:TAG
Example:
docker tag myflask-app:1.0 USERNAME/myflask-app:1.0
π§© Part 4 — Push the Image to Docker Hub
docker push USERNAME/myflask-app:1.0
π§© Part 5 — Test Pulling the Image From Docker Hub
docker rm -f myflask
docker rmi myflask-app:1.0
docker pull USERNAME/myflask-app:1.0
π§© Part 6 — Run the Pulled Image
docker run -d -p 5000:5000 --name myflask geopat/myflask-app:1.0
Your app is now running from the Docker Hub–hosted image!