Skip to content

Commit cdc0490

Browse files
fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! !fixup Introduce devcontainer service container based development environment (#705)
1 parent 9a7e831 commit cdc0490

8 files changed

Lines changed: 90 additions & 15 deletions

File tree

.env.dist

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
COMPOSE_PROJECT_NAME=react-ui
88

99
# Docker compose ports for Docs server instances
10-
COMPOSE_START_DOCS_SERVER_PORT=8000
10+
COMPOSE_DOCS_SERVER_PORT=8000
1111

1212
# Docker compose ports for Playwright Component Testing report server
1313
COMPOSE_PLAYWRIGHT_REPORT_PORT=9323
1414

15-
# Flag whether to start JavaScript files watcher at container start
16-
COMPOSE_START_JS_FILES_WATCHER_AT_START=true
17-
18-
# Flag whether to start docs server at container start
19-
COMPOSE_START_DOCS_SERVER_AT_START=true
15+
# Flag whether the `node` and `docs` service containers should automatically install dependencies,
16+
# build, and run the application (JavaScript files watcher, docs server) when they start
17+
COMPOSE_AUTOSTART=false
2018

2119
# Ownership of the files created in the container
2220
# ⚠️ [Linux] This needs to be set to the output of `id --user`

docker-compose.base.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ services:
4848
node:
4949
build: docker/node
5050
user: ${COMPOSE_UID}:${COMPOSE_GID}
51-
command: sleep infinity
52-
entrypoint: sh -c 'if [ "$$COMPOSE_START_JS_FILES_WATCHER_AT_START" = "true" ]; then npm ci && npm start; fi'
51+
entrypoint: sh -c 'if [ "$$COMPOSE_AUTOSTART" = "true" ]; then sh scripts/auto-start-node.sh; else sleep infinity; fi'
5352
env_file:
5453
- .env
5554
volumes:
@@ -71,12 +70,11 @@ services:
7170
docs:
7271
build: docker/mkdocs
7372
user: ${COMPOSE_UID}:${COMPOSE_GID}
74-
command: sleep infinity
75-
entrypoint: sh -c 'if [ "$$COMPOSE_START_DOCS_SERVER_AT_START" = "true" ]; then mkdocs serve; fi'
73+
entrypoint: sh -c 'if [ "$$COMPOSE_AUTOSTART" = "true" ]; then sh scripts/auto-start-mkdocs.sh; else sleep infinity; fi'
7674
env_file:
7775
- .env
7876
ports:
79-
- ${COMPOSE_START_DOCS_SERVER_PORT}:8000
77+
- ${COMPOSE_DOCS_SERVER_PORT}:8000
8078
volumes:
8179
- .:/workspace:z
8280

docker/mkdocs/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
FROM squidfunk/mkdocs-material:9
1+
# We freezed the version of mkdocs-material to prevent issue with live reload
2+
# See <https://github.com/squidfunk/mkdocs-material/issues/8478>
3+
FROM squidfunk/mkdocs-material:9.6.20
24
RUN mkdir /workspace
35
WORKDIR /workspace

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"lint": "npm run eslint && npm run markdownlint && npm run stylelint",
5555
"markdownlint": "markdownlint-cli2 \"README.md\" \"src/**/*.md\"",
5656
"postbuild": "npm run copy",
57+
"postinstall": "sh scripts/write-lockfile-hash.sh",
5758
"precopy": "rm -rf dist && mkdir dist",
5859
"prepublishOnly": "npm run build",
5960
"start": "webpack --watch --mode=development",

scripts/auto-start-mkdocs.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
# Start the docs server
4+
echo "Starting the docs server..."
5+
mkdocs serve

scripts/auto-start-node.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
# File to read the hash of the package-lock.json
6+
LOCK_HASH_FILE="node_modules/.package-lock-hash"
7+
8+
# Parent directory of the script
9+
SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
10+
11+
# Change to the parent directory of the script
12+
cd "$SCRIPT_DIR/.."
13+
14+
# Install dependencies if node_modules is missing or out of date
15+
CURRENT_HASH=$(sha256sum package-lock.json | awk '{print $1}')
16+
if [ ! -d "node_modules" ]; then
17+
echo "Installing dependencies (node_modules directory is missing)..."
18+
npm ci
19+
elif [ ! -f "$LOCK_HASH_FILE" ]; then
20+
echo "Installing dependencies (lockfile of package-lock.json is missing)..."
21+
npm ci
22+
elif [ "$(cat "$LOCK_HASH_FILE")" != "$CURRENT_HASH" ]; then
23+
echo "Installing dependencies (package-lock.json has changed)..."
24+
npm ci
25+
fi
26+
27+
# Build the application (must be run prior to starting the server to ensure the latest code is used)
28+
echo "Building the application..."
29+
npm run build
30+
31+
# Start the application
32+
echo "Starting the application..."
33+
npm start

scripts/write-lockfile-hash.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
# File to store the hash of the package-lock.json
6+
LOCK_HASH_FILE="node_modules/.package-lock-hash"
7+
8+
# Parent directory of the script
9+
SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
10+
11+
# Change to the parent directory of the script
12+
cd "$SCRIPT_DIR/.."
13+
14+
# Record the hash of the lockfile we just installed against
15+
sha256sum package-lock.json | awk '{print $1}' > "$LOCK_HASH_FILE"

src/docs/contribute/general-guidelines.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,32 @@ The `devcontainer` depends on the following service containers defined in
163163
All service containers mount the workspace at `/workspace` so that file changes
164164
are shared.
165165

166+
## Automatic Service Bootstrap
167+
168+
> You can skip this section if you do not want to automatically
169+
> [install dependencies](#installing-dependencies), [build](#building), and
170+
> [run](#running) the application, or if you are not an experienced developer.
171+
172+
Setting `COMPOSE_AUTOSTART=true` in `.env` makes the `node` and `docs`
173+
service containers automatically install dependencies, build, and run the
174+
application when they start. The default is `false`.
175+
176+
Setting `COMPOSE_AUTOSTART=true` comes with the following trade-offs:
177+
178+
* **Changes to dependencies require a container restart.** The watcher owns the
179+
service container's entrypoint, so updating dependencies (e.g. pulling a
180+
branch that changes `package-lock.json`, or running `npm install <pkg>`)
181+
only takes effect after restarting the `node` service container. The same
182+
applies to changes that affect the documentation server.
183+
* **Service logs are not directly visible.** The watcher and docs server run in
184+
their own service containers rather than in your `devcontainer` shell, so
185+
their output is not shown alongside your regular terminal work. You have to
186+
inspect it via `docker compose logs <service>` from the host.
187+
188+
> If something is not working as expected, or you are not sure what is going on,
189+
> set `COMPOSE_AUTOSTART=false`, restart the containers, and follow the
190+
> manual steps in the sections below instead.
191+
166192
## Installing Dependencies
167193
168194
Run it on initial setup or when dependencies have changed:
@@ -187,9 +213,6 @@ mkdocs build
187213
188214
## Running
189215
190-
> See `.env` whether both the application and documentation server are not configured
191-
> to auto-start on container startup. If they are, you can skip the following steps.
192-
193216
To start building JavaScript files in watch mode:
194217
195218
```bash

0 commit comments

Comments
 (0)