Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker development environment update #4325

Merged
merged 11 commits into from
Jan 10, 2025
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
43 changes: 43 additions & 0 deletions docker/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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
develop:
watch:
- action: sync
path: ..
target: /opt/app
ignore:
- .venv/
- node_modules/
- hypha/static_compiled/
- action: rebuild
path: ./pyproject.toml
frjo marked this conversation as resolved.
Show resolved Hide resolved
- 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.

78 changes: 39 additions & 39 deletions docs/setup/deployment/development/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Require most recent version of [Docker](https://www.docker.com/get-started).

## Domains for local development

You will need two domain to run this app. One for the public site and one for the apply site.
You will need a domain to run this app.

Add this to your `/etc/hosts` file.

Expand Down Expand Up @@ -42,99 +42,99 @@ Run the docker compose command to build the images. This will take some time.
If you need to rebuild the images to get a later version just run the "build" again.

```shell
docker-compose --file docker/docker-compose.yaml build
docker compose --file docker/compose.yaml build
```

The build command needs to be run from Hypha root so it can copy needed files. The other commands are easier to run directly from the "docker" sub directory.

### Start the docker environment

Move to the "docker" directory.
To start the docker containers you use the "up --watch" command. This command you will use each time you want to start up and use this docker environment.

```shell
cd docker
docker compose --file docker/compose.yaml up --watch
```

To start the docker containers you use the "up" command. This command you will use each time you want to start up and use this docker environment.

```shell
docker-compose up
```
This will run "npm watch" as well as the "runserver_plus". All code changes to hypha will be synced in to the conatiner thanks to the docker watch functionality.

### Access the docker environment

Go to [http://hypha.test:8090/](http://hypha.test:8090/)
Go to [http://hypha.test:9001/](http://hypha.test:9001/)

### Stop the docker environment.

Press `ctrl+c` in the terminal window.

### Run commands in the docker environment

To get bash shell on the container that runs the Django app, use this command.

```shell
docker-compose exec py bash
docker exec -i -t hypha-django-dev bash
frjo marked this conversation as resolved.
Show resolved Hide resolved
```

Here you can issue django commands as normal. You might want to change the user - the default is circleci, but most of the code is owned by the user 'node'. To do that:
Here you can issue django commands as normal.

You can also run commands directly, e.g. "uv sync" like this.

```shell
docker-compose exec -u node py bash
docker exec hypha-django-dev uv sync
```

To get a shell on the container that runs Postgres, use this command.

```shell
docker-compose exec db bash
docker exec -i -t hypha-postgres-dev bash
```

### Stop the docker environment.

Press `ctrl+c` in the terminal window.

## Restore a database dump in Docker

We will use the "public/sandbox\_db.dump" for this example. That is a good start in any case, you get some example content etc.

First get a shell on the db container.
First copy the sandbox db dump into the container that runs Postgres.

```shell
docker-compose exec db bash
docker cp public/sandbox_db.dump hypha-postgres-dev:/tmp/
```

Then in that shell you need to install `wget` to download the db dump.
Get a shell on the container that runs Postgres.

```shell
apt update
apt install wget
docker exec -i -t hypha-postgres-dev bash
frjo marked this conversation as resolved.
Show resolved Hide resolved
```

Then download the sandbox db dump from Github.
Before being able to work on this database, you have to drop and prevent any other connections to it.

```shell
wget https://github.com/HyphaApp/hypha/raw/sandbox/public/sandbox_db.dump
psql --username=hypha -c "REVOKE CONNECT ON DATABASE hypha FROM public;SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'hypha';"
```

Before being able to work on this database, you have to drop and prevent any other connections to it.
With this done, drop and then create the hypha database and run the pg restore command like this.

```shell
psql
dropdb --username=hypha hypha
```

```sql
REVOKE CONNECT ON DATABASE hypha FROM public;
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'hypha';
```shell
createdb --username=hypha hypha
```

With this done, drop and then create the hypha database and run the pg restore command like this.
```shell
pg_restore --verbose --clean --if-exists --no-acl --no-owner --username=hypha --dbname=hypha /tmp/sandbox_db.dump
```

Exit the container shell.

```shell
dropdb --user=hypha hypha
createdb --user=hypha hypha
pg_restore --verbose --clean --if-exists --no-acl --no-owner --dbname=hypha --username=hypha sandbox_db.dump
exit
```

After restoring the sandbox db run the migrate command inside the py container.
Run the "migrate" and "sync_roles" commands inside the py container to update the db.

```shell
docker-compose exec py python3 manage.py migrate
docker-compose exec py python3 manage.py sync_roles
docker exec hypha-django-dev python3 manage.py migrate
```

```shell
docker exec hypha-django-dev python3 manage.py sync_roles
```

Done.
Loading
Loading