Skip to content

Commit

Permalink
chore: improved docker setup (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
shafy authored Mar 15, 2022
1 parent 240b5be commit 0b5c44c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 17 deletions.
8 changes: 5 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ ALLOW_NEW_ACCOUNTS=true # setting this to false disallows new sign ups
DATABASE_NAME=
DATABASE_USER=
DATABASE_PASSWORD=
SECRET_KEY_BASE= # Generate key with `rails secret`
SECRET_KEY_BASE= # Generate key with `rails secret` or manually (alphanumeric string with 128 chars)


# OPTIONAL if you're self-hosting (can be left out)
# OPTIONAL if you're self-hosting
INIT_USER_EMAIL= # set this if you run with docker-compose and want to create an initial user
INIT_USER_PASSWORD= # set this if you run with docker-compose and want to create an initial user
DATABASE_URL= # required if you're running Fugu without docker-compose in production
SENTRY_DSN=
SENTRY_DSN= # if you want to use Sentry to track errors
RAILS_LOG_TO_STDOUT=true # if you want to log errors in server logs
DATABASE_NAME_TEST= # only needed if you are running automated tests during development
5 changes: 3 additions & 2 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ COPY . .
RUN SECRET_KEY_BASE=`bin/rake secret` rails assets:precompile --trace && \
rm -rf tmp/cache vendor/assets test

#RUN rm -rf tmp/cache vendor/assets test

###############################################################################
# Stage 2: Run
FROM ruby:3.0.0
Expand All @@ -67,5 +65,8 @@ COPY --from=builder /usr/local/bundle /usr/local/bundle
# Copy app files
COPY --from=builder $RAILS_ROOT $RAILS_ROOT

# Default entrypoint (overriden if used with this project's docker-compose)
ENTRYPOINT ["sh", "/app/docker-entrypoint.sh"]

# Default command (overriden if used with this project's docker-compose)
CMD ["rails", "server", "-b", "0.0.0.0"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Fugu is a simple, privacy-friendly, open source and self-hostable product analytics.

<img src="https://fugu.lol/images/fugu_screenshot_main.jpg" width="650" alt="Fugu Screenshot">


## Get Fugu
There are two ways to use Fugu: Self-hosting or Fugu Cloud (managed version by us as a SaaS).
Expand Down
33 changes: 22 additions & 11 deletions SELFHOSTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Managed services like Heroku, Digital Ocean and Render offer fast a quick and si

Don't forget to create the database after your first deploy, and migrate the database if you do any updates in the future:
```shell
# run this once in the beginning
# run after the initial deploy
rails db:create

# run this before every deploy
# run this after every deploy
rails db:migrate
```

Expand All @@ -35,13 +35,22 @@ If you prefer to build the image locally, you can do it like this before running

`docker build -f Dockerfile.prod -t shafyy/fugu .`

If you're setting up Fugu for the first time, make sure to create the database before running the server:
The default entrypoint file `docker-entrypoint.sh` in `Dockerfile.prod` automatically creates the database and runs schema migrations. You are free to override the entrypoint when you're running your container.

`docker run --env-file .env shafyy/fugu:latest rails db:create`
To create the database and run migrations manually, you can run the container like so:
```shell
# creates database
docker run --env-file .env shafyy/fugu:latest rails db:create

# runs any pending schema migrations
docker run --env-file .env shafyy/fugu:latest rails db:migrate
```

Note that you can run the database creation and migration commands however you like (e.g., with `docker exec` if you have a running container, or if you're in the shell of a running container directly with `rails db:migrate`).

#### Docker image and database
#### Docker image and database (docker-compose)

If you don't have an existing PostgreSQL databse running on your server, we provide a `docker-compose` configuration that might come in handy.
We provide a docker-compose configuration that sets up everything you need to run Fugu, including PostgreSQL.
There are 3 different `docker-compose` configurations:

- `docker-compose.prod-remote.yml` uses the remote `shafyy/fugu:latest` image from the Docker registry
Expand All @@ -52,17 +61,19 @@ In most cases, you would just want to go with the remote image:

`docker-compose -f docker-compose.prod-remote.yml up`

If you're setting up Fugu for the first time, make sure to create the database before running the server:
The `docker-compose` configuration automatically creates and runs migrations for you (see `docker-entrypoint.sh` for more info).

`docker-compose -f docker-compose.prod-remote.yml run web rails db:create`

`docker-compose` uses the environment variables defined in your local `.env` file.
Note: `docker-compose` uses the environment variables defined in your local `.env` file.

If you want to use a different `docker-compose` configuration, simply pass its file name to the `-f` option in the commands above.

## Tips

### ALLOW_REGISTRATION

### Creating and initial user
After you've deployed Fugu for the first time, you can simply navigate to the root URL and create a user account. If for some reason you want to create an initial user account automatically, define the environment variables `INIT_USER_EMAIL` and `INIT_USER_PASSWORD` and (re)deploy. You can then just log in with this user.

### Disabling new user registration
After you've created your own account on your self-hosted instance, we recommend to set the environment variable `ALLOW_REGISTRATION` to `false`. Otherwise, random people who know your URL can create Fugu accounts on your instance.

## Questions and help
Expand Down
5 changes: 4 additions & 1 deletion docker-compose.prod-remote.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
- POSTGRES_USER=${DATABASE_USER}
- POSTGRES_DB=${DATABASE_NAME}
web:
image: shafyy/fugu:0.0.1
image: shafyy/fugu:latest
entrypoint: ["sh", "/app/docker-entrypoint.sh"]
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
tty: true
Expand All @@ -26,8 +26,11 @@ services:
- RAILS_ENV=production
- RACK_ENV=production
- RAILS_SERVE_STATIC_FILES=true
- DATABASE_NAME=${DATABASE_NAME}
- DATABASE_URL=postgres://${DATABASE_USER}:${DATABASE_PASSWORD}@db:5432/${DATABASE_NAME}
- SECRET_KEY_BASE=${SECRET_KEY_BASE}
- RAILS_LOG_TO_STDOUT=${RAILS_LOG_TO_STDOUT}
- SENTRY_DSN=${SENTRY_DSN}
- ALLOW_REGISTRATION=${ALLOW_REGISTRATION}
- INIT_USER_EMAIL=${INIT_USER_EMAIL}
- INIT_USER_PASSWORD=${INIT_USER_PASSWORD}
3 changes: 3 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ services:
- RAILS_ENV=production
- RACK_ENV=production
- RAILS_SERVE_STATIC_FILES=true
- DATABASE_NAME=${DATABASE_NAME}
- DATABASE_URL=postgres://${DATABASE_USER}:${DATABASE_PASSWORD}@db:5432/${DATABASE_NAME}
- SECRET_KEY_BASE=${SECRET_KEY_BASE}
- RAILS_LOG_TO_STDOUT=${RAILS_LOG_TO_STDOUT}
- SENTRY_DSN=${SENTRY_DSN}
- ALLOW_NEW_ACCOUNTS=${ALLOW_NEW_ACCOUNTS}
- INIT_USER_EMAIL=${INIT_USER_EMAIL}
- INIT_USER_PASSWORD=${INIT_USER_PASSWORD}
11 changes: 11 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#!/bin/bash
set -e

# create database if it doesn't exist
rails db:create

# run migrations if necessary
rails db:migrate

# create initial user if env vars are set and user doesn't exist
if [ -n "${INIT_USER_EMAIL}" ] && [ -n "${INIT_USER_PASSWORD}" ] && [ "$(rails runner "puts User.exists?(email: '${INIT_USER_EMAIL}')")" = 'false' ]
then
echo "Creating initial user..."
rails runner "User.create!({email: '${INIT_USER_EMAIL}', password: '${INIT_USER_PASSWORD}', password_confirmation: '${INIT_USER_PASSWORD}', status: 'active' })"
fi

exec "$@"

0 comments on commit 0b5c44c

Please sign in to comment.