If a user encounters an error but you don’t know about, did it happen at all?
Sentry is one of the several options for error tracking across many platforms and languages. Sentry has a great free option for 10,000 errors per month and a single user, but they also offer a self-hosted option that is 100% free. This post aims to help you configure your own installation of Sentry using docker-compose.
I will start by assuming that you have Docker and docker-compose installed and working. It would also be great if you had some experience with both of these.
For your convenience I am providing a zip with the 3 files I describe below. It cannot just be executed ‘as-is’ you must make the configuration changes described in this post. sentry_docker_7_2019.zip
Save the following file as
docker-compose.yml. It tells docker what containers to create and what environment variables to use when running them. Fill in the missing portions. The containers are configured to save their data to
/srv/sentry so you may need to create this path or choose a different path.
version: '3' services: # OPTIONAL: If you want to get emails from sentry include this docker container smtp: image: 'tianon/exim4:latest' environment: GMAIL_USER: 'GMAIL_USER@gmail.com' GMAIL_PASSWORD: 'GMAIL_PASSWORD_OR_GMAIL_APP_PASSWORD' sentry-base: image: 'sentry:latest' container_name: sentry-base restart: unless-stopped depends_on: - sentry-redis - sentry-postgres links: - sentry-redis - sentry-postgres ports: - 880:9000 env_file: - sentry.env volumes: - /srv/sentry/sentry:/var/lib/sentry/files # - /srv/sentry/etc:/etc/sentry # Uncomment the line above if using the GitHub plugin sentry-cron: image: 'sentry:latest' restart: unless-stopped depends_on: - sentry-base links: - sentry-redis - sentry-postgres command: "sentry run cron" env_file: - sentry.env volumes: - /srv/sentry/sentry:/var/lib/sentry/files # - /srv/sentry/etc:/etc/sentry # Uncomment the line above if using the GitHub plugin sentry-worker: image: 'sentry:latest' depends_on: - sentry-base links: - sentry-redis - sentry-postgres command: "sentry run worker" env_file: - sentry.env volumes: - /srv/sentry/sentry:/var/lib/sentry/files # - /srv/sentry/etc:/etc/sentry # Uncomment the line above if using the GitHub plugin sentry-redis: image: 'redis:latest' sentry-postgres: image: 'postgres:latest' environment: POSTGRES_USER: sentry POSTGRES_PASSWORD: sentry POSTGRES_DB: sentry volumes: - /srv/sentry/postgres:/var/lib/postgresql/data
Next, save the environment variables file as
sentry.env and edit it as appropriate.
# OPTIONAL: Include if you're using email SENTRY_EMAIL_HOST=smtp SENTRY_POSTGRES_HOST=sentry-postgres SENTRY_DB_USER=sentry SENTRY_DB_PASSWORD=sentry SENTRY_REDIS_HOST=sentry-redis # OPTIONAL: Include if Sentry is available over HTTPS SOCIAL_AUTH_REDIRECT_IS_HTTPS=true SENTRY_SECRET_KEY=
If you want to use GitHub integration follow the steps below, otherwise skip ahead.
- Make a new GitHub application at this link and enter in an application name.
- Set “Homepage URL” to the web root of your Sentry install.
- “User authorization callback URL” and “Setup URL (optional)” should both be set to “https://<URL>/extensions/github/setup/”.
- “Webhook URL” should be set to “https://<URL>/extensions/github/webhook/”
- Come up with a webhook secret and save it elsewhere, you’ll need it in the next step
- Finish creating the app and then create a new private key for it
sentry.conf.pyfrom GitHub and save it into
- Fill in the following keys with values from the GitHub app
- github-app.id: <GitHub App Id>
- github-app.name: <GitHub App Name>
- github-app.webhook-secret: <Webhook secret>
- github-app.client-id: <GitHub App Client ID>
- github-app.client-secret: <GitHub App Client Secret>
- github-app.private-key: “<Private key with no spaces and with new lines replaced by ‘\n’>”
- If your Sentry will be accessible over HTTPS, include the optional environment variable “SOCIAL_AUTH_REDIRECT_IS_HTTPS” in
Bringing up the service for the first time
Utilize the script below or manually type the commands to generate a new secret key, build the database, and start the service.
#!/bin/bash # Generate a random secret key and put it into the environment variable file sed -i "$ s/$/$(docker-compose run --rm sentry-base sentry config generate-secret-key)/" sentry.env # Run database migrations (build the database) docker-compose run --rm sentry-base sentry upgrade --noinput # Startup the whole service docker-compose up -d
Save this file as
sentry-install.sh and then make it executable by running
chmod +x sentry-install.sh. Next, run the file by doing
./sentry-install.sh. This file will write a new secret key to the
sentry.env file, so it is important that it is named in that way and that there are no additional lines after the “SENTRY_SECRET_KEY=”.
To do this all manually, run
docker-compose exec sentry-base sentry config generate-secret-key and put the output into the
sentry.env file as “SENTRY_SECRET_KEY”.
docker-compose exec sentry-base sentry upgrade which will initialize the postgres database. This command will need to be run every time the Sentry docker image is updated.
docker-compose up -d to bring up the service.
If you ran the commands above manually, then you’re done. If you used the script however, the service is up but there is no administrator.
Create the Administrator Account
Use the following command to create the administrator.
docker-compose exec sentry-base sentry createuser --email YOUR_EMAIL --password YOUR_NEW_PASSWORD --superuser --no-input
Thank you Michael.
Thanks for the tutorial. But I have an error with redis when I try the command “docker-compose run –rm sentry -base sentry upgrade –noinput” : “sentry.exceptions.InvalidConfiguration: Error -2 connecting to sentry-redis:6379. Name or service not known.” Any idea? Thanks again.
On OSX, recommend replacing:
sed -i "$ s/$/$(docker-compose run --rm sentry-base sentry config generate-secret-key)/" sentry.env
echo -e "\nSENTRY_SECRET_KEY=$(docker-compose run --rm sentry-base sentry config generate-secret-key)" >> sentry.env