Skip to content

Commit

Permalink
Update Docker image to run server via pm2 instead of serving static f…
Browse files Browse the repository at this point in the history
…iles only
  • Loading branch information
stefandesu committed Nov 19, 2024
1 parent 670043d commit 40fbd41
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 54 deletions.
20 changes: 13 additions & 7 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
FROM node:20-alpine
WORKDIR /usr/src/app

## We need bash and git
RUN apk add --no-cache bash
RUN apk add --no-cache git

## Show version numbers for Node and npm
RUN node -v
RUN npm -v
# Copy and install dependencies
COPY package*.json ./
RUN npm ci

# Bundle app source
COPY . .

# Use pm2 to run app
RUN npm i -g pm2

ENV NODE_ENV=production

## Make Docker-related scripts available in root folder
COPY .docker/*.sh .

## Use http-server for serving the site
RUN npm i -g http-server
# Build Vue frontend for default configuration
RUN bash ./build.sh

CMD ["bash", "entrypoint.sh"]
41 changes: 24 additions & 17 deletions .docker/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# coli-rich-web Docker

Create a `docker-compose.yml` file:
## Supported Architectures
Currently, only `x86-64` is supported.

<!-- ## Available Tags
- The current release version is available under `latest`. However, new major versions might break compatibility of the previously used config file, therefore it is recommended to use a version tag instead.
- We follow SemVer for versioning the application. Therefore, `x` offers the latest image for the major version x, `x.y` offers the latest image for the minor version x.y, and `x.y.z` offers the image for a specific patch version x.y.z.
- Additionally, the latest development version is available under `dev`. -->

## Usage
It is recommended to run the image using [Docker Compose](https://docs.docker.com/compose/). Note that depending on your system, it might be necessary to use `sudo docker compose`. For older Docker versions, use `docker-compose` instead of `docker compose`.

1. Create a `docker-compose.yml` file:

```yml
services:
Expand All @@ -10,32 +21,28 @@ services:
# Store the built site in a volume (optional, but prevents having to rebuild the site if the container is recreated)
- ./data:/usr/src/app/dist
environment:
# Base for URL (e.g. when not running under root of domain)
# See main README for explanation of configuration options
- BASE=/
# Internal port can be changed via environment variable PORT
- PORT=3454
# When used in Docker, this needs to be a publicly available URL
- VITE_LOGIN_SERVER=http://localhost:3004
ports:
- 80:80
- 3454:3454
restart: unless-stopped
```
Start the container:
2. Start the container:
```sh
docker compose up -d
```

The app needs to be built after each update, as it is dependent on <!-- the specified environment variables and --> the Git commit. This update and build will be run in the background (if needed) each time the container is started.

To run the update manually without restarting the container (should be zero downtime):

```sh
docker compose exec -it coli-rich-web bash build.sh
```

Note that the container will clone the `main` branch of the site on first launch, then update the site via Git each time `build.sh` is run or the container is restarted.
This will create and start a coli-rich-web container running under host (and guest) port 3454. See [Configuration](#configuration) on how to configure it.

The app will be served on port 80.
You can now access the application under `http://localhost:3454`.

## Publishing the Docker Image
## Application Setup
After changing `docker-compose.yml` (e.g. adjusting environment variables), it is necessary to recreate the container to apply changes: `docker compose up -d`

For maintainers: As the site within the container uses Git to keep itself updated, updates to the published image won't be necessary unless there are changes to the image itself (`Dockerfile` or any of the Docker-related scripts). The Docker workflow on GitHub will therefore only build and publish a new image when necessary.
### Configuration
You can use environment variables via `environment` to configure coli-rich-web. Please refer to the [main documentation](../README.md#configuration) for more information and all available options.
28 changes: 8 additions & 20 deletions .docker/build.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
#!/bin/bash

# Clone site if not yet done
if [ ! -e .git ]; then
git init -b main
git remote add origin https://github.com/gbv/coli-rich-web.git
git fetch
git checkout -t origin/main
fi

# Pull changes
git pull
# We need to rebuild the front-end if environment differs from default

env () {
echo "$(git rev-parse HEAD) ### $BASE"
}
env_file=dist/.build-env
base_file=dist/.base
base="$BASE /// $VITE_LOGIN_SERVER"

if [ -e $env_file ] && [ "$(env)" == "$(cat $env_file)" ]; then
echo "Site rebuild skipped because there was no update."
if [[ -e $base_file && "$base" == "$(cat $base_file)" ]]; then
echo "Front-end rebuild skipped."
else
# Might need to update dependencies after each pull
npm ci
echo "Rebuilding front-end because configuration changed..."
# Build the site
npm run build
# Remember the current commit
env > $env_file
# Remember the current environment
echo "$base" > $base_file
fi
4 changes: 3 additions & 1 deletion .docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
- ./data:/usr/src/app/dist
environment:
- BASE=/
- PORT=3454
- VITE_LOGIN_SERVER=http://localhost:3004
ports:
- 3454:80
- 3454:3454
restart: unless-stopped
5 changes: 1 addition & 4 deletions .docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@
# Build site in background
bash build.sh &

PORT=${PORT:-80}
echo "Starting http-server on port $PORT..."

http-server -s -d false -p "$PORT" /usr/src/app/dist
pm2-runtime src/server/server.js
17 changes: 12 additions & 5 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
name: Publish Docker
# Only run when Docker-related files are changed
on:
push:
branches:
- main
paths:
- .docker/Dockerfile
- .docker/entrypoint.sh
- .docker/build.sh
- dev
tags:
- v*
paths-ignore:
- '**.md'
- '.github/**'
- '.husky/**'
- '.vscode/**'
- jsconfig.json
- .gitignore
- '.eslint**'

# Adapted from:
# - https://github.com/docker/metadata-action#semver
Expand All @@ -30,6 +36,7 @@ jobs:
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# TODO: Adjust this as soon as we have semver
tags: |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
-
Expand Down

0 comments on commit 40fbd41

Please sign in to comment.