Skip to content

Commit

Permalink
Modernize docker-compose.yml
Browse files Browse the repository at this point in the history
* Move database connection parameters to an environment file to avoid duplication
* Update to modern command `docker compose` vs `docker-compose`
* Remove the version number from the file since this is ignored by docker and causes IDEs to attempt to validate
* Removed mount of `node_modules` volume in `web` container since the `web` container does not have `node` installed
* Removed `:delegated,rw` since this was the incorrect option (it meant that the container had the authoritative view of the files, leading to long delays in synchronizing with the host which caused issues when trying to detect changes for migrations, etc.), and is no longer necessary in modern docker
* Added health checks for containers to avoid warnings/errors
* Updated base image to python:3.9-bullseye to at least match the version used in `bakerydemo`'s `docker-compose.yml`
* Updated base image to postgres:14.1 to match `bakerydemo`'s `docker-compose.yml`
  • Loading branch information
jsma committed Nov 11, 2023
1 parent 8624601 commit db8914c
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DATABASE_HOST=db
DATABASE_PORT=5432
DATABASE_NAME=app_db
DATABASE_USER=app_user
DATABASE_PASSWORD=changeme
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
wagtail
bakerydemo
libs
.env
.idea
.vscode
.DS_Store
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Use an official Python runtime as a parent image
FROM python:3.8-bullseye
FROM python:3.9-bullseye
LABEL maintainer="[email protected]"

# Set environment varibles
# Set environment variables
ENV PYTHONUNBUFFERED 1

# Install libenchant and create the requirements folder.
Expand All @@ -26,3 +26,5 @@ RUN cd /code/wagtail/ \
COPY ./libs/Willow /code/willow/
RUN cd /code/willow/ \
&& pip install -e .[testing]

WORKDIR /code/bakerydemo
34 changes: 17 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,46 @@ help: ## ⁉️ - Display help comments for each make command
| sort

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

start: ## Bring the backend Docker container up
docker-compose up
docker compose up

stop: ## Stop the backend Docker container
docker-compose stop
docker compose stop

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

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

ssh-fe: ## Open a shell to work with the frontend code (Node/NPM)
docker-compose exec frontend bash
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 web bash

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

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

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

migrate: ## Migrate the wagtail bakery site migrations
docker-compose exec web python manage.py migrate
docker compose exec web 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 web python runtests.py $(file) $(FILE)

