Reworked the database and Wagtail installation setup (#18)

* Broke out the initial database stuff into a separate one-time script.

Also enhanced the .dockerignore file to make image builds a lot faster, and added .idea to the .gitignire, so that PyCharm projects won't get added to the repo.

* Reworked the database and Wagtail installation setup

In order to improve the development experience while using docker-wagtail-develop, I reorganized how the database setup is done, and how and when Wagtail gets installed into the python environment inside the docker container. Plus a few other changes.

1. Database. The migration and initial data load steps are now done once, by the user, during initial setup (setup-sb.sh), rather than happening at the start of every `docker-compose up`. This is possible because the postgres data is already being stored in a Docker Volume, so it persists across container executions.

2. Wagtail is now installed into the 'web' docker container by the Dockerfile, rather than docker-compose. This lets Docker skip that step if nothing has changed in Wagtail since the last build (admittedly, not super common, given that this project is for developing Wagtail), and generally makes the whole process a lot cleaner.

3. I added a bunch of stuff to .dockerignore, which significantly cuts down on the 'web' and 'frontend' images filesizes and build times.

4. I gave each of the three containers specific names, so that they can be more easily executed into for things like running manage.py commands or other OS stuff. This resulted in the connect string for the postgres DB needing to be tweaked.

5. I changed the 'frontend' container's copy command for the node_modules folder to use the -u flag, which allows cp to skip any files that haven't changed since the last time the copy was run. This *dramatically* speeds up the startup of the 'frontend' container on all but the very first execution (which does the initial copy into the host's wagtail folder).

I really wanted to find a way to put node_modules into a Docker Volume, but since you can't write to Volumes during the image build process, that unfortunately negates any value-add that we might have gotten from their use in this case.
pull/15/head^2
Robert Rollins 2020-10-09 09:58:58 -07:00 zatwierdzone przez GitHub
rodzic f473083142
commit 43782a1533
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
6 zmienionych plików z 40 dodań i 22 usunięć

8
.dockerignore 100644
Wyświetl plik

@ -0,0 +1,8 @@
# Don't copy the git repos, the compiled node_modules, or anything else that isn't needed into the docker image.
# This greatly speeds up re-building the image.
**/.git
**/node_modules
**/Dockerfile
**/docker-compose.yml
**/Procfile
**/Vagrantfile

2
.gitignore vendored
Wyświetl plik

@ -1,5 +1,5 @@
wagtail
bakerydemo
libs
.idea
.DS_Store

Wyświetl plik

@ -5,11 +5,18 @@ LABEL maintainer="hello@wagtail.io"
# Set environment varibles
ENV PYTHONUNBUFFERED 1
RUN apt-get update -y && apt-get install -y libenchant-dev
# Install libenchant and create the requirements folder.
RUN apt-get update -y \
&& apt-get install -y libenchant-dev \
&& mkdir -p /code/requirements
RUN mkdir -p /code/requirements
COPY ./bakerydemo/requirements/base.txt /code/requirements/base.txt
COPY ./bakerydemo/requirements/production.txt /code/requirements/production.txt
# Install the bakerydemo project's dependencies into the image.
COPY ./bakerydemo/requirements/* /code/requirements/
RUN pip install --upgrade pip \
&& pip install -r /code/requirements/production.txt
RUN pip install --upgrade pip
RUN pip install -r /code/requirements/production.txt
# Install wagtail from the host. This folder will be overriteen by a volume mount during run time (so that code
# changes show up immediately), but it also needs to be copied into the image now so that wagtail can be pip install'd.
COPY ./wagtail /code/wagtail/
RUN cd /code/wagtail/ \
&& pip install -e .[testing,docs]

Wyświetl plik

@ -37,10 +37,12 @@ Here is the resulting folder structure:
└── bakerydemo # Wagtail Bakery project used for development.
```
Once setup is over,
Once the build is complete:
```sh
# 6. Start your container setup
# 6. Run one-time databse setup, which will be persisted across container executions by Docker's Volumes system
setup-db.sh
# 7. Start your container setup
docker-compose up
# Success!
```

Wyświetl plik

@ -5,31 +5,23 @@ volumes:
services:
web:
container_name: "web"
build: ./
working_dir: /code/bakerydemo
command:
- /bin/bash
- -c
- |
cd /code/wagtail
pip install -e .[testing,docs]
cd /code/bakerydemo
python manage.py migrate --noinput
python manage.py load_initial_data
python manage.py update_index
python manage.py runserver 0.0.0.0:8000
command: ./manage.py runserver 0.0.0.0:8000
volumes:
- ./wagtail:/code/wagtail:delegated,rw
- ./bakerydemo:/code/bakerydemo:delegated,rw
ports:
- "8000:8000"
environment:
DATABASE_URL: postgres://wagtail:changeme@postgres/wagtail
DATABASE_URL: postgres://wagtail:changeme@db/wagtail
PYTHONPATH: /code/wagtail:/code/bakerydemo:$PYTHONPATH
depends_on:
- db
- frontend
db:
container_name: "db"
image: postgres:12.3-alpine
environment:
POSTGRES_USER: wagtail
@ -41,6 +33,7 @@ services:
expose:
- "5432"
frontend:
container_name: "frontend"
build:
context: .
dockerfile: Dockerfile.frontend
@ -51,5 +44,5 @@ services:
- /bin/sh
- -c
- |
cp -r /node_modules /code/wagtail
cp -ru /node_modules /code/wagtail
npm run start

8
setup-db.sh 100755
Wyświetl plik

@ -0,0 +1,8 @@
#!/usr/bin/env bash
# Fail if any command fails.
set -e
docker-compose exec web ./manage.py migrate --noinput
docker-compose exec web ./manage.py load_initial_data
docker-compose exec web ./manage.py update_index