Skip to content

Commit

Permalink
Dockerfile (#644)
Browse files Browse the repository at this point in the history
* dockerfile, docker-compose, updated docs
* small updates to settings.py and test settings to improve coverage in CI
* updated docs to reflect docker compose vs. the old start.sh method
* updated node version in .nvmrc
  • Loading branch information
chrisclark authored Jul 17, 2024
1 parent 67ea498 commit 3c35b37
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 103 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20.10.0
20.15.1
82 changes: 82 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Build stage
FROM python:3.12.4 as builder

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
build-essential \
&& rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements /app/requirements
RUN pip install --no-cache-dir -r requirements/dev.txt

# Install NVM and Node.js
ENV NVM_DIR /root/.nvm
# This should match the version referenced below in the Run stage, and in entrypoint.sh
ENV NODE_VERSION 20.15.1

COPY package.json package-lock.json /app/

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash \
&& . "$NVM_DIR/nvm.sh" \
&& nvm install ${NODE_VERSION} \
&& nvm use v${NODE_VERSION} \
&& nvm alias default v${NODE_VERSION} \
&& npm install


# Runtime stage
FROM python:3.12.4

WORKDIR /app

# Copy Python environment from builder
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# Copy Node.js environment from builder
COPY --from=builder /root/.nvm /root/.nvm
ENV NVM_DIR /root/.nvm

# The version in this path should match the version referenced above in the Run stage, and in entrypoint.sh
ENV PATH $NVM_DIR/versions/node/v20.15.1/bin:$PATH

COPY --from=builder /app/node_modules /app/node_modules

COPY . /app

# Run migrations and create initial data
RUN python manage.py migrate && \
python manage.py shell <<ORM
from django.contrib.auth.models import User
u = User.objects.filter(username='admin').first()
if not u:
u = User(username='admin')
u.set_password('admin')
u.is_superuser = True
u.is_staff = True
u.save()

from explorer.models import Query
queries = Query.objects.all().count()
if queries == 0:
q = Query(sql='select * from explorer_query;', title='Sample Query')
q.save()
ORM

# Copy and set permissions for the entrypoint script
COPY --chown=myuser:myuser entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Expose the ports the app runs on
EXPOSE 8000 5173

# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/ || exit 1

# Set the entrypoint
ENTRYPOINT ["/entrypoint.sh"]
16 changes: 8 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
SQL Explorer
============

* `Official Website <https://www.sqlexplorer.io/>`_
* `Live Demo <https://demo.sqlexplorer.io/>`_

* `Documentation <https://django-sql-explorer.readthedocs.io/en/latest/>`_
* Quick Start: Clone and then ``docker compose up``

* `Website <https://www.sqlexplorer.io/>`_
Video Tour:

.. |inline-image| image:: https://sql-explorer.s3.amazonaws.com/video-thumbnail.png
:target: https://sql-explorer.s3.amazonaws.com/Sql+Explorer+5.mp4
:height: 10em

* Demo video: |inline-image|
|inline-image|

SQL Explorer aims to make the flow of data between people fast,
simple, and confusion-free. It is a Django-based application that you
Expand Down Expand Up @@ -97,9 +98,8 @@ Screenshots
Development
------------

Included is a test_project that you can use to kick the tires. Just
create a new virtualenv, cd into ``test_project`` and run ``start.sh`` (or
walk through the steps yourself) to get a test instance of the app up
and running.
Included is a test_project that you can use to kick the tires. You can run this via ``docker compose up``

You can now navigate to 127.0.0.1:8000/explorer/, log in with admin/admin, and begin exploring!

You can now navigate to 127.0.0.1:8000/explorer/ and begin exploring!
This will also run a Vite dev server with hot reloading for front-end changes.
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
web:
build: .
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
ports:
- "8000:8000"
- "5173:5173"
volumes:
- .:/app
- node_modules:/app/node_modules
environment:
- DJANGO_SETTINGS_MODULE=test_project.settings

volumes:
node_modules:
47 changes: 15 additions & 32 deletions docs/development.rst
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
Running Locally (quick start)
-----------------------------

Included is a test_project that you can use to kick the tires. Just
create a new virtualenv, cd into ``test_project`` and run ``start.sh`` (or
walk through the steps yourself) to get a test instance of the app up
and running.
Whether you have cloned the repo, or installed via pip, included is a test_project that you can use to kick the tires.

You can now navigate to 127.0.0.1:8000/explorer/ and begin exploring!
Run:

``docker compose up``

You can now navigate to 127.0.0.1:8000/explorer/, log in with admin/admin, and begin exploring!

Installing From Source
----------------------

If you are installing SQL Explorer from source (by cloning the repository),
you may want to first look at simply running test_project/start.sh.

If you want to install SQL Explorer from source, into an existing project,
you can do so by cloning the repository and following the usual
:doc:`development` instructions, and then additionally building the front-end
dependencies:
If you want to install SQL Explorer from source (e.g. not from the built PyPi package),
into an existing project, you can do so by cloning the repository and following the usual
:doc:`install` instructions, and then additionally building the front-end dependencies:

::

Expand All @@ -33,30 +30,16 @@ phase. Copy the /explorer directory into site-packages and you're ready to go.
Tests
-----

Factory Boy is needed if you'd like to run the tests. They can be run with:
Install the dev requirements:

``pip install -r requirements/dev.txt``

And then:

``python manage.py test --settings=tests.settings``

and with coverage:
Or with coverage:

``coverage run --source='.' manage.py test --settings=tests.settings``
``coverage combine``
``coverage report``

Running Celery
--------------

To run tests with Celery enabled, you will need to install Redis and Celery.
::

brew install redis
pip install celery
pip install redis

Then run the redis server and the celery worker. A good way of doing it is:
::

screen -d -S 'redis' -m redis-server
screen -d -S 'celery' -m celery -A test_project worker

Finally, set ``EXPLORER_TASKS_ENABLED`` to True in tests.settings and run the tests.
6 changes: 1 addition & 5 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ Install with pip from pypi:
$ pip install django-sql-explorer
If you would also like to support downloading Excel files install with the dependency using:

.. code-block:: shell-session
$ pip install django-sql-explorer[xls]
Take a look at available ``extras``

Add to your ``INSTALLED_APPS``, located in the ``settings.py`` file in your project folder:

Expand Down
19 changes: 19 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
# entrypoint.sh

set -e

# Source the nvm script to set up the environment
# This should match the version referenced in Dockerfile
. /root/.nvm/nvm.sh
nvm use 20.15.1

# Django
python manage.py migrate
python manage.py runserver 0.0.0.0:8000 &
echo "Django server started"

# Vite dev server
export APP_VERSION=$(python -c 'from explorer import __version__; print(__version__)')
echo "Starting Vite with APP_VERSION=${APP_VERSION}"
npx vite --config vite.config.mjs
5 changes: 2 additions & 3 deletions explorer/tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from test_project.settings import * # noqa

EXPLORER_ENABLE_ANONYMOUS_STATS = False
EXPLORER_TASKS_ENABLED = True # set to true to test async tasks
EXPLORER_AI_API_KEY = None # set to any value to enable assistant
EXPLORER_CHARTS_ENABLED = False
EXPLORER_TASKS_ENABLED = True
EXPLORER_AI_API_KEY = "foo"
CELERY_BROKER_URL = "redis://localhost:6379/0"
CELERY_TASK_ALWAYS_EAGER = True
TEST_MODE = True
Expand Down
2 changes: 2 additions & 0 deletions explorer/tests/settings_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

EXPLORER_TASKS_ENABLED = False
EXPLORER_USER_UPLOADS_ENABLED = False
EXPLORER_CHARTS_ENABLED = False
EXPLORER_AI_API_KEY = None
54 changes: 0 additions & 54 deletions test_project/start.sh

This file was deleted.

3 changes: 3 additions & 0 deletions vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export default defineConfig({
root: resolve(__dirname, './'),
base: '',
server: {
host: true,
port: 5173,
strictPort: true,
open: false,
watch: {
usePolling: true,
Expand Down

0 comments on commit 3c35b37

Please sign in to comment.