Link Shortener Docker Setup: What Works, What Breaks

A person's hands are seen connecting network cables to server hardware in a dedicated setup area.

Written by

in

A link shortener Docker setup means running the shortener and its database as containers you control, usually two or three, behind a reverse proxy that terminates TLS. What matters is where your click data lives and who can switch your links off. Containers make the install repeatable. They do nothing about the database backup, the DNS record, or the redirect code you choose.

TL;DR:

  • The minimum viable stack is two containers: the shortener and its database. A reverse proxy makes three.
  • The database volume holds everything that matters. Containers are disposable and rebuildable.
  • Pin image tags. latest can turn a Tuesday restart into a Thursday outage.
  • Return 302 until a destination is final. A cached 301 cannot be recalled.
  • A nightly database dump copied off the VPS is the only backup that counts.

Table of Contents

Two hands are working near server components, one securing data drives and the other adjusting nearby equipment.

A link shortener Docker setup gives you an install you can rebuild from a text file, an upgrade that takes a minute, and a stack you can move to another server by copying one volume. Docker's Compose specification lets you pin an exact image tag, so the build that worked in March is the build running in July.

The containers are disposable. They hold no links, no users, and no click history. Everything your business depends on sits in a database volume beside them, plus a DNS record and a TLS certificate that both live outside Docker entirely.

Three things decide whether the project works past the first week: where the database lives, which HTTP status code the redirect returns, and whether a nightly dump leaves the machine. This guide covers all three, with a compose file you can run today and the failures that stay invisible until month three.

Two Containers Minimum, Three If You Want Click Data

Container Job What breaks without it
Shortener app Redirects, admin UI, API Nothing resolves
Database Links, clicks, users Every link dies on restart
Reverse proxy TLS, hostname routing No HTTPS, no custom domain

Shortener app. Serves the redirect and the admin interface. Pin the image tag. When you deploy latest, you accept whatever migration the maintainers pushed this morning.

Database. MySQL, MariaDB, or PostgreSQL in most projects. Give it 512 MB to 1 GB of RAM on a small VPS, and give it a named volume so the data outlives the container.

Reverse proxy. Caddy, Nginx, or Traefik. It holds the certificate, listens on 443, and forwards your short domain to the app over an internal Docker network.

What you can leave out. A cache container, unless the project's own documentation lists it as required. Every extra service costs RAM you may need on a $6-a-month VPS.

Single-container builds that embed SQLite are legitimate for a marketing team's link volume. The limiting factor is write concurrency.

SQLite serializes writes: one writer at a time. That is fine for hundreds of redirects a minute and painful for anything that batches thousands per second.

If you are still deciding between a container stack and a package install, the trade-offs are laid out in how to run a URL shortener on your own server.

A docker-compose.yml That Survives a Reboot

This is the shape of a working stack. Environment variable names differ between projects, so read your image's README before you copy anything.

services:
  shortener:
    image: your-shortener:1.4.2
    restart: unless-stopped
    environment:
      DB_HOST: db
      DB_NAME: links
      DB_USER: links
      DB_PASSWORD: ${DB_PASSWORD}
    depends_on:
      - db
  db:
    image: mariadb:11.4
    restart: unless-stopped
    environment:
      MARIADB_DATABASE: links
      MARIADB_USER: links
      MARIADB_PASSWORD: ${DB_PASSWORD}
      MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
volumes:
  db_data:

restart: unless-stopped matters more than it looks. Without it, a host reboot leaves both containers down until somebody SSHes in and runs docker compose up -d again.

Keep passwords in a .env file next to the compose file, and keep that file out of version control. OWASP's Top 10 lists security misconfiguration and credential handling among the most common root causes of breaches, and a committed .env is the easy version of both.

Do not publish the database port. Containers on the same compose network reach each other by service name. Publishing 3306 to the host adds exposure and buys nothing.

Pro Tip: bind any published port to the loopback address, as in 127.0.0.1:8080:8080, instead of 8080:8080. On a default Docker install, published ports get their own iptables rules that take precedence over your host firewall, so a port you believe ufw is blocking can still answer requests from the internet. Confirm it by scanning the port from a second machine.

A fuller walkthrough of the host side, including Nginx and certificate renewal, is in installing a link shortener on a Linux server.

Point an A record (and an AAAA record if you have IPv6) for go.example.com at the server's IP before you touch the proxy configuration. Certificate authorities validate domain control over the public internet, not from inside your Docker network.

The HTTP-01 challenge needs port 80 reachable from outside for the few seconds it takes to validate. If you close port 80 permanently, use the DNS-01 challenge instead, which also gets you a wildcard certificate covering *.go.example.com.

The rate limit catches people rebuilding containers in a loop:

50 certificates per registered domain per week. That is the Let's Encrypt cap, and a misconfigured container that restarts on every failed health check can burn through it in an afternoon.

Test the proxy with curl -I https://go.example.com/any-slug before you print anything. A redirect that works in a browser and fails in curl usually means the container is returning a 200 HTML page rather than a 3xx header.

301 or 302: The Switch You Cannot Take Back

