Shlink configuration with database

Shlink is really easy to set up quickly. The big drawback of their default docker setup instructions is: everything is gone when the container is recreated i.e. updated. Of course, I like my containers up-to-date, so I use watchtower to automate this. Surprise surprise, all my links are now gone.

My fault.

After feeling like an idiot for a while, I looked around and found a nice pre-made docker-compose file that uses shlink and mariadb, with a bind-mount for data. I found the original tutorial here:

https://community.hetzner.com/tutorials/install-shlink-docker . Thanks to the Hetzner community.

In short, the compose file looks like this:

version: "3"

services:
  shlink:
    image: shlinkio/shlink:stable
    restart: unless-stopped
    container_name: shlink_container
    environment:
      - TZ="Europe/Berlin"
      - DEFAULT_DOMAIN=your.domain-he.re
      - IS_HTTPS_ENABLED=true
      - GEOLITE_LICENSE_KEY=<GEOLITE_LICENSE>
      - DB_DRIVER=maria
      - DB_USER=shlink
      - DB_NAME=shlink
      - DB_PASSWORD=a_random_string
      - DB_HOST=database
    depends_on:
      - database
    ports:
      - 7795:8080 # can use any port here, i just use 7795
    volumes:
      - /a/bind/mount/Shlink/import:/imports
        #I use this bind mount to import short links via csv files

  database:
    image: mariadb:10.8
    restart: unless-stopped
    container_name: shlink_db
    environment:
      - MARIADB_ROOT_PASSWORD=lkjllkjasdfsdfkjfs #hit your keyboard a bunch here
      - MARIADB_DATABASE=shlink
      - MARIADB_USER=shlink
      - MARIADB_PASSWORD=a_random_string
    volumes:
      - /a/bind/mount/Shlink/db:/var/lib/mysql
Code language: YAML (yaml)

This compose file lets you easily run a short-link solution for yourself (and others?).

To add an API key for the web admin panel, run this command in the container (either via docker exec or portainer -> console -> sh):

# shlink api-key:generateCode language: Bash (bash)

To import a csv file with links, just put the csv file in the folder you specified above and run the command in your container:

# shlink short-url:import csv
Code language: Bash (bash)

Input the path you specified (in relation to the container) and enter the delimiter used, job done!
For more infos on importing data to shlink, see here: https://shlink.io/documentation/advanced/import-short-urls/

This might be what I was actually looking for...

Leave a Reply

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