Skip to content

Commit

Permalink
Docker development environment update (#4325)
Browse files Browse the repository at this point in the history
Co-authored-by: Saurabh Kumar <[email protected]>
  • Loading branch information
frjo and theskumar authored Jan 10, 2025
1 parent 9960640 commit 2c26a3c
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 239 deletions.
39 changes: 39 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
FROM python:3.12.7-bookworm

# Add venv/bin to PATH.
ENV PATH="/opt/app/.venv/bin:/usr/local/bin:$PATH"

# Copy from the cache instead of linking since it's a mounted volume
ENV UV_LINK_MODE=copy

# Set work directory.
WORKDIR /opt/app

# Install node.
COPY --from=node:20.18-slim /usr/local/bin /usr/local/bin
COPY --from=node:20.18-slim /usr/local/lib/node_modules /usr/local/lib/node_modules

# Install uv.
COPY --from=ghcr.io/astral-sh/uv:0.5.13 /uv /uvx /usr/local/bin

# Install node dependencies.
COPY package*.json ./
RUN npm install --quiet

# Install python dependencies.
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync

# Copy the project into the image
COPY ./ ./

# Create directories.
RUN mkdir -p ./hypha/static_compiled && mkdir -p ./hypha/media

# Build front end.
RUN npm run dev:build

# Run entrypoint.sh.
ENTRYPOINT ["/opt/app/docker/entrypoint.sh"]
21 changes: 0 additions & 21 deletions docker/Dockerfile.dev

This file was deleted.

10 changes: 10 additions & 0 deletions docker/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.cache
.git
.github
.ruff_cache
.venv
.vscode
docs_build
hypha/static_compiled
media
node_modules
45 changes: 45 additions & 0 deletions docker/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: hypha-dev

services:
py:
container_name: hypha-django-dev
build:
context: ..
dockerfile: docker/Dockerfile
environment:
- "DATABASE_URL=postgres://hypha:hypha@db:5432/hypha"
- "DJANGO_SETTINGS_MODULE=hypha.settings.dev"
- "PYTHONDONTWRITEBYTECODE=1"
- "PYTHONUNBUFFERED=1"
- "VIRTUAL_ENV=/opt/app/.venv"
ports:
- 9001:9001
volumes:
- ../hypha:/opt/app/hypha
develop:
watch:
- action: sync
path: ..
target: /opt/app
ignore:
- .venv/
- hypha/
- node_modules/
- action: rebuild
path: ./pyproject.toml
- action: rebuild
path: ./package-lock.json
depends_on:
- db
db:
container_name: hypha-postgres-dev
image: postgres:14-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=hypha
- POSTGRES_PASSWORD=hypha
- POSTGRES_DB=hypha

volumes:
postgres_data:
38 changes: 0 additions & 38 deletions docker/docker-compose.yaml

This file was deleted.

16 changes: 0 additions & 16 deletions docker/entrypoint.dev.sh

This file was deleted.

15 changes: 15 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -e

# Run needed python commands.
python manage.py createcachetable
python manage.py migrate --noinput
python manage.py clear_cache --cache=default
python manage.py sync_roles
python manage.py wagtailsiteupdate hypha.test 9001

# Start dev server.
npm run watch &
python manage.py runserver_plus 0.0.0.0:9001

exec "$@"
24 changes: 0 additions & 24 deletions docker/nginx/hypha.conf

This file was deleted.

67 changes: 67 additions & 0 deletions docker/prod/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#
# Builder stage.
#
FROM python:3.12.7-bookworm AS builder

# Add venv/bin to PATH.
ENV PATH="/opt/app/.venv/bin:/usr/local/bin:$PATH"

# Enable bytecode compilation
ENV UV_COMPILE_BYTECODE=1

# Copy from the cache instead of linking since it's a mounted volume
ENV UV_LINK_MODE=copy

# Set work directory.
WORKDIR /opt/app

# Create directories.
RUN mkdir -p ./hypha/static_compiled && mkdir -p ./hypha/media

# Install node.
COPY --from=node:20.18-slim /usr/local/bin /usr/local/bin
COPY --from=node:20.18-slim /usr/local/lib/node_modules /usr/local/lib/node_modules

# Install uv.
COPY --from=ghcr.io/astral-sh/uv:0.5.13 /uv /uvx /usr/local/bin

# Install node dependencies.
COPY package*.json ./
RUN npm install --quiet

# Install python dependencies.
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-dev

# Copy the project into the image
COPY ./ ./

# Build front end.
RUN npm run build && python manage.py collectstatic --noinput --verbosity=0


#
# Production stage.
#
FROM python:3.12.7-slim-bookworm

# Add venv/bin to PATH.
ENV PATH="/opt/app/.venv/bin:/usr/local/bin:$PATH"

# Set work directory.
WORKDIR /opt/app

# Create directories.
RUN mkdir -p ./hypha/static_compiled && mkdir -p ./hypha/media

# Copy venv and assets from builder.
COPY --from=builder /opt/app/.venv /opt/app/.venv
COPY --from=builder /opt/app/static /opt/app/static

# Copy the project into the image
COPY ./ ./

# Run entrypoint.sh.
ENTRYPOINT ["/opt/app/docker/prod/entrypoint.sh"]
11 changes: 11 additions & 0 deletions docker/prod/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.cache
.git
.github
.ruff_cache
.venv
.vscode
docs
docs_build
hypha/static_compiled
media
node_modules
9 changes: 9 additions & 0 deletions docker/prod/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Docker for production

This is not a complete solution, but a starting point. A Dockerfile that build the Hypha app with all dependencies are provided.

Anyone using this will need to add a Postgres container and a Nginx (or equivalent) container.


Please play around with it and suggest improvements at <https://github.com/HyphaApp/hypha/issues>.

12 changes: 12 additions & 0 deletions docker/prod/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

# Run needed python commands.
python manage.py createcachetable
python manage.py migrate --noinput
python manage.py clear_cache --cache=default
python manage.py sync_roles

# Start gunicorn server.
gunicorn hypha.wsgi:application --env DJANGO_SETTINGS_MODULE=hypha.settings.production --threads 3 --reload --bind 0.0.0.0:9001

exec "$@"
Loading

0 comments on commit 2c26a3c

Please sign in to comment.