Skip to content

Commit

Permalink
Updates and optimizations
Browse files Browse the repository at this point in the history
Renames docker-compose.yml => compose.yaml which is the new preferred name going forward
Moves Dockerfiles to subdirectory, ensuring the filenames end with Dockerfile (for syntax highlighting in IDEs)
Renamed the `web` service to `app`, since this is typical for Docker examples and is also what bakerydemo uses
Various tricks and tips to optimize the build cache to speed up subsequent builds

The new Dockerfile was partially based on the output of `docker init`, which includes some best practices:
* create non-root users to run the applications
* use bind-mounts for pip & npm installs (this means that when adding a new requirement, it will only download the new package and install everything else from the cache)

NOTE: the wagtail and libs directories are no longer COPY'd into the container for doing a `pip install`, but installing wagtail and django-modelcluster creates .egg-info directories, so the bind mounts need write access.

Added custom ignore files for each Dockerfile to restrict the build context to the absolute minimum

Added a custom requirements file to avoid relying on bakerydemo's requirements which results in duplicate installs.
The previous setup was installing bakerydemo requirements, which installed wagtail, django-modelcluster, and Willow, and then a few steps later, reinstalling these packages from the local sources. Now they are only installed once.

The CMDs for both `app` and `frontend` are now wrapped with `dumb-init` (node is not designed to run as PID 1) which handles killing the processes. Previously attempting the Ctrl-C the running containers would hang waiting for `npm` to give up the ghost, now it exits immediately.
  • Loading branch information
jsma committed Nov 13, 2023
1 parent 7b87fc5 commit 01ad11c
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "Existing Docker Compose (Extend)",
"dockerComposeFile": [
"../docker-compose.yml",
"../compose.yml",
"docker-compose.yml"
],
"service": "web",
Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
web:
volumes:
- ./.vscode:/code/.vscode:delegated,rw
- ./.vscode:/code/.vscode

# Overrides default command so things don't shut down after the process ends.
command: /bin/sh -c "while sleep 1000; do :; done"
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ DATABASE_HOST=db
DATABASE_PORT=5432
DATABASE_NAME=app_db
DATABASE_USER=app_user
DATABASE_PASSWORD=changeme
DATABASE_PASSWORD=changeme
DATABASE_URL=postgres://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST/$DATABASE_NAME
30 changes: 0 additions & 30 deletions Dockerfile

This file was deleted.

7 changes: 0 additions & 7 deletions Dockerfile.frontend

This file was deleted.

22 changes: 11 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ help: ## ⁉️ - Display help comments for each make command
| sort

build: ## Build the backend Docker image
docker compose build web
docker compose build app

start: ## Bring the backend Docker container up
docker compose up
Expand All @@ -17,37 +17,37 @@ stop: ## Stop the backend Docker container
docker compose stop

ssh: ## Enter the running backend Docker container for the wagtail bakery site
docker compose exec web bash
docker compose exec app bash

ssh-shell: ## Enter the running Docker container shell
docker compose exec web python manage.py shell
docker compose exec app python manage.py shell

ssh-fe: ## Open a shell to work with the frontend code (Node/NPM)
docker compose exec frontend bash

ssh-wagtail: ## Enter the running Docker container for the wagtail development environment
docker compose exec -w /code/wagtail web bash
docker compose exec -w /code/wagtail app bash

ssh-db: ## Open a PostgreSQL shell session
docker compose exec web python manage.py dbshell
docker compose exec app python manage.py dbshell

down: ## Stop and remove all Docker containers
docker compose down

migrations: ## Make migrations to the wagtail bakery site
docker compose exec web python manage.py makemigrations
docker compose exec app python manage.py makemigrations

migrate: ## Migrate the wagtail bakery site migrations
docker compose exec web python manage.py migrate
docker compose exec app python manage.py migrate

test: ## Run all wagtail tests or pass in a file with `make test file=wagtail.admin.tests.test_name`
docker compose exec -w /code/wagtail web python runtests.py $(file) $(FILE)
docker compose exec -w /code/wagtail app python runtests.py $(file) $(FILE)

format-wagtail: ## Format Wagtail repo
docker compose exec -w /code/wagtail web make format-server
docker compose exec -w /code/wagtail app make format-server
docker compose exec frontend make format-client

lint-wagtail: ## Lint the Wagtail repo (server, client, docs)
docker compose exec -w /code/wagtail web make lint-server
docker compose exec -w /code/wagtail web make lint-docs
docker compose exec -w /code/wagtail app make lint-server
docker compose exec -w /code/wagtail app make lint-docs
docker compose exec frontend make lint-client
43 changes: 20 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ WARNINGS:
If you're running this on Linux you might get into some privilege issues that can be solved using this command (tested on Ubuntu):

```sh
CURRENT_UID=$(id -u):$(id -g) docker compose -f docker-compose.yml -f docker-compose.linux.yml up
CURRENT_UID=$(id -u):$(id -g) docker compose -f compose.yaml -f compose.linux.yaml up
```

Alternatively, if you're using VSCode and have the "Remote - Containers" extension, you can open the command palette and select "Remote Containers - Reopen in Container" to attach VSCode to the container. This allows for much deeper debugging.
Expand All @@ -78,11 +78,10 @@ Alternatively, if you're using VSCode and have the "Remote - Containers" extensi

