The Docker Compose and Nginx setup I use for small production apps
Most small and medium businesses do not need Kubernetes. They need one well-run server that someone understands, can rebuild from scratch in an hour, and backs itself up. This is the layout I start almost every project with: a Node.js API, MongoDB, Redis and a web front end on a single Ubuntu machine.
The layout on disk
/srv/acme/
├── docker-compose.yml
├── .env # secrets, never committed
├── api/ # NestJS app with its Dockerfile
├── web/dist/ # built front end, served by Nginx
└── backups/
One folder per client or app. Everything needed to run it is in that folder, so moving to a new server is: copy the folder, restore the database, point DNS.
The compose file
# docker-compose.yml
services:
api:
build: ./api
restart: unless-stopped
env_file: .env
depends_on: [mongo, redis]
ports:
- '127.0.0.1:3000:3000' # only Nginx on this machine can reach it
healthcheck:
test: ['CMD', 'wget', '-qO-', 'http://localhost:3000/health']
interval: 30s
timeout: 5s
retries: 3
mongo:
image: mongo:8
restart: unless-stopped
env_file: .env # MONGO_INITDB_ROOT_USERNAME / _PASSWORD
volumes:
- mongo-data:/data/db
redis:
image: redis:7-alpine
restart: unless-stopped
command: ['redis-server', '--appendonly', 'yes']
volumes:
- redis-data:/data
volumes:
mongo-data:
redis-data:
A few choices worth explaining:
127.0.0.1:3000:3000, not3000:3000. Docker writes its own firewall rules, and a port published as plain3000:3000is reachable from the internet even if yourufwfirewall says it is closed. Binding to127.0.0.1means only Nginx on the same machine can reach the API. This catches people out more than anything else on this list.- Mongo and Redis publish no ports at all. The API reaches them by service name (
mongodb://mongo:27017) over Docker’s internal network. Nothing outside the server can talk to the database. - Named volumes keep the data when containers are rebuilt.
docker compose downkeeps them;docker compose down -vdeletes them, so never type-von a production server without a fresh backup. restart: unless-stoppedbrings everything back after a reboot or a crash.- The health check lets
docker psshow whether the API is actually answering, not just running.
A small, safe Dockerfile for the API
# api/Dockerfile
FROM node:24-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev
FROM node:24-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/main.js"]
The two-stage build keeps compilers and dev dependencies out of the final image, and USER node means the app does not run as root inside the container. Add a .dockerignore with node_modules, .env and .git so they never end up in the image.
Nginx in front
Nginx runs on the host (installed with apt), not in a container. It serves the front end, passes /api/ to the container, and handles HTTPS.
# /etc/nginx/sites-available/acme.conf
server {
server_name app.acme.in;
# Front end: static files, with a fallback for client-side routes.
root /srv/acme/web/dist;
location / {
try_files $uri $uri/ /index.html;
}
# Hashed build files never change, so let browsers keep them for a year.
location /assets/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# API: pass through to the container.
location /api/ {
proxy_pass http://127.0.0.1:3000/;
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;
client_max_body_size 20m;
}
gzip on;
gzip_types application/json text/css application/javascript image/svg+xml;
}
Enable it and check the syntax before reloading:
sudo ln -s /etc/nginx/sites-available/acme.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
Note the trailing slash in proxy_pass http://127.0.0.1:3000/;. With it, a request to /api/orders reaches the app as /orders. Without it, the app receives /api/orders. Pick one and make the app match.
Free HTTPS
Certbot gets a Let’s Encrypt certificate, edits the Nginx file to use it, and sets up automatic renewal:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d app.acme.in
sudo certbot renew --dry-run # confirm renewal will work
Deploying an update
cd /srv/acme
git pull
docker compose up -d --build api
docker compose ps
Only the API is rebuilt; the database keeps running. For the front end, build it (locally or in CI) and copy the new dist/ folder over. Nginx serves the new files immediately.
What I add on top
- Server hardening before anything else: SSH keys only, a firewall, automatic security updates. I wrote that up as a 15-minute checklist.
- Backups that are actually tested. See MongoDB backups you have actually restored.
- Uptime monitoring from outside the server, so I hear about a problem before the client does. Any monitor that checks
/api/healthevery minute and sends an alert works. - Log rotation for Docker, so logs never fill the disk. In
/etc/docker/daemon.json:{"log-driver": "json-file", "log-opts": {"max-size": "10m", "max-file": "3"}}, then restart Docker.
None of this is clever, which is the point. Six months later, anyone who reads these four files knows exactly how the system runs.