published: false
description: A step-by-step guide to running a Docker container bound to localhost, routing traffic through Nginx, and automating HTTPS with Let's Encrypt on Ubuntu 24.04.
tags: devops, docker, nginx, linux
Running a containerized web application directly on a public port like :3000 or :8080 works during local development, but it creates serious security and reliability gaps in production. Exposing a container port directly to the internet leaves your traffic unencrypted over plain HTTP, makes managing multiple services on a single server difficult, and bypasses reverse-proxy protections like rate limiting and clean header forwarding.
In this guide, you will deploy a containerized web service on an Ubuntu 24.04 LTS server using a production-ready architecture. Instead of exposing the container to the public internet, you will bind it strictly to 127.0.0.1, route incoming requests through Nginx, and secure your domain with an auto-renewing Let's Encrypt SSL certificate.
Architecture Overview
By the end of this tutorial, all external traffic will follow this request flow:
Internet (HTTPS :443) ──▶ UFW Firewall ──▶ Nginx Reverse Proxy ──▶ Docker Container (127.0.0.1:8080)
Prerequisites
Before you begin, make sure you have:
- An Ubuntu 24.04 LTS cloud server (VPS) with at least 1 GB of RAM and a non-root user with
sudoprivileges. - A registered domain or subdomain (such as
://yourdomain.com) with a DNS A record pointing to your server's public IPv4 address. - SSH access to your server.
Note: Replace
://yourdomain.comwith your actual domain name throughout every command and configuration file in this tutorial.
Step 1 — Configure the Firewall and Install Dependencies
Start by updating your server's package index and installing the prerequisite packages needed to add Docker's official repository alongside Nginx.
sudo apt update && sudo apt upgrade -y
sudo apt install -y ca-certificates curl gnupg nginx ufw
Next, add Docker's official GPG key and repository so you install the maintained Docker Engine rather than an outdated distribution package:
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://docker.com | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://docker.com \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
Add your non-root user to the docker group so you can manage containers without prefixing every command with sudo:
sudo usermod -aG docker $USER
newgrp docker
Now configure UFW (Uncomplicated Firewall) to allow SSH connections and standard web traffic (80 and 443 via the Nginx Full profile) while blocking all other incoming ports:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw --force enable
Verify both your firewall rules and Docker installation:
sudo ufw status
docker --version
You should see output confirming that ports 22, 80, and 443 are open and Docker is installed:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)
Docker version 27.3.1, build ce12230
Step 2 — Run the Sample Container Bound to Localhost
When developers run docker run -p 8080:80 ..., Docker binds port 8080 to 0.0.0.0 (every network interface on the host) and automatically modifies iptables rules—often bypassing UFW completely. That means anyone who knows your server's IP address could visit http://YOUR_SERVER_IP:8080 and bypass Nginx and HTTPS altogether.
To prevent this, explicitly bind the host port to 127.0.0.1 so only processes running on the same server (like Nginx) can reach the container.
Launch a lightweight demo web container (nginxdemos/hello) bound strictly to localhost:
docker run -d \
--name web-app \
--restart unless-stopped \
-p 127.0.0.1:8080:80 \
nginxdemos/hello
Here is what each flag accomplishes:
-
-druns the container in detached mode in the background. -
--name web-appassigns a predictable name for logs and restarts. -
--restart unless-stoppedensures the container starts automatically if the server reboots. -
-p 127.0.0.1:8080:80maps container port80exclusively to port8080on the host's loopback interface.
Confirm the container is running and responds locally:
docker ps
curl -I http://127.0.0.1:8080
Expected output:
HTTP/1.1 200 OK
Server: nginx/1.25.3
Content-Type: text/html
Connection: keep-alive
Step 3 — Configure Nginx as a Reverse Proxy
Now configure Nginx to accept public requests for ://yourdomain.com and forward them internally to http://127.0.0.1:8080.
Create a dedicated server block configuration file in /etc/nginx/sites-available/:
sudo nano /etc/nginx/sites-available/://yourdomain.com
Paste the following configuration, replacing ://yourdomain.com with your actual domain:
server {
listen 80;
listen [::]:80;
server_name ://yourdomain.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Why These Proxy Headers Matter
-
Host $hostpreserves the original domain requested by the client rather than passing127.0.0.1:8080. -
X-Real-IPandX-Forwarded-Forpass the visitor's real IP address to your application logs instead of showing every request as coming from127.0.0.1. -
X-Forwarded-Protolets your backend app know whether the visitor connected overhttporhttps.
Save and close the file (Ctrl + O, Enter, Ctrl + X in nano). Next, enable the site by creating a symbolic link in sites-enabled, remove the default welcome site if you no longer need it, and test your Nginx syntax:
sudo ln -s /etc/nginx/sites-available/://yourdomain.com /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t
If the configuration test succeeds, reload Nginx to apply the changes:
sudo systemctl reload nginx
Verify that your domain now serves the containerized application over HTTP:
curl -I http://://yourdomain.com
Step 4 — Secure the Domain with Let's Encrypt SSL
With HTTP routing confirmed, use Certbot to issue a free TLS certificate from Let's Encrypt and automatically configure Nginx to redirect all HTTP traffic to HTTPS.
Install Certbot and its Nginx plugin:
sudo apt install -y certbot python3-certbot-nginx
Run Certbot for your domain:
sudo certbot --nginx -d ://yourdomain.com
Certbot will prompt you for:
- An email address for urgent renewal and security notices.
- Agreement to the Let's Encrypt Terms of Service.
Once validated, Certbot modifies /etc/nginx/sites-available/://yourdomain.com to attach your SSL certificate and enable automatic HTTP-to-HTTPS redirection.
Verify HTTPS and Auto-Renewal
First, verify that HTTP requests now return a 301 Moved Permanently redirect to HTTPS:
curl -I http://://yourdomain.com
Expected output:
HTTP/1.1 301 Moved Permanently
Server: nginx/1.24.0 (Ubuntu)
Location: https://://yourdomain.com/
Next, confirm that Certbot's scheduled systemd timer will renew your certificate automatically before its 90-day expiration:
sudo certbot renew --dry-run
If you see Congratulations, all simulated renewals succeeded, your TLS automation is ready for production.
Troubleshooting Common Errors
Even with clean configs, two issues commonly trip up fresh deployments:
1. 502 Bad Gateway in the Browser
A 502 Bad Gateway means Nginx is reachable, but it cannot connect to your upstream container on 127.0.0.1:8080.
-
Check container status: Run
docker ps -ato see if the container exited with an error. Inspect logs withdocker logs web-app. -
Verify port mapping: Ensure the host port in
docker run -p 127.0.0.1:8080:80matches theproxy_pass http://127.0.0.1:8080;directive in your Nginx config.
2. Certbot Fails Domain Validation
Let's Encrypt must connect to your server over port 80 to verify domain ownership.
-
Check DNS propagation: Run
dig +short ://yourdomain.comand verify it returns your server's exact public IPv4 address. -
Check cloud security groups: In addition to UFW inside Ubuntu, confirm that your cloud provider's external firewall or Security Group (AWS, DigitalOcean, Vultr, etc.) allows inbound TCP traffic on ports
80and443.
Conclusion & Next Steps
You now have a containerized web application running behind an Nginx reverse proxy on Ubuntu 24.04, isolated from direct public port access and encrypted with automated Let's Encrypt SSL certificates.
From here, you can expand this setup by:
- Replacing the single
docker runcommand with adocker-compose.ymlfile to manage multi-container stacks (such as a backend API plus Redis or PostgreSQL).
Top comments (0)