Skip to content

Commit e82f8a6

Browse files
authored
Add a Docker Compose setup for development (#128)
* Add a Docker Compose setup. * Make app and docs ports configurable. * Add .dockerignore. * Wait for postgres in app container. * Run app container in /app. * Add docs to contributing. * Add settings.LOGIN_URL. * Make GID, UID customizable and document it. * Move LOGIN_URL to settings.py. * Add redirect from / to /dashboard/. * Auto-run migrations on 'docker compose up'. Thanks, Atul Varma
1 parent b88350f commit e82f8a6

9 files changed

Lines changed: 198 additions & 1 deletion

File tree

.dockerignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.venv
2+
.vscode
3+
__pycache__/
4+
*.py[cod]
5+
*$py.class
6+
venv
7+
.eggs
8+
.pytest_cache
9+
*.egg-info
10+
.DS_Store

Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
FROM python:3.9
2+
3+
WORKDIR /app
4+
5+
# Set up the minimum structure needed to install
6+
# django_sql_dashboard's dependencies and the package itself
7+
# in development mode.
8+
COPY setup.py README.md .
9+
RUN mkdir django_sql_dashboard && pip install -e '.[test]'
10+
11+
# We need to have postgres installed in this container
12+
# because the automated test suite actually spins up
13+
# (and shuts down) a database inside the container.
14+
RUN apt-get update && apt-get install -y \
15+
postgresql postgresql-contrib \
16+
&& rm -rf /var/lib/apt/lists/*
17+
18+
# Install dependencies needed for editing documentation.
19+
COPY docs/requirements.txt .
20+
RUN pip install -r requirements.txt
21+
22+
ARG GID=1000
23+
ARG UID=1000
24+
25+
# Set up a non-root user. Aside from being best practice,
26+
# we also need to do this because the test suite refuses to
27+
# run as the root user.
28+
RUN groupadd -g ${GID} appuser && useradd -r -u ${UID} -g appuser appuser
29+
30+
USER appuser

docker-compose.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: "2"
2+
services:
3+
app:
4+
build: .
5+
links:
6+
- db
7+
volumes:
8+
- .:/app
9+
environment:
10+
- DATABASE_URL=postgres://appuser:test123@db/test_project
11+
- DJANGO_SETTINGS_MODULE=config.settings_interactive
12+
- PYTHONUNBUFFERED=yup
13+
working_dir: /app
14+
entrypoint: ["./test_project/wait-for-postgres.sh"]
15+
ports:
16+
- "${APP_PORT:-8000}:${APP_PORT:-8000}"
17+
command: bash -c "python test_project/manage.py migrate && python test_project/manage.py runserver 0.0.0.0:${APP_PORT:-8000}"
18+
docs:
19+
build: .
20+
volumes:
21+
- .:/app
22+
working_dir: /app/docs
23+
ports:
24+
- "${DOCS_PORT:-8001}:${DOCS_PORT:-8001}"
25+
command: make SPHINXOPTS="--host 0.0.0.0 --port ${DOCS_PORT:-8001}" livehtml
26+
db:
27+
# Note that this database is only used when we use
28+
# test_project interactively; automated tests spin up
29+
# their own database inside the app container.
30+
image: postgres:13-alpine
31+
environment:
32+
- POSTGRES_PASSWORD=test123
33+
- POSTGRES_USER=appuser
34+
- POSTGRES_DB=test_project

docs/contributing.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,98 @@ To build the documentation locally, run the following:
4242
make livehtml
4343

4444
This will start a live preview server, using [sphinx-autobuild](https://pypi.org/project/sphinx-autobuild/).
45+
46+
## Using Docker Compose
47+
48+
If you're familiar with Docker--or even if you're not--you may want to consider using our optional Docker Compose setup.
49+
50+
An advantage of this approach is that it relieves you of setting up any dependencies, such as ensuring that you have the proper version of Python and Postgres and so forth. On the downside, however, it does require you to familiarize yourself with Docker, which, while relatively easy to use, still has its own learning curve.
51+
52+
To try out the Docker Compose setup, you will first want to [get Docker][] and [install Docker Compose][].
53+
54+
Then, after checking out the code, run the following:
55+
56+
```
57+
cd django-sql-dashboard
58+
docker-compose build
59+
```
60+
61+
At this point, you can start editing code. To run any development tools such as `pytest` or `black`, just prefix everything with `docker-compose run app`. For instance, to run the test suite, run:
62+
63+
```
64+
docker-compose run app python pytest
65+
```
66+
67+
If this is a hassle, you can instead run a bash shell inside your container:
68+
69+
```
70+
docker-compose run app bash
71+
```
72+
73+
At this point, you'll be in a bash shell inside your container, and can run development tools directly.
74+
75+
[get Docker]: https://docs.docker.com/get-docker/
76+
[install Docker Compose]: https://docs.docker.com/compose/install/
77+
78+
### Using the dashboard interactively
79+
80+
The Docker Compose setup is configured to run a simple test project that you can use to tinker with the dashboard interactively.
81+
82+
To use it, run:
83+
84+
```
85+
docker-compose up
86+
```
87+
88+
Then, in a separate terminal, run:
89+
90+
```
91+
docker-compose run app python test_project/manage.py createsuperuser
92+
```
93+
94+
You will now be prompted to enter details about a new superuser. Once you've done that, you can visit the example app's dashboard at http://localhost:8000/. After entering the credentials for the superuser you just created, you will be able to tinker with the dashboard.
95+
96+
### Editing the documentation
97+
98+
Running `docker-compose up` also starts the documentation system's live preview server. You can visit it at http://localhost:8001/.
99+
100+
### Changing the default ports
101+
102+
If you are already using ports 8000 and/or 8001 for other things, you can change them. To do this, create a file in the repository root called `.env` and populate it with the following:
103+
104+
```
105+
APP_PORT=9000
106+
DOCS_PORT=9001
107+
```
108+
109+
You can change the above port values to whatever makes sense for your setup.
110+
111+
Once you next run `docker-compose up` again, the services will be running on the ports you specified in `.env`.
112+
113+
### Changing the default UID and GID
114+
115+
The default settings assume that the user id (UID) and group id (GID) of the account you're using to develop are both 1000. This is likely to be the case, since that's the UID/GID of the first non-root account on most systems. However, if your account doesn't match this, you can customize the container to use a different UID/GID.
116+
117+
For instance, if your UID and GID are 1001, you can build your container with the following arguments:
118+
119+
```
120+
docker-compose build --build-arg UID=1001 --build-arg GID=1001
121+
```
122+
123+
### Updating
124+
125+
The project's Python dependencies are all baked into the container image, which means that whenever they change (or to be safe, whenever you `git pull` new changes to the codebase), you will want to run:
126+
127+
```
128+
docker-compose build
129+
```
130+
131+
You will also want to restart `docker-compose up`.
132+
133+
### Cleaning up
134+
135+
If you somehow get your Docker Compose setup into a broken state, or you decide that you never use Docker Compose again, you can clean everything up by running:
136+
137+
```
138+
docker-compose down -v
139+
```

test_project/config/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,5 @@
9797
# https://docs.djangoproject.com/en/3.1/howto/static-files/
9898

9999
STATIC_URL = "/static/"
100+
101+
LOGIN_URL = "/admin/login/"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Normally test_project is used as scaffolding for
2+
# django_sql_dashboard's automated tests. However, it can
3+
# be useful during development to have a sample project to
4+
# tinker with interactively. These Django settings can be
5+
# useful when we want to do that.
6+
7+
from .settings import *
8+
9+
# Just have our dashboard use the exact same credentials for
10+
# our database, there's no need to bother with read-only
11+
# permissions when using test_project interactively.
12+
DATABASES["dashboard"] = DATABASES["default"]

test_project/config/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from django.contrib import admin
22
from django.urls import include, path
3+
from django.views.generic.base import RedirectView
34

45
import django_sql_dashboard
56

7+
68
urlpatterns = [
79
path("dashboard/", include(django_sql_dashboard.urls)),
810
path("admin/", admin.site.urls),
11+
path("", RedirectView.as_view(url="/dashboard/")),
912
]

test_project/test_dashboard_permissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
def test_anonymous_user_redirected_to_login(client):
1111
response = client.get("/dashboard/?sql=select+1")
1212
assert response.status_code == 302
13-
assert response.url == "/accounts/login/?next=/dashboard/%3Fsql%3Dselect%2B1"
13+
assert response.url == "/admin/login/?next=/dashboard/%3Fsql%3Dselect%2B1"
1414

1515

1616
def test_superusers_allowed(admin_client, dashboard_db):

test_project/wait-for-postgres.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
# wait-for-postgres.sh
3+
4+
set -e
5+
6+
until psql $DATABASE_URL -c '\q'; do
7+
>&2 echo "Postgres is unavailable - sleeping"
8+
sleep 1
9+
done
10+
11+
exec "$@"

0 commit comments

Comments
 (0)