Docker Containers

πŸ§ͺ 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)
  1. Open PowerShell as Administrator.

  2. Run:

    wsl --install
    
  3. Restart your PC if asked.

  4. 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

  1. Go to:
    https://docs.docker.com/desktop/setup/install/windows-install/
  2. Click Download Docker Desktop for Windows.
  3. Save the installer (e.g. Docker Desktop Installer.exe).

4. Install Docker Desktop

  1. Double‑click Docker Desktop Installer.exe.
  2. In the installer:
    • Accept the license.
    • Make sure β€œUse WSL 2 instead of Hyper‑V” is checked (recommended).
  3. Click Install and wait for it to finish.
  4. Restart your PC if prompted.

5. Start Docker Desktop

  1. Open Start Menu β†’ Docker Desktop.
  2. Wait until the Docker whale icon in the system tray stops animating (it means Docker is up).
  3. 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

  1. Go to https://hub.docker.com/
  2. Click Sign Up
  3. Fill in:
    • Username\
    • Email\
    • Password\
  4. 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!


Previous