Skip to content

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.

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.

  1. SSH: Securely connect to the Linux server using SSH keys (disable password login).
  2. Docker Installation: Install the official Docker Engine and Docker Compose plugins via the OS package manager (apt or yum). Do not use Snap or default repository versions as they are often heavily outdated.
  3. 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).
  4. DNS: Point your domain name (e.g., api.myapp.com) to the public IP address of your VPS using an A-Record.
  5. Environment Configuration: Securely transfer your production .env file to the server (never commit it to Git).
  6. Docker Compose: Pull the latest images and start the stack using docker compose -f compose.prod.yaml up -d.

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.

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.

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.

Modern web browsers mandate HTTPS. Without it, your application is completely insecure and browsers will warn users away.

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

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.

  1. Nginx holds the SSL certificates.
  2. Nginx decrypts the incoming HTTPS traffic (“SSL Termination”).
  3. Nginx passes the decrypted, plain-text HTTP traffic over the secure internal Docker network to the application container.

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.

When you push a new version of your application (e.g., v2.0), you need a strategy for replacing the old v1.0 containers.

The simplest, but most disruptive method.

  • How it works: Stop v1.0, delete the container, and start v2.0.
  • Pros: Extremely simple. Guarantees no two versions are running simultaneously.
  • Cons: Causes hard downtime (from seconds to minutes) while the new container boots.

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 with v2.0, waits for it to become healthy, and then moves to the next container.
  • Pros: Zero downtime.
  • Cons: Both v1.0 and v2.0 are serving traffic simultaneously during the rollout, meaning your database schema must support both versions.

The safest high-availability strategy.

  • How it works: You have the entire v1.0 stack running (Blue). You spin up a completely separate, fully identical v2.0 stack 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.
  • How it works: You deploy v2.0 alongside v1.0, but you configure the load balancer to only send 5% of your live user traffic to v2.0 (the “Canary in the coal mine”). If errors spike, you route traffic back. If it’s stable, you slowly increase it to 100%.

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.