Skip to content

Commit a44daab

Browse files
Docker improvements (#620)
## Status Ready for review ## What's changed? - Bump docker postgres to v17 - Add a healthcheck to the db container and ensure the api depends on that - Move the bundle and node_moduels to volumes for easier installs, automate install & db migrations via the entrypoints - Add an example override and update docs for dev containers - Force copilot to be installed to prevent corrupted state resulting in error: `Cannot find module '/root/.vscode-server/extensions/github.copilot-chat-0.33.1/dist/tikTokenizerWorker.js'` ## Steps to perform after deploying to production - Run `docker compose down -v --remove-orphans` to remove the volumes (especially postgres) - Run `docker compose build` to upgrade postgres <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Upgrade to Postgres 17 with healthchecks, add persistent bundle/node_modules volumes with automated installs via new entrypoints, and enhance dev-container config and docs. > > - **Docker/Compose**: > - Upgrade database image to `postgres:17` and client to `postgresql-client-17`. > - Add DB `healthcheck` and make `api` depend on healthy DB. > - Mount `bundle-data` and `node_modules` as named volumes; add tmpfs for `tmp/pids` and `tmp/cache`. > - **Entrypoints**: > - New `bin/docker-entrypoint.sh` and `bin/docker-debug-entrypoint.sh` run in strict mode, auto-run `bundle install`/`yarn install`, prepare DB, and start Rails (debug variant uses `rdbg`). > - **Dev Container**: > - Add GitHub Copilot extensions in `.devcontainer/devcontainer.json`. > - Provide `.devcontainer/docker-compose.yml` override and `docker-compose.override.yml.example` for using the `dev-container` target. > - **Docs**: > - Update `README.md` with dev-container usage and gem install guidance. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 565e417. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Adrian Lansdown <adrian.lansdown@raspberrypi.org>
1 parent b51a19b commit a44daab

7 files changed

Lines changed: 102 additions & 19 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@
6767
"shopify.ruby-lsp",
6868
"koichisasada.vscode-rdbg",
6969
"postman.postman-for-vscode",
70-
"ninoseki.vscode-mogami"
70+
"ninoseki.vscode-mogami",
71+
"GitHub.copilot",
72+
"GitHub.copilot-chat"
7173
],
7274
"settings": {
7375
"terminal.integrated.defaultProfile.linux": "zsh",
@@ -79,7 +81,7 @@
7981
}
8082
}
8183
}
82-
}
84+
},
8385

8486
// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
8587
// "remoteUser": "devcontainer"

