19. Production Deployment
Deploying a Dockerized application to production requires more than just running docker compose up. You must secure the Linux host server, properly route incoming web traffic using a Reverse Proxy, terminate SSL/TLS for secure HTTPS connections, and choose a safe deployment strategy to minimize downtime.
19.1 Linux VPS Deployment
Section titled “19.1 Linux VPS Deployment”For many small to medium applications, deploying to a single Linux Virtual Private Server (VPS) via providers like DigitalOcean, AWS EC2, or Linode is the standard approach.
The Deployment Checklist
Section titled “The Deployment Checklist”- SSH: Securely connect to the Linux server using SSH keys (disable password login).
- Docker Installation: Install the official Docker Engine and Docker Compose plugins via the OS package manager (
aptoryum). Do not use Snap or default repository versions as they are often heavily outdated. - Firewall: Configure a firewall (like UFW or AWS Security Groups). The absolute minimum requirement is to block all incoming traffic except for ports 22 (SSH), 80 (HTTP), and 443 (HTTPS).
- DNS: Point your domain name (e.g.,
api.myapp.com) to the public IP address of your VPS using an A-Record. - Environment Configuration: Securely transfer your production
.envfile to the server (never commit it to Git). - Docker Compose: Pull the latest images and start the stack using
docker compose -f compose.prod.yaml up -d.
19.2 Reverse Proxy
Section titled “19.2 Reverse Proxy”You should rarely expose an application container directly to the internet (e.g., mapping a Node.js container straight to port 80). Instead, you place a highly optimized Reverse Proxy in front of your application.
The Architecture
Section titled “The Architecture” Internet ↓ Cloudflare (DNS & WAF) ↓ (Port 443 / HTTPS) Reverse Proxy (Nginx / Traefik on the VPS) ↓ (Internal Docker Network) Application Container (Node.js / Python)The industry standard web server and reverse proxy. Nginx receives all incoming HTTP/HTTPS traffic on ports 80 and 443. It inspects the requested domain name, handles the SSL encryption, and routes the raw traffic to the appropriate internal Docker container on the shared Docker bridge network.
Traefik (Concept)
Section titled “Traefik (Concept)”A modern, Docker-aware reverse proxy. Unlike Nginx which requires manual configuration files, Traefik automatically talks to the Docker Daemon. When you spin up a new container, Traefik instantly detects it, generates an SSL certificate, and begins routing traffic to it with zero manual configuration required.
19.3 HTTPS & SSL Termination
Section titled “19.3 HTTPS & SSL Termination”Modern web browsers mandate HTTPS. Without it, your application is completely insecure and browsers will warn users away.
TLS & Certificates
Section titled “TLS & Certificates”Transport Layer Security (TLS) encrypts the traffic between the user’s browser and your server. To use TLS, you need a cryptographic certificate verified by a Certificate Authority (like Let’s Encrypt).
Reverse Proxy Termination
Section titled “Reverse Proxy Termination”It is highly inefficient for your Node.js or Python application to handle SSL encryption itself. Instead, the Reverse Proxy (Nginx) handles all the heavy cryptographic math.
- Nginx holds the SSL certificates.
- Nginx decrypts the incoming HTTPS traffic (“SSL Termination”).
- Nginx passes the decrypted, plain-text HTTP traffic over the secure internal Docker network to the application container.
HTTP → HTTPS Redirection
Section titled “HTTP → HTTPS Redirection”The Reverse Proxy must be configured to intercept any unsecured HTTP requests hitting port 80 and immediately issue a 301 Redirect to force the user’s browser onto the secure HTTPS port 443.
19.4 Deployment Strategies
Section titled “19.4 Deployment Strategies”When you push a new version of your application (e.g., v2.0), you need a strategy for replacing the old v1.0 containers.
Recreate Deployment
Section titled “Recreate Deployment”The simplest, but most disruptive method.
- How it works: Stop
v1.0, delete the container, and startv2.0. - Pros: Extremely simple. Guarantees no two versions are running simultaneously.
- Cons: Causes hard downtime (from seconds to minutes) while the new container boots.
Rolling Deployment (Concept)
Section titled “Rolling Deployment (Concept)”The standard for clustered orchestration (like Docker Swarm or Kubernetes).
- How it works: If you have 5 containers running
v1.0, the orchestrator takes down one container, replaces it withv2.0, waits for it to become healthy, and then moves to the next container. - Pros: Zero downtime.
- Cons: Both
v1.0andv2.0are serving traffic simultaneously during the rollout, meaning your database schema must support both versions.
Blue/Green Deployment (Concept)
Section titled “Blue/Green Deployment (Concept)”The safest high-availability strategy.
- How it works: You have the entire
v1.0stack running (Blue). You spin up a completely separate, fully identicalv2.0stack next to it (Green). Once Green is fully tested and healthy, you instantly flip the load balancer/reverse proxy to send all traffic to Green. - Pros: Instantaneous cutover. Zero downtime. If Green fails, you instantly flip the router back to Blue.
- Cons: Requires double the server resources (CPU/RAM) since two full environments are running at the same time.
Canary Deployment (Concept)
Section titled “Canary Deployment (Concept)”- How it works: You deploy
v2.0alongsidev1.0, but you configure the load balancer to only send 5% of your live user traffic tov2.0(the “Canary in the coal mine”). If errors spike, you route traffic back. If it’s stable, you slowly increase it to 100%.
Rollback
Section titled “Rollback”If a deployment fails catastrophically, a rollback strategy is your escape hatch. Because Docker images are immutable, rolling back is as simple as updating your compose.yaml to point back to the previous tag (e.g., changing myapp:v2.0 back to myapp:v1.9) and running docker compose up -d.