diff --git a/Dockerfile b/Dockerfile index 4d230d8..754c116 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,8 @@ ENV VIRTUAL_ENV=/home/mediacms.io ENV PATH="$VIRTUAL_ENV/bin:$PATH" ENV PIP_NO_CACHE_DIR=1 -ARG DEVELOPMENT_MODE=no +ARG DEVELOPMENT_MODE +ENV DEVELOPMENT=${DEVELOPMENT_MODE} RUN mkdir -p /home/mediacms.io/mediacms/{logs} && cd /home/mediacms.io && python3 -m venv $VIRTUAL_ENV @@ -16,7 +17,7 @@ COPY requirements.txt . COPY requirements-dev.txt . RUN set -x \ - && if [ "$DEVELOPMENT_MODE" = "yes" ]; then pip install -r requirements-dev.txt ; fi + && if [ "$DEVELOPMENT" = "True" ]; then pip install -r requirements-dev.txt ; fi RUN pip install -r requirements.txt diff --git a/cms/dev_settings.py b/cms/dev_settings.py index 24c6999..94575f4 100644 --- a/cms/dev_settings.py +++ b/cms/dev_settings.py @@ -1,7 +1,6 @@ -# Development settings, mainly used in docker-compose-dev.yaml -# Django runs in debug mode with runserver, uwsgi and nginx arenot loaded -# Django static loads everything from static/ folder +# Development settings, used in docker-compose-dev.yaml import os + BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) INSTALLED_APPS = [ @@ -28,7 +27,7 @@ INSTALLED_APPS = [ 'djcelery_email', 'ckeditor', 'drf_yasg', - 'corsheaders' + 'corsheaders', ] MIDDLEWARE = [ @@ -45,7 +44,5 @@ MIDDLEWARE = [ DEBUG = True CORS_ORIGIN_ALLOW_ALL = True -STATICFILES_DIRS = ( - os.path.join(BASE_DIR, 'static/'), -) -STATIC_ROOT = None \ No newline at end of file +STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static/'),) +STATIC_ROOT = None diff --git a/docker-compose-dev.yaml b/docker-compose-dev.yaml index e9c2597..034cbe8 100644 --- a/docker-compose-dev.yaml +++ b/docker-compose-dev.yaml @@ -5,6 +5,8 @@ services: build: context: . dockerfile: ./Dockerfile + args: + - DEVELOPMENT_MODE=True image: mediacms/mediacms-dev:latest volumes: - ./:/home/mediacms.io/mediacms/ diff --git a/docs/dev_exp.md b/docs/dev_exp.md index 72587e4..555b7ba 100644 --- a/docs/dev_exp.md +++ b/docs/dev_exp.md @@ -4,4 +4,57 @@ There is ongoing effort to provide a better developer experience and document it ## How to develop locally with Docker First install a recent version of [Docker](https://docs.docker.com/get-docker/), and [Docker Compose](https://docs.docker.com/compose/install/). +Then run `docker-compose -f docker-compose-dev.yaml up` +``` +user@user:~/mediacms$ docker-compose -f docker-compose-dev.yaml up +``` + +In a few minutes the app will be available at http://localhost . Login via admin/admin + +### What does docker-compose-dev.yaml do? +It build the two images used for backend and frontend. + +* Backend: `mediacms/mediacms-dev:latest` +* Frontend: `frontend` + +and will start all services required for MediaCMS, as Celery/Redis for asynchronous tasks, PostgreSQL database, Django and React + +For Django, the changes from the image produced by docker-compose.yaml are these: + +* Django runs in debug mode, with `python manage.py runserver` +* uwsgi and nginx are not run +* Django runs in Debug mode, with Debug Toolbar +* Static files (js/css) are loaded from static/ folder +* corsheaders is installed and configured to allow all origins + +For React, it will run `npm start` in the frontend folder, which will start the development server. +Check it on http://localhost:8088/ + +### How to develop in Django +Django starts at http://localhost and is reloading automatically. Making any change to the python code should refresh Django. + +### How to develop in React +React is started on http://localhost:8088/ , code is located in frontend/ , so making changes there should have instant effect on the page. Keep in mind that React is loading data from Django, and that it has to be built so that Django can serve it. + +### Making changes to the frontend + +The way React is added is more complicated than the usual SPA project and this is because React is used as a library loaded by Django Templates, so it is not a standalone project and is not handling routes etc. + +The two directories to consider are: +* frontend/src , for the React files +* templates/, for the Django templates. + +Django is using a highly intuitive hierarchical templating system (https://docs.djangoproject.com/en/4.2/ref/templates/), where the base template is templates/root.html and all other templates are extending it. + +React is called through the Django templates, eg templates/cms/media.html is loading js/media.js + +In order to make changes to React code, edit code on frontend/src and check it's effect on http://localhost:8088/ . Once ready, build it and copy it to the Django static folder, so that it is served by Django. + +### Development workflow with the frontend +1. Edit frontend/src/ files +2. Check changes on http://localhost:8088/ +3. Build frontend with `docker-compose -f docker-compose-dev.yaml exec frontend npm run dist` +4. Copy static files to Django static folder with`cp -r frontend/dist/static/* static/` +5. Restart Django - `docker-compose -f docker-compose-dev.yaml restart web` so that it uses the new static files +6. Commit the changes \ No newline at end of file