Skip to content

How to host your own libSQL instance on Docker guide #51

@aliozinan

Description

@aliozinan

As discussed on the Discord server #support channel here :

https://discord.com/channels/1309279407743172608/1345758358141141113

The steps below explain how to use a self-hosted libSQL instance running on a local working environment using Docker for StudioCMS projects. This setup can be used for Astro.js projects that utilize AstroDB and/or libSQL as well.

Note that this guide targets the development environment, and using this setup in production is not recommended since doing so has its own risks.

In addition, this issue is created to support the StudioCMS documentation.

Note : You'll need Docker and openssl installed on your system. pnpm is used in this guide.

  • In your project root folder, create a new folder (here we're naming it as keys) :
mkdir keys
  • Run the following command to create a new PKCS#8-encoded Ed25519 key-pair :
openssl pkey -in ./keys/libsql.pem -pubout -out ./keys/libsql.pub
  • Install jwtgen :
pnpm add -g jwtgen

jwtgen is a node CLI utility used to generate JWT tokens. run pnpm jwtgen -h for more options.

  • Run the following command to get the contents of your private key file in single line format :
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' ./keys/libsql.pem
  • Copy the output and use it in the jwtgen command below
pnpm jwtgen -a HS256 -s "paste your private key output here" -c "iss=admin" -e 31556926

The output is the JWT auth token encrypted with your private key, which will be used for libSQL authentication. Keep in mind that the token will be valid for 1 year!

  • Run the following command to encode the JWT auth token in base64URL format :
echo -n "paste the JWT auth token output here" | base64 | tr '/+' '_-' | tr -d '='
  • Now update your Studio CMS .env file :
ASTRO_DB_REMOTE_URL=http://localhost:8080
ASTRO_DB_APP_TOKEN=paste your base64URL encoded JWT auth token here
  • Create a new docker-compose.yml file for the libSQL instance. At this point you have two options to configure libSQL authentication :

    • Using the public key file itself
    • Using the encoded JWT auth token as an environment variable (the same ASTRO_DB_APP_TOKEN value you used in your .env file)
services:
  libsql:
    image: ghcr.io/tursodatabase/libsql-server:latest
    platform: linux/amd64
    ports:
      - "8080:8080"
      - "5001:5001"
    environment:
      - SQLD_NODE=primary
      - SQLD_AUTH_JWT_KEY_FILE=/home/.ssh/libsql.pub
    volumes:
      - ./data/libsql:/var/lib/sqld
      - ./keys/libsql.pub:/home/.ssh/libsql.pub
services:
  libsql:
    image: ghcr.io/tursodatabase/libsql-server:latest
    platform: linux/amd64
    ports:
      - "8080:8080"
      - "5001:5001"
    environment:
      - SQLD_NODE=primary
      - SQLD_AUTH_JWT_KEY=paste your base64URL encoded JWT auth token here
    volumes:
      - ./data/libsql:/var/lib/sqld

If you'd like to use a different libSQL instance per project, you'll need to use different ports for each libSQL instance. You can find more details on how to configure it here :

https://github.com/tursodatabase/libsql/blob/main/docs/DOCKER.md

  • Start the libSQL instance :
docker compose up -d
  • Prepare your project's database :
pnpm astro db push --remote
  • Run your StudioCMS application :
pnpm dev

Metadata

Metadata

Labels

documentationImprovements or additions to documentationenhancementNew feature or request
No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions