Skip to content

Commit db8914c

Browse files
committed
Modernize docker-compose.yml
* 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`
1 parent 8624601 commit db8914c

File tree

8 files changed

+82
-69
lines changed

8 files changed

+82
-69
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DATABASE_HOST=db
2+
DATABASE_PORT=5432
3+
DATABASE_NAME=app_db
4+
DATABASE_USER=app_user
5+
DATABASE_PASSWORD=changeme

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
wagtail
22
bakerydemo
33
libs
4+
.env
45
.idea
56
.vscode
67
.DS_Store

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Use an official Python runtime as a parent image
2-
FROM python:3.8-bullseye
2+
FROM python:3.9-bullseye
33
LABEL maintainer="hello@wagtail.org"
44

5-
# Set environment varibles
5+
# Set environment variables
66
ENV PYTHONUNBUFFERED 1
77

88
# Install libenchant and create the requirements folder.
@@ -26,3 +26,5 @@ RUN cd /code/wagtail/ \
2626
COPY ./libs/Willow /code/willow/
2727
RUN cd /code/willow/ \
2828
&& pip install -e .[testing]
29+
30+
WORKDIR /code/bakerydemo

Makefile

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,46 @@ help: ## ⁉️ - Display help comments for each make command
88
| sort
99

1010
build: ## Build the backend Docker image
11-
docker-compose build web
11+
docker compose build web
1212

1313
start: ## Bring the backend Docker container up
14-
docker-compose up
14+
docker compose up
1515

1616
stop: ## Stop the backend Docker container
17-
docker-compose stop
17+
docker compose stop
1818

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

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

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

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

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

3434
down: ## Stop and remove all Docker containers
35-
docker-compose down
35+
docker compose down
3636

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

4040
migrate: ## Migrate the wagtail bakery site migrations
41-
docker-compose exec web python manage.py migrate
41+
docker compose exec web python manage.py migrate
4242

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

4646
format-wagtail: ## Format Wagtail repo
47-
docker-compose exec -w /code/wagtail web make format-server
48-
docker-compose exec frontend make format-client
47+
docker compose exec -w /code/wagtail web make format-server
48+
docker compose exec frontend make format-client
4949

5050
lint-wagtail: ## Lint the Wagtail repo (server, client, docs)
51-
docker-compose exec -w /code/wagtail web make lint-server
52-
docker-compose exec -w /code/wagtail web make lint-docs
53-
docker-compose exec frontend make lint-client
51+
docker compose exec -w /code/wagtail web make lint-server
52+
docker compose exec -w /code/wagtail web make lint-docs
53+
docker compose exec frontend make lint-client

README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Initial work in Bristol sprint January 2020 by [esperk](https://github.com/esper
66

77
## Setup
88

9-
**Requirements:** [Docker](https://www.docker.com/) and Docker Compose (Docker Compose is included with Docker Desktop for Mac and Windows).
9+
**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).
1010

1111
Open a terminal and follow those instructions:
1212

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

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

3939
```sh
4040
# 6. Start your containers and wait for them to finish their startup scripts.
41-
docker-compose up
41+
docker compose up
4242
```
4343

44-
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`).
44+
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`).
4545

4646
````
4747
WARNINGS:
@@ -64,7 +64,7 @@ WARNINGS:
6464
If you're running this on Linux you might get into some privilege issues that can be solved using this command (tested on Ubuntu):
6565

6666
```sh
67-
CURRENT_UID=$(id -u):$(id -g) docker-compose -f docker-compose.yml -f docker-compose.linux.yml up
67+
CURRENT_UID=$(id -u):$(id -g) docker compose -f docker-compose.yml -f docker-compose.linux.yml up
6868
```
6969

7070
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.
@@ -77,7 +77,7 @@ Alternatively, if you're using VSCode and have the "Remote - Containers" extensi
7777
### See a list of running containers
7878

7979
```sh
80-
$ docker-compose ps
80+
$ docker compose ps
8181
Name Command State Ports
8282
--------------------------------------------------------------------------
8383
db docker-entrypoint.sh postgres Up 5432/tcp
@@ -94,7 +94,7 @@ make build
9494
or
9595

9696
```sh
97-
docker-compose build web
97+
docker compose build web
9898
```
9999

100100
### Bring the backend Docker container up
@@ -106,7 +106,7 @@ make start
106106
or
107107

108108
```sh
109-
docker-compose up
109+
docker compose up
110110
```
111111

112112
### Stop all Docker containers
@@ -118,7 +118,7 @@ make stop
118118
or
119119

120120
```sh
121-
docker-compose stop
121+
docker compose stop
122122
```
123123

124124
### Stop all and remove all Docker containers
@@ -130,7 +130,7 @@ make down
130130
or
131131

132132
```sh
133-
docker-compose down
133+
docker compose down
134134
```
135135

136136
### Run tests
@@ -142,7 +142,7 @@ make test
142142
or
143143

144144
```sh
145-
docker-compose exec -w /code/wagtail web python runtests.py
145+
docker compose exec -w /code/wagtail web python runtests.py
146146
```
147147

148148
### Run tests for a specific file
@@ -154,7 +154,7 @@ make test file=wagtail.admin.tests.test_name.py
154154
or
155155

156156
```sh
157-
docker-compose exec -w /code/wagtail web python runtests.py wagtail.admin.tests.{test_file_name_here}
157+
docker compose exec -w /code/wagtail web python runtests.py wagtail.admin.tests.{test_file_name_here}
158158
```
159159

