Containers

Docker & Reverse Proxy Strategy

Containerization ensures that applications run identically in development and production. By pairing Docker with an Nginx Reverse Proxy, we create a secure, scalable deployment pipeline.

🐳 The Workflow: Build the app image → Run container → Expose securely via Nginx (HTTPS).

Step 1: Dockerize the Web App

The Dockerfile Structure

This file contains instructions for building the Docker image.

Dockerfile
FROM python:3.9
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]
  • FROM: The base OS/image (Python, Node, etc).
  • WORKDIR: Sets the working folder inside the container.
  • COPY: Moves your code from host to container.
  • CMD: The command that starts your app.

Build & Run Commands

1. Build the Image

docker build -t my-web-app .

2. Run the Container

docker run -d -p 8080:5000 my-web-app

-d: Detached (background) mode.
-p 8080:5000: Maps host port 8080 to container port 5000.

🛠️ Troubleshooting & Management

Confirm File Paths

Get inside the container to debug:

docker exec -it my_container bash
cd /usr/src/app
ls

Volume Mounting (Dev)

Sync local folder with container for live updates:

docker run -d -p 8080:80 \
 -v "$(pwd):/usr/share/nginx/html" \
 nginx

Copy File (Quick Fix)

Inject a file without rebuilding:

docker cp index.html my_container:/path/
MethodUse CaseAction Needed
RebuildProduction / Permanent changesdocker build + run
Volume MountActive DevelopmentNone (Updates Instantly)
docker cpQuick PatchRestart Service

Step 2: Add Security (SSL/TLS)

Never expose a container directly to the internet. Use Nginx as a Reverse Proxy to handle HTTPS encryption.

1. Install Nginx

sudo apt update
sudo apt install nginx
sudo systemctl enable nginx

2. Configure Reverse Proxy

Edit default config: /etc/nginx/sites-available/default

server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://localhost:8080; # Points to your Docker Container
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

3. Enable HTTPS (Let's Encrypt)

Use Certbot to automatically generate SSL certificates.

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com

✅ This automatically updates your Nginx config with the SSL keys.