Tutorial — 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 tutorial, 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
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!