simonw / django-sql-dashboard

Django app for building dashboards using raw SQL queries
https://django-sql-dashboard.datasette.io/
Apache License 2.0
437 stars 37 forks source link

Add a Docker Compose setup for development. #128

Closed toolness closed 3 years ago

toolness commented 3 years ago

This attempts to add support for contributing to the project using Docker Compose, as mentioned in https://github.com/simonw/django-sql-dashboard/issues/120#issuecomment-859152991. My hope is that this will make it easier for contributors (or at least, ones familiar with Docker Compose) to get up and running.

Specifically, it does the following:

Instructions

Note: These instructions are now out of date; see this PR's contributing.md for up-to-date instructions.

Setting up the Docker Compose environment can be accomplished with:

docker-compose build

# Wait a few seconds after running this, to give Postgres time
# to create the initial account and database.
docker-compose up -d db

docker-compose run app python manage.py migrate
docker-compose run app python manage.py createsuperuser

Once you've done that, you can run:

docker-compose up

This will start up both the test project and the Sphinx documentation server. If you only want to start one of them, you can use docker-compose up app to start up only the test project, or docker-compose up docs to start up only the documentation server.

You will probably want to visit http://localhost:8000/admin/ to log in as your newly-created superuser, and then visit http://localhost:8000/dashboard/ to tinker with the dashboard UI.

If you want to run the test suite, you can run:

docker-compose run app pytest

To do

toolness commented 3 years ago

Ok here's a rough attempt @simonw, let me know if this is something that you think would be useful to add to the repo. No worries if not--as I mentioned in the PR description, since this only adds files to the repo and doesn't change anything, I (and anyone else who prefers docker-based development) can always just use this stuff separately.

simonw commented 3 years ago

I like this a lot. I'm not at all opposed to optional Docker stuff - it's especially good for helping people get a working dev environment with as little friction as possible.

Do you know if it's possible to use Docker Compose in this way but allow users to easily override which ports are forwarded to on the host system? My port 8000 and 8001 are frequently running something else, would be neat if there was a pattern that meant I didn't have to free up those ports before running this.

This has made me think that maybe a cool ability would be if you could run a pre-compiled Django SQL Dashboard in its own Docker container and point it at an existing PostgreSQL database via an environment variable...

simonw commented 3 years ago

I particularly like that this runs the Sphinx auto build container too - helps enforce a culture that documentation isn't an afterthought.

simonw commented 3 years ago

Wait a few seconds after running this, to give Postgres time to create the initial account and database.

I think we can avoid that step entirely by adding the following to the Docker Compose file:

services:
  app:
    # ...
    depends_on:
    - db

Might need an extra trick to wait for the DB to fully start though, see https://docs.docker.com/compose/startup-order/

simonw commented 3 years ago

docker-compose run app python manage.py migrate

It would be nice if we could automate this too - maybe have the development container run it on startup? Definitely not a blocker though, just a nice-to-have.

simonw commented 3 years ago

Do you know if it's possible to use Docker Compose in this way but allow users to easily override which ports are forwarded to on the host system?

It looks like there's a pattern for making ports configurable with environment variables - see replies to this tweet: https://twitter.com/simonw/status/1403383417899675651

simonw commented 3 years ago

https://docs.docker.com/compose/compose-file/compose-file-v3/#variable-substitution looks like the trick. Looks like we can substitute variables from the environment but provide defaults like this:

services:
  app:
    web:
      ports: "${WEB_PORT:-8000}:8000"
toolness commented 3 years ago

Oh nice! Cool, I will make the changes you've suggested and will let you know when it's ready for review!

toolness commented 3 years ago

Ok @simonw, I think this is good enough for a first attempt, let me know what you think!

toolness commented 3 years ago

docker-compose run app python manage.py migrate

It would be nice if we could automate this too - maybe have the development container run it on startup? Definitely not a blocker though, just a nice-to-have.

Just automated this in 3570550! This was easy since migrate is idempotent... the developer will still need to manually run createsuperuser though.

toolness commented 3 years ago

Ok I think this is good to go @simonw!