Dockerfile

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
FROM ruby:3.2-slim-bullseye as base
1+
FROM ruby:3.2-slim-bullseye AS base
22
RUN gem install bundler \
33
&& apt-get update \
44
&& apt-get upgrade --yes \
55
&& apt-get install --yes --no-install-recommends \
66
libpq5 libxml2 libxslt1.1 libvips \
77
curl gnupg graphviz nodejs \
8-
&& echo "deb http://apt.postgresql.org/pub/repos/apt bullseye-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
9-
&& curl -sL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
8+
&& mkdir -p /usr/share/keyrings \
9+
&& curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /usr/share/keyrings/postgresql-archive-keyring.gpg \
10+
&& echo "deb [signed-by=/usr/share/keyrings/postgresql-archive-keyring.gpg] http://apt.postgresql.org/pub/repos/apt bullseye-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
1011
&& apt-get update \
11-
&& apt-get install --yes --no-install-recommends postgresql-client-15 \
12+
&& apt-get install --yes --no-install-recommends postgresql-client-17 \
1213
&& rm -rf /var/lib/apt/lists/* /var/lib/apt/archives/*.deb
1314
ENV TZ='Europe/London'
1415
ENV RUBYOPT='-W:no-deprecated -W:no-experimental'
@@ -31,11 +32,11 @@ FROM builder AS dev-container
3132
RUN apt-get update \
3233
&& apt-get install --yes --no-install-recommends sudo git vim zsh ssh curl less
3334
RUN sh -c "$(curl -L https://github.com/deluan/zsh-in-docker/releases/download/v1.1.5/zsh-in-docker.sh)" -- \
34-
-t robbyrussell \
35-
-p git -p docker-compose -p yarn \
36-
-p https://github.com/zsh-users/zsh-autosuggestions \
37-
# -p https://github.com/marlonrichert/zsh-autocomplete \
38-
-p https://github.com/unixorn/fzf-zsh-plugin
35+
-t robbyrussell \
36+
-p git -p docker-compose -p yarn \
37+
-p https://github.com/zsh-users/zsh-autosuggestions \
38+
# -p https://github.com/marlonrichert/zsh-autocomplete \
39+
-p https://github.com/unixorn/fzf-zsh-plugin
3940
RUN chsh -s $(which zsh) ${USER}
4041

4142
# Slim application image without development dependencies

README.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,48 @@ Start the application and its dependencies via docker:
3333
docker-compose up
3434
```
3535

36+
### Using the dev-container
37+
38+
There is a dedicated image called `dev-container`, and the easiest way to use this is to use the override:
39+
40+
`cp docker-compose.override.yml.example docker-compose.override.yml`
41+
42+
Now when you run `docker compose up -d` it will always use the `dev-container` image.
43+
44+
When adding a vscode project the dev-container should be automatically found and a dialog will show asking if
45+
you want to use this, but failing that you can open the cmd palette with `cmd + shift + p` and call:
46+
47+
`Dev Containers: Reopen in Container`
48+
49+
If there's an issue, do check the logs, but you can run:
50+
51+
`Dev Containers: Rebuild and Reopen in Container`
52+
53+
and there's also a 'without cache' variant, to ensure packages are re-built.
54+
3655
#### Updating gems inside the container
3756

38-
This can be done with the `bin/with-builder.sh` script:
57+
If running inside a dev-container:
58+
59+
```
60+
bundle install
61+
```
62+
63+
or outside with:
64+
65+
```
66+
docker compose run --rm --no-deps api bundle install
67+
```
68+
69+
This can also be done with the `bin/with-builder.sh` script:
3970

4071
```
4172
./bin/with-builder.sh bundle update
4273
```
4374

44-
which should update the Gems in the container, without the need for rebuilding.
75+
which should update the Gems in the container.
76+
77+
None of these methods require rebuilding.
4578

4679
### Seeding
4780

bin/docker-debug-entrypoint.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
#!/bin/bash
2+
set -euo pipefail # Use strict-mode in based to ensure errors are surfaced early
23

3-
rails db:prepare
4-
rdbg -n -o -c -- bin/rails s -p 3009 -b '0.0.0.0'
4+
# Check if bundle install needs to be run
5+
bundle check >/dev/null 2>&1 || bundle install --jobs "${BUNDLE_JOBS:-4}"
6+
7+
# Check if yarn install needs to be run
8+
if [ -f package.json ] && command -v yarn >/dev/null 2>&1; then
9+
yarn install --check-files --frozen-lockfile || yarn install --check-files
10+
fi
11+
12+
# Prepare the database
13+
bundle exec rails db:prepare
14+
15+
exec rdbg -n -o -c -- bundle exec rails s -p 3009 -b '0.0.0.0'

bin/docker-entrypoint.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
#!/bin/bash
2+
set -euo pipefail # Use strict-mode in based to ensure errors are surfaced early
23

3-
rails db:prepare
4-
rails server --port 3009 --binding 0.0.0.0
4+
# Check if bundle install needs to be run
5+
bundle check >/dev/null 2>&1 || bundle install --jobs "${BUNDLE_JOBS:-4}"
6+
7+
# Check if yarn install needs to be run
8+
if [ -f package.json ] && command -v yarn >/dev/null 2>&1; then
9+
yarn install --check-files --frozen-lockfile || yarn install --check-files
10+
fi
11+
12+
# Prepare the database
13+
bundle exec rails db:prepare
14+
15+
exec bundle exec rails server --port 3009 --binding 0.0.0.0
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
api:
3+
build:
4+
context: .
5+
target: dev-container
6+
command: bash bin/docker-debug-entrypoint.sh
7+
volumes:
8+
# - ${HOME}/.bashrc:/root/.bashrc:ro # Map a ~/.bashrc in your home directory for customising bash
9+
- ${HOME}/.ssh:/root/.ssh:ro # To share any ssh keys with the container
10+
- /var/run/docker.sock:/var/run/docker.sock
11+
- node_modules:/app/node_modules
12+
13+
volumes:
14+
node_modules: null

docker-compose.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
services:
22
db:
3-
image: postgres:14
3+
image: postgres:17
44
environment:
55
- POSTGRES_DB
66
- POSTGRES_PASSWORD
77
- POSTGRES_USER
8+
healthcheck:
9+
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres}"]
10+
interval: 5s
11+
timeout: 5s
12+
retries: 5
13+
start_period: 5s
814
ports:
915
- "5434:5432"
1016
volumes:
@@ -16,9 +22,12 @@ services:
1622
target: builder
1723
image: editor-api:builder
1824
depends_on:
19-
- db
25+
db:
26+
condition: service_healthy
2027
volumes:
2128
- .:/app
29+
- bundle-data:/usr/local/bundle
30+
- node_modules:/app/node_modules
2231
# This is here to avoid rails finding stale pid-files in tmp/pids and then
2332
# thinking it is already running:
2433
- type: tmpfs
@@ -46,3 +55,5 @@ services:
4655

4756
volumes:
4857
postgres-data:
58+
bundle-data:
59+
node_modules:

0 commit comments

Comments
 (0)