From b5fa7e89d13b8305d72d5ab1062f0c58b2dfd0dd Mon Sep 17 00:00:00 2001 From: John-Scott Atlakson <24574+jsma@users.noreply.github.com> Date: Sat, 11 Nov 2023 18:49:46 -0800 Subject: [PATCH 1/2] Modernize `docker-compose.yml` * Move database connection parameters and other environment variables to an environment file * Remove the version number from docker-compose.yml since this is ignored by docker and causes IDEs to attempt to validate * Added health checks for containers to avoid warnings/errors (and removed "try again" notes from the readme) * Updated docs to use `exec` instead of `run` since the containers should already be running * Updated base images to latest stable versions (Python 3.12, PostgreSQL 16, and Redis 7) * Added missing notes about `update_index` --- .env.example | 15 ++++++++-- .github/workflows/lint.yml | 2 +- Dockerfile | 10 ++----- docker-compose.yml | 56 ++++++++++++++++++++------------------ docker-entrypoint.sh | 19 ------------- readme.md | 18 ++++++------ 6 files changed, 53 insertions(+), 67 deletions(-) delete mode 100755 docker-entrypoint.sh diff --git a/.env.example b/.env.example index 0d75c955d..e5ee20741 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,17 @@ -# This file contains Content Security Policy (CSP) directives to test Wagtail's compatibility with CSP. -# If the variables defined here are loaded into the environment, CSP will be enabled. +DJANGO_SETTINGS_MODULE=bakerydemo.settings.dev +REDIS_URL=redis://redis + +# Database connection settings: +DATABASE_HOST=db +DATABASE_PORT=5432 +DATABASE_NAME=app_db +DATABASE_USER=app_user +DATABASE_PASSWORD=changeme +DATABASE_URL=postgres://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST/$DATABASE_NAME + +# Content Security Policy (CSP) +# To test Wagtail's compatibility with CSP, configure the variables below to enable CSP. # These values are commented out by default because Wagtail is not (yet) compatible with # the strict policy defined below. diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 6671e8294..7527b5f67 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -16,7 +16,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v4 with: - python-version: 3.9 + python-version: 3.12 cache: 'pip' cache-dependency-path: 'requirements/*.txt' - name: Install dependencies diff --git a/Dockerfile b/Dockerfile index 82da0cadb..a1627b057 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.9-slim +FROM python:3.12-slim # Install packages needed to run your application (not build deps): # We need to recreate the /usr/share/man/man{1..8} directories first because @@ -33,7 +33,7 @@ RUN set -ex \ zlib1g-dev \ " \ && apt-get update && apt-get install -y --no-install-recommends $BUILD_DEPS \ - && python3.9 -m venv ${VIRTUAL_ENV} \ + && python3 -m venv ${VIRTUAL_ENV} \ && pip install -U pip \ && pip install --no-cache-dir -r /requirements/production.txt \ && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $BUILD_DEPS \ @@ -45,9 +45,6 @@ ADD . /code/ ENV PORT 8000 EXPOSE 8000 -# Add custom environment variables needed by Django or your settings file here: -ENV DJANGO_SETTINGS_MODULE=bakerydemo.settings.production DJANGO_DEBUG=off - # Call collectstatic with dummy environment variables: RUN DATABASE_URL=postgres://none REDIS_URL=none python manage.py collectstatic --noinput @@ -57,8 +54,5 @@ RUN mkdir -p /code/bakerydemo/media/images && chown -R 1000:2000 /code/bakerydem # mark the destination for images as a volume VOLUME ["/code/bakerydemo/media/images/"] -# start uWSGI, using a wrapper script to allow us to easily add more commands to container startup: -ENTRYPOINT ["/code/docker-entrypoint.sh"] - # Start uWSGI CMD ["uwsgi", "/code/etc/uwsgi.ini"] diff --git a/docker-compose.yml b/docker-compose.yml index 5c65d0361..b3911a5b6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,38 +1,40 @@ -version: '2' +volumes: + postgres-data: services: - db: - environment: - POSTGRES_DB: app_db - POSTGRES_USER: app_user - POSTGRES_PASSWORD: changeme - restart: unless-stopped - image: postgres:14.1 - expose: - - '5432' - - redis: - restart: unless-stopped - image: redis:6.2 - expose: - - '6379' - app: - environment: - DJANGO_SECRET_KEY: changeme - DATABASE_URL: postgres://app_user:changeme@db/app_db - REDIS_URL: redis://redis - DJANGO_SETTINGS_MODULE: bakerydemo.settings.dev build: context: . dockerfile: ./Dockerfile volumes: - ./bakerydemo:/code/bakerydemo - links: - - db:db - - redis:redis ports: - '8000:8000' depends_on: - - db - - redis + db: + condition: service_healthy + redis: + condition: service_healthy + + db: + environment: + - "POSTGRES_DB=${DATABASE_NAME}" + - "POSTGRES_USER=${DATABASE_USER}" + - "POSTGRES_PASSWORD=${DATABASE_PASSWORD}" + restart: unless-stopped + image: postgres:16 + volumes: + - postgres-data:/var/lib/postgresql/data + healthcheck: + test: "pg_isready --quiet --dbname=${DATABASE_URL}" + interval: 10s + timeout: 5s + retries: 5 + + redis: + restart: unless-stopped + image: redis:7 + expose: + - '6379' + healthcheck: + test: ["CMD-SHELL", "redis-cli ping || exit 1"] diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh deleted file mode 100755 index 576006e2e..000000000 --- a/docker-entrypoint.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh -set -e - -until psql $DATABASE_URL -c '\q'; do - >&2 echo "Postgres is unavailable - sleeping" - sleep 1 -done - ->&2 echo "Postgres is up - continuing" - -if [ "$1" = '/venv/bin/uwsgi' ]; then - /venv/bin/python manage.py migrate --noinput -fi - -if [ "x$DJANGO_LOAD_INITIAL_DATA" = 'xon' ]; then - /venv/bin/python manage.py load_initial_data -fi - -exec "$@" diff --git a/readme.md b/readme.md index 277b23795..7bde49202 100644 --- a/readme.md +++ b/readme.md @@ -89,19 +89,16 @@ Run the following commands: ```bash git clone https://github.com/wagtail/bakerydemo.git cd bakerydemo +cp .env.example .env docker compose up --build -d ``` -After this command completes and returns to the command prompt, wait 10 more seconds for the database setup to complete. Then run: +After this command completes and returns to the command prompt, run: ```bash -docker compose run app /venv/bin/python manage.py migrate -docker compose run app /venv/bin/python manage.py load_initial_data -``` -If this fails with a database error, wait 10 more seconds and re-try. Finally, run: - -```bash -docker compose up +docker compose exec app python manage.py migrate --noinput +docker compose exec app python manage.py load_initial_data +docker compose exec app python manage.py update_index ``` The demo site will now be accessible at [http://localhost:8000/](http://localhost:8000/) and the Wagtail admin @@ -125,7 +122,7 @@ You can run the Wagtail demo locally without setting up Vagrant or Docker and si #### Dependencies -- Python 3.7, 3.8, 3.9, 3.10 or 3.11 +- Python 3.8, 3.9, 3.10, 3.11, or 3.12 - [Virtualenv](https://virtualenv.pypa.io/en/stable/installation/) - [VirtualenvWrapper](https://virtualenvwrapper.readthedocs.io/en/latest/install.html) (optional) @@ -141,7 +138,7 @@ Confirm that this is showing a compatible version of Python 3.x. If not, and you deactivate rmvirtualenv wagtailbakerydemo - mkvirtualenv wagtailbakerydemo --python=python3.9 + mkvirtualenv wagtailbakerydemo --python=python3.12 python --version Now we're ready to set up the bakery demo project itself: @@ -161,6 +158,7 @@ To set up your database and load initial data, run the following commands: ./manage.py migrate ./manage.py load_initial_data + ./manage.py update_index ./manage.py runserver Log into the admin with the credentials `admin / changeme`. From 6c206039295b18fe5f0760f3f6b5c1884b595ffb Mon Sep 17 00:00:00 2001 From: John-Scott Atlakson <24574+jsma@users.noreply.github.com> Date: Sat, 11 Nov 2023 21:07:11 -0800 Subject: [PATCH 2/2] Updated Github CI to copy `.env` --- .github/workflows/docker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index da2151a2a..91be68668 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -14,7 +14,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: Build container - run: docker-compose build && docker-compose pull + run: cp .env.example .env && docker-compose build && docker-compose pull - name: Run migrations run: docker-compose run app /venv/bin/python manage.py migrate - name: Load initial data