format-wagtail: ## Format Wagtail repo
docker-compose exec -w /code/wagtail web make format-server
docker-compose exec frontend make format-client
docker compose exec -w /code/wagtail web 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 frontend make lint-client
docker compose exec -w /code/wagtail web make lint-server
docker compose exec -w /code/wagtail web make lint-docs
docker compose exec frontend make lint-client
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Initial work in Bristol sprint January 2020 by [esperk](https://github.com/esper

## Setup

**Requirements:** [Docker](https://www.docker.com/) and Docker Compose (Docker Compose is included with Docker Desktop for Mac and Windows).
**Requirements:** [Docker](https://www.docker.com/) and Docker Compose version 2.22 and later (Docker Compose is included with Docker Desktop for Mac and Windows).

Open a terminal and follow those instructions:

Expand All @@ -20,7 +20,7 @@ cd wagtail-dev/
# 4. Run the setup script. This will check out the bakerydemo project and local copies of wagtail and its dependencies.
./setup.sh
# 5. Build the containers
docker-compose build
docker compose build
```

It can take a while (typically 15-20 minutes) to fetch and build all dependencies and containers.
Expand All @@ -38,10 +38,10 @@ Once the build is complete:

```sh
# 6. Start your containers and wait for them to finish their startup scripts.
docker-compose up
docker compose up
```

You might see a message like this the first time you run your containers. This is normal because the frontend container has not finished building the assets for the Wagtail admin. Just wait a few seconds for the frontend container to finish building (you should see a message like `webpack compiled successfully in 15557 ms` and then stop and start your containers again (Ctrl+C + `docker-compose up`).
You might see a message like this the first time you run your containers. This is normal because the frontend container has not finished building the assets for the Wagtail admin. Just wait a few seconds for the frontend container to finish building (you should see a message like `webpack compiled successfully in 15557 ms` and then stop and start your containers again (Ctrl+C + `docker compose up`).

````
WARNINGS:
Expand All @@ -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 docker-compose.yml -f docker-compose.linux.yml 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 @@ -77,7 +77,7 @@ Alternatively, if you're using VSCode and have the "Remote - Containers" extensi
### See a list of running containers

```sh
$ docker-compose ps
$ docker compose ps
Name Command State Ports
--------------------------------------------------------------------------
db docker-entrypoint.sh postgres Up 5432/tcp
Expand All @@ -94,7 +94,7 @@ make build
or

```sh
docker-compose build web
docker compose build web
```

### Bring the backend Docker container up
Expand All @@ -106,7 +106,7 @@ make start
or

```sh
docker-compose up
docker compose up
```

### Stop all Docker containers
Expand All @@ -118,7 +118,7 @@ make stop
or

```sh
docker-compose stop
docker compose stop
```

### Stop all and remove all Docker containers
Expand All @@ -130,7 +130,7 @@ make down
or

```sh
docker-compose down
docker compose down
```

### Run tests
Expand All @@ -142,7 +142,7 @@ make test
or

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

### Run tests for a specific file
Expand All @@ -154,7 +154,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 web python runtests.py wagtail.admin.tests.{test_file_name_here}
```

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

### Lint Wagtail codebase
Expand All @@ -175,9 +175,9 @@ 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 frontend make lint-client
docker compose exec -w /code/wagtail web make lint-server
docker compose exec -w /code/wagtail web make lint-docs
docker compose exec frontend make lint-client
```

### Open a Django shell session
Expand All @@ -189,7 +189,7 @@ make ssh-shell
or

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

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

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

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

```sh
docker-compose exec web bash
docker compose exec web bash
```

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

```sh
docker-compose exec frontend bash
docker compose exec frontend bash
```

### Open a shell to work within the wagtail container
Expand All @@ -237,7 +237,7 @@ make ssh-fe
or

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

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

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

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

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

## Getting ready to contribute
Expand Down
47 changes: 24 additions & 23 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
version: '3'

volumes:
postgres-data:
node_modules:

services:
web:
container_name: "web"
build: ./
working_dir: /code/bakerydemo
build:
context: .
dockerfile: ./Dockerfile
command: python manage.py runserver 0.0.0.0:8000
restart: "no"
volumes:
- ./wagtail:/code/wagtail:delegated,rw
- ./bakerydemo:/code/bakerydemo:delegated,rw
- node_modules:/code/wagtail/node_modules/
- ./wagtail:/code/wagtail
- ./bakerydemo:/code/bakerydemo
ports:
- "8000:8000"
environment:
DATABASE_URL: "postgres://wagtail:changeme@db/wagtail"
DATABASE_URL: "postgres://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}/${DATABASE_NAME}"
depends_on:
- db
- frontend
db:
condition: service_healthy
frontend:
condition: service_started

db:
container_name: "db"
image: postgres:12.3-alpine
environment:
POSTGRES_USER: wagtail
POSTGRES_DB: wagtail
POSTGRES_PASSWORD: changeme
- "POSTGRES_DB=${DATABASE_NAME}"
- "POSTGRES_USER=${DATABASE_USER}"
- "POSTGRES_PASSWORD=${DATABASE_PASSWORD}"
restart: unless-stopped
image: postgres:14.1
volumes:
- postgres-data:/var/lib/postgresql/data
restart: "no"
expose:
- "5432"
healthcheck:
test: "pg_isready --quiet --dbname=postgres://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}/${DATABASE_NAME}"
interval: 10s
timeout: 5s
retries: 5

frontend:
container_name: "frontend"
build:
context: .
dockerfile: Dockerfile.frontend
working_dir: /code/wagtail
volumes:
- ./wagtail:/code/wagtail:delegated,rw
- node_modules:/code/wagtail/node_modules/
- ./wagtail:/code/wagtail
- node_modules:/code/wagtail/node_modules
command: bash -c "echo 'Copying node_modules, this may take a few minutes...' && rsync -rah --info=progress2 /node_modules /code/wagtail/ && npm run start"
restart: "no"
tty: true
6 changes: 3 additions & 3 deletions setup-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
# Fail if any command fails.
set -e

docker-compose exec web python manage.py migrate --noinput
docker-compose exec web python manage.py load_initial_data
docker-compose exec web python manage.py update_index
docker compose exec web python manage.py migrate --noinput
docker compose exec web python manage.py load_initial_data
docker compose exec web python manage.py update_index
4 changes: 4 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ else
echo Directory libs/Willow already exists, skipping...
fi

if [ ! -f .env ]; then
echo "Creating file for `docker compose` environment variables"
cp .env.example .env
fi

# Set up bakerydemo to use the Postgres database in the sister container
if [ ! -f bakerydemo/bakerydemo/settings/local.py ]; then
Expand Down

0 comments on commit db8914c

Please sign in to comment.