160160
### Format Wagtail codebase
@@ -164,8 +164,8 @@ make format-wagtail
164164
```
165165
or
166166
```sh
167-
docker-compose exec -w /code/wagtail web make format-server
168-
docker-compose exec frontend make format-client
167+
docker compose exec -w /code/wagtail web make format-server
168+
docker compose exec frontend make format-client
169169
```
170170

171171
### Lint Wagtail codebase
@@ -175,9 +175,9 @@ make lint-wagtail
175175
```
176176
or
177177
```sh
178-
docker-compose exec -w /code/wagtail web make lint-server
179-
docker-compose exec -w /code/wagtail web make lint-docs
180-
docker-compose exec frontend make lint-client
178+
docker compose exec -w /code/wagtail web make lint-server
179+
docker compose exec -w /code/wagtail web make lint-docs
180+
docker compose exec frontend make lint-client
181181
```
182182

183183
### Open a Django shell session
@@ -189,7 +189,7 @@ make ssh-shell
189189
or
190190

191191
```sh
192-
docker-compose exec web python manage.py shell
192+
docker compose exec web python manage.py shell
193193
```
194194

195195
### Open a PostgreSQL shell session
@@ -201,7 +201,7 @@ make ssh-db
201201
or
202202

203203
```sh
204-
docker-compose exec web python manage.py dbshell
204+
docker compose exec web python manage.py dbshell
205205
```
206206

207207
### Open a shell on the web server
@@ -213,7 +213,7 @@ make ssh
213213
or
214214

215215
```sh
216-
docker-compose exec web bash
216+
docker compose exec web bash
217217
```
218218

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

227227
```sh
228-
docker-compose exec frontend bash
228+
docker compose exec frontend bash
229229
```
230230

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

239239
```sh
240-
docker-compose exec -w /code/wagtail web bash
240+
docker compose exec -w /code/wagtail web bash
241241
```
242242

243243
### Make migrations to the wagtail bakery site
@@ -249,7 +249,7 @@ make migrations
249249
or
250250

251251
```sh
252-
docker-compose exec web python manage.py makemigrations
252+
docker compose exec web python manage.py makemigrations
253253
```
254254

255255
### Migrate the wagtail bakery site
@@ -261,7 +261,7 @@ make migrate
261261
or
262262

263263
```sh
264-
docker-compose exec web python manage.py migrate
264+
docker compose exec web python manage.py migrate
265265
```
266266

267267
## Getting ready to contribute

docker-compose.yml

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
1-
version: '3'
2-
31
volumes:
42
postgres-data:
53
node_modules:
64

75
services:
86
web:
9-
container_name: "web"
10-
build: ./
11-
working_dir: /code/bakerydemo
7+
build:
8+
context: .
9+
dockerfile: ./Dockerfile
1210
command: python manage.py runserver 0.0.0.0:8000
13-
restart: "no"
1411
volumes:
15-
- ./wagtail:/code/wagtail:delegated,rw
16-
- ./bakerydemo:/code/bakerydemo:delegated,rw
17-
- node_modules:/code/wagtail/node_modules/
12+
- ./wagtail:/code/wagtail
13+
- ./bakerydemo:/code/bakerydemo
1814
ports:
1915
- "8000:8000"
2016
environment:
21-
DATABASE_URL: "postgres://wagtail:changeme@db/wagtail"
17+
DATABASE_URL: "postgres://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}/${DATABASE_NAME}"
2218
depends_on:
23-
- db
24-
- frontend
19+
db:
20+
condition: service_healthy
21+
frontend:
22+
condition: service_started
23+
2524
db:
26-
container_name: "db"
27-
image: postgres:12.3-alpine
2825
environment:
29-
POSTGRES_USER: wagtail
30-
POSTGRES_DB: wagtail
31-
POSTGRES_PASSWORD: changeme
26+
- "POSTGRES_DB=${DATABASE_NAME}"
27+
- "POSTGRES_USER=${DATABASE_USER}"
28+
- "POSTGRES_PASSWORD=${DATABASE_PASSWORD}"
29+
restart: unless-stopped
30+
image: postgres:14.1
3231
volumes:
3332
- postgres-data:/var/lib/postgresql/data
34-
restart: "no"
35-
expose:
36-
- "5432"
33+
healthcheck:
34+
test: "pg_isready --quiet --dbname=postgres://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}/${DATABASE_NAME}"
35+
interval: 10s
36+
timeout: 5s
37+
retries: 5
38+
3739
frontend:
38-
container_name: "frontend"
3940
build:
4041
context: .
4142
dockerfile: Dockerfile.frontend
4243
working_dir: /code/wagtail
4344
volumes:
44-
- ./wagtail:/code/wagtail:delegated,rw
45-
- node_modules:/code/wagtail/node_modules/
45+
- ./wagtail:/code/wagtail
46+
- node_modules:/code/wagtail/node_modules
4647
command: bash -c "echo 'Copying node_modules, this may take a few minutes...' && rsync -rah --info=progress2 /node_modules /code/wagtail/ && npm run start"
4748
restart: "no"
4849
tty: true

setup-db.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
# Fail if any command fails.
44
set -e
55

6-
docker-compose exec web python manage.py migrate --noinput
7-
docker-compose exec web python manage.py load_initial_data
8-
docker-compose exec web python manage.py update_index
6+
docker compose exec web python manage.py migrate --noinput
7+
docker compose exec web python manage.py load_initial_data
8+
docker compose exec web python manage.py update_index

setup.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ else
3333
echo Directory libs/Willow already exists, skipping...
3434
fi
3535

36+
if [ ! -f .env ]; then
37+
echo "Creating file for `docker compose` environment variables"
38+
cp .env.example .env
39+
fi
3640

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

0 commit comments

Comments
 (0)