```sh
$ docker compose ps
Name Command State Ports
--------------------------------------------------------------------------
db docker-entrypoint.sh postgres Up 5432/tcp
frontend docker-entrypoint.sh /bin/ ... Up
web ./manage.py runserver 0.0. ... Up 0.0.0.0:8000->8000/tcp
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
wagtail-dev-app-1 wagtail-dev-app "/bin/sh -c 'python …" app 10 minutes ago Up 10 minutes 0.0.0.0:8000->8000/tcp
wagtail-dev-db-1 postgres:16.0-bookworm "docker-entrypoint.s…" db 10 minutes ago Up 10 minutes (healthy) 5432/tcp
wagtail-dev-frontend-1 wagtail-dev-frontend "docker-entrypoint.s…" frontend 10 minutes ago Up 10 minutes
```

### Build the backend Docker image
Expand All @@ -94,7 +93,7 @@ make build
or

```sh
docker compose build web
docker compose build app
```

### Bring the backend Docker container up
Expand Down Expand Up @@ -142,7 +141,7 @@ make test
or

```sh
docker compose exec -w /code/wagtail web python runtests.py
docker compose exec -w /code/wagtail app python runtests.py
```

### Run tests for a specific file
Expand All @@ -154,7 +153,7 @@ make test file=wagtail.admin.tests.test_name.py
or

```sh
docker compose exec -w /code/wagtail web python runtests.py wagtail.admin.tests.{test_file_name_here}
docker compose exec -w /code/wagtail app python runtests.py wagtail.admin.tests.{test_file_name_here}
```

### Format Wagtail codebase
Expand All @@ -164,7 +163,7 @@ make format-wagtail
```
or
```sh
docker compose exec -w /code/wagtail web make format-server
docker compose exec -w /code/wagtail app make format-server
docker compose exec frontend make format-client
```

Expand All @@ -175,8 +174,8 @@ make lint-wagtail
```
or
```sh
docker compose exec -w /code/wagtail web make lint-server
docker compose exec -w /code/wagtail web make lint-docs
docker compose exec -w /code/wagtail app make lint-server
docker compose exec -w /code/wagtail app make lint-docs
docker compose exec frontend make lint-client
```

Expand All @@ -189,7 +188,7 @@ make ssh-shell
or

```sh
docker compose exec web python manage.py shell
docker compose exec app python manage.py shell
```

### Open a PostgreSQL shell session
Expand All @@ -201,7 +200,7 @@ make ssh-db
or

```sh
docker compose exec web python manage.py dbshell
docker compose exec app python manage.py dbshell
```

### Open a shell on the web server
Expand All @@ -213,7 +212,7 @@ make ssh
or

```sh
docker compose exec web bash
docker compose exec app bash
```

### Open a shell to work with the frontend code (Node/NPM)
Expand All @@ -237,7 +236,7 @@ make ssh-fe
or

```sh
docker compose exec -w /code/wagtail web bash
docker compose exec -w /code/wagtail app bash
```

### Make migrations to the wagtail bakery site
Expand All @@ -249,7 +248,7 @@ make migrations
or

```sh
docker compose exec web python manage.py makemigrations
docker compose exec app python manage.py makemigrations
```

### Migrate the wagtail bakery site
Expand All @@ -261,14 +260,14 @@ make migrate
or

```sh
docker compose exec web python manage.py migrate
docker compose exec app python manage.py migrate
```

## Getting ready to contribute

Here are other actions you will likely need to do to make your first contribution to Wagtail.

Set up git remotes to Wagtail forks (run these lines outside of the Docker instances, on your machine):
Set up git remotes to Wagtail forks (run these commands on your machine, not within Docker):

```sh
cd ~/Development/wagtail-dev/wagtail
Expand All @@ -280,10 +279,8 @@ git remote add upstream git@github.com:wagtail/wagtail.git
git pull --all
```

## Contributing to Willow

You can use the same setup to contribute to Willow.
You simply do the same operations to fork the Willow project and point your local copy of Willow to your fork.
You can use the same steps to contribute to any of the dependencies installed in `./libs`.
By default, `django-modelcluster` and `Willow` are checked out from `git` into this folder.

## See also

Expand Down
File renamed without changes.
42 changes: 42 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
volumes:
postgres-data:
node_modules:

services:
app:
build:
context: .
dockerfile: docker/app.Dockerfile
volumes:
- ./wagtail:/code/wagtail
- ./libs:/code/libs
- ./bakerydemo:/code/bakerydemo
ports:
- "8000:8000"
depends_on:
db:
condition: service_healthy
frontend:
condition: service_started

db:
environment:
- "POSTGRES_DB=${DATABASE_NAME}"
- "POSTGRES_USER=${DATABASE_USER}"
- "POSTGRES_PASSWORD=${DATABASE_PASSWORD}"
restart: always
image: postgres:16.0-bookworm
volumes:
- postgres-data:/var/lib/postgresql/data
healthcheck:
test: "pg_isready --quiet --dbname=${DATABASE_URL}"
interval: 10s
timeout: 5s
retries: 5

frontend:
build:
context: .
dockerfile: docker/frontend.Dockerfile
volumes:
- ./wagtail:/code/wagtail
49 changes: 0 additions & 49 deletions docker-compose.yml

This file was deleted.

Loading

0 comments on commit 01ad11c

Please sign in to comment.