A 301 tells the browser the move is permanent. RFC 9110 marks 301 as cacheable by default, which means browsers and any intermediary proxies can store it and keep sending visitors to the old destination even after you change the target in your admin panel.

Code Cached by default Use it when
301 Yes The destination is final and will never change
302 No You may repoint the link later
307 No Same as 302, with the request method preserved
308 Yes Permanent, with the method preserved

For marketing links, event links, and anything you might re-target, 302 is the safe default. For a slug that will point at the same page for the rest of its life, 301 is faster and slightly kinder to your server.

Check what your shortener returns by default, because the defaults vary between projects and some ship 301 out of the box. One curl -I tells you. If you have already handed out links through a hosted service and are planning to move, the migration order matters and is covered in what happens to links when a hosted plan ends.

Volumes, Backups, and the Command That Deletes Everything

docker compose down -v deletes your links. The -v flag removes named volumes. It is the single most destructive command in this whole setup, and it looks almost identical to the harmless one.

Dump the database from the host, on a schedule. For MariaDB or MySQL, a cron job like this writes a dated file every night:

docker compose exec -T db mariadb-dump -u root -p"$DB_ROOT_PASSWORD" links > /srv/backups/links-$(date +%F).sql

Copy those files off the machine. A dump sitting in /srv/backups on the same VPS is not a backup. It disappears with the disk, the account, or the snapshot mistake. Push it to object storage or pull it to a second host.

Restore one file before you need to. Spin up a throwaway database container, import the newest dump, and count the rows. An untested backup may not restore.

Volumes are managed by Docker. Docker's documentation on volumes notes that named volumes persist until you remove them explicitly, which is exactly why they are both durable and easy to delete by accident.

What It Costs, and When Self-Hosting Is the Wrong Call

The recurring bill is small. A 1 GB to 2 GB VPS runs $5 to $12 a month. A domain costs $10 to $15 a year. Let's Encrypt certificates are free. Your time is the expensive line: one to two hours for the first install, then 15 to 30 minutes a quarter for updates and restore tests.

Compare that against a hosted plan at $20 to $40 a month, roughly $720 to $1,440 over three years, with your click data in someone else's database and your links depending on a subscription you keep paying.

Docker makes the install repeatable. It does not make anything highly available. Two containers on one machine is still one machine, and a dead VPS takes every short link with it. If you need five nines and nobody is on call at 2am, a hosted service is the honest answer, and no compose file changes that. The costs and risks of the whole approach, including the ones people undercount, are broken out in running your own URL shortener, and the analytics side has its own math in self-hosted analytics costs and ownership.

Self-hosting is also the wrong call when you have fewer than about 20 links, no branded domain, and no interest in Linux. The setup is straightforward, and maintenance is the part that persists.

Get a Shortener You Install Once and Pay For Once

Kortli is a self-hosted link shortener you install on your own Linux server for a one-time license instead of a monthly subscription. It runs as a container against your own database, so redirects and click data stay on the machine you control. Custom and branded short domains carry no per-domain fee, links can be password-protected, and a single slug can run an A/B split between two destinations. QR codes are generated with your logo embedded and styled to match your brand, and the click analytics, including referrers and geography, live in your database.

One limit worth stating plainly: Kortli does not manage the three things this article said Docker does not cover either. You still own the DNS record, the TLS certificate, and the nightly database dump. If you have never run a compose stack, budget an afternoon for the first install, and check the current specification and license price on the site before you buy, since the RAM and database version requirements are listed there and they do change between releases.

Frequently Asked Questions

A shortener container and a MariaDB or PostgreSQL container together fit comfortably in 1 GB, and 2 GB gives you headroom for updates and a reverse proxy. SQLite-based single-container installs run in a few hundred megabytes. Scale up if you expect thousands of redirects per minute instead of per hour.

Do I have to open port 80 for the TLS certificate?

Only briefly, and only if you use the HTTP-01 challenge. Keep port 80 open long enough for the certificate authority to validate your domain, then it can stay open for redirects to HTTPS. If you cannot open port 80 at all, use the DNS-01 challenge, which also supports wildcard certificates.

Pull the new image tag and run docker compose up -d. The database lives in a named volume and is unaffected by replacing the application container. Take a database dump first anyway, and read the release notes for migration steps, since some versions change the schema on first boot.

They stop resolving, because the short domain points at a machine that is gone. Recovery means restoring the database dump onto a new server and repointing the DNS record, which is why the dump has to be stored somewhere other than the VPS itself. Plan on a few hours of downtime unless you keep a warm standby.

Use 302 for any link you might repoint, because browsers do not cache it by default. Use 301 only when the destination is permanent, since RFC 9110 makes it cacheable and visitors can keep reaching the old target after you change it. Check your project's default before you publish anything.

Can I run this on a Raspberry Pi or a home server?

Yes, provided the images you choose publish ARM64 builds, which most current ones do. The practical limits are your home upload bandwidth and residential IP reputation, since some corporate networks and email scanners treat home IP ranges with suspicion. A small VPS avoids both problems for a few dollars a month.

Sources

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *