Hosting My Own Short URLs With YOURLS and Docker

I’ve always wanted to self-host a short URL service. Mainly because sometimes you need to hand out links, and links expire. (Unless you manage to cheat entropy and the decay of the universe.) So if I have my own short URL service, I can hand out what is essentially a permalink, because I can update it until the heat death of the universe or when my server shuts down because I forgot to pay the bill on time, whichever is faster.

Anyway, I set it up with YOURLS and Docker (+ Docker Compose). I thought it was going to be difficult, but it’s literally this file:

docker-compose.yml:

version: "3"

services:
  mariadb:
    image: mariadb:11
    container_name: yourls-mariadb
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ROOT_PASSWORD_HERE
      MYSQL_PASSWORD: DATABASE_PASSWORD_HERE
      MYSQL_DATABASE: yourls
      MYSQL_USER: yourls
    volumes:
      - ./mariadb:/var/lib/mysql

  yourls:
    image: yourls:latest
    container_name: yourls-app
    restart: unless-stopped
    depends_on:
      - mariadb
    environment:
      YOURLS_DB_HOST: mariadb
      YOURLS_DB_USER: yourls
      YOURLS_DB_PASS: DATABASE_PASSWORD_HERE
      YOURLS_DB_NAME: yourls
      YOURLS_USER: ADMIN_USERNAME_HERE
      YOURLS_PASS: ADMIN_PASSWORD_HERE
      YOURLS_SITE: "https://yourls.example.com"
      YOURLS_UNIQUE_URLS: "true"
      YOURLS_PRIVATE: "true"
    volumes:
      - ./yourls:/var/www/html/user

  swag:
    image: linuxserver/swag:latest
    container_name: swag
    restart: unless-stopped
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Etc/UTC
      - URL=yourls.example.com
      - VALIDATION=http
      - EMAIL=letsencrypt-email@example.com
    volumes:
      - ./swag/config:/config
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - yourls

Once you have it up and running with docker-compose up -d, you do need to move the sample configuration file in the swag/ folder and adjust some settings, but that’s not too difficult.

Here’s a short link back to this blog post: https://links.ericswpark.com/blog-yourls (so meta!)

comments