Skip to content

Commit

Permalink
Added voluming example
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-chambers committed Dec 12, 2023
1 parent 1c8f92a commit a3a12a4
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
16 changes: 15 additions & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on: push
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
VOLUMING_IMAGE_NAME: ${{ github.repository }}-voluming

# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
jobs:
Expand All @@ -16,7 +17,7 @@ jobs:
permissions:
contents: read
packages: write
#
#
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down Expand Up @@ -44,3 +45,16 @@ jobs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

- name: Extract metadata (tags, labels) for Docker (Voluming Image)
id: meta-voluming
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.VOLUMING_IMAGE_NAME }}
- name: Build and push Docker image (Voluming Image)
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
file: voluming.Dockerfile
push: true
tags: ${{ steps.meta-voluming.outputs.tags }}
labels: ${{ steps.meta-voluming.outputs.labels }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
fly.toml
dist
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
voluming-example:
build:
context: .
dockerfile: voluming.Dockerfile
volumes:
- ./src:/app/src
- ./package.json:/app/package.json
- ./package-lock.json:/app/package-lock.json
- ./tsconfig.json:/app/tsconfig.json
- cache-volume:/cache

volumes:
cache-volume: {}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ process.on('SIGINT', () => {
process.exit(0);
});

process.on('SIGTERM', () => {
server.log.info("terminated");
process.exit(0);
});

const start = async () => {
try {
await server.listen({port: port, host: "0.0.0.0"});
Expand Down
58 changes: 58 additions & 0 deletions voluming-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/sh
set -eu -o pipefail

cd /app

create_cache_symlink () {
local SOURCE="$1"
local CACHE_BASE_DIR="$2"
local CACHE_DEST="$2/$(basename $1)"

# If the cache already exists, discard whatever's already
# there are use the cache
if [ -d "$CACHE_DEST" ]
then
rm -rf "$SOURCE"
echo "Cache exists in $CACHE_DEST, replacing $SOURCE"
fi

# If there's something already there (and the cache does not exist)
# move it into the cache
if [ -d "$SOURCE" ]
then
mv -f "$SOURCE" "$CACHE_BASE_DIR"
echo "Prepopulating cache $CACHE_DEST from $SOURCE"
fi

if [ ! -d "$CACHE_DEST" ]
then
mkdir -p "$CACHE_DEST"
fi

ln -s "$CACHE_DEST" "$SOURCE"
echo "Linked $SOURCE to cache $CACHE_DEST"
}

if [ -d "/cache" ] # If a caching volume has been mounted, use the cache
then
create_cache_symlink "/root/.npm" "/cache"

# npm deletes any node_modules symlink (see https://github.com/npm/arborist/security/advisories/GHSA-gmw6-94gg-2rc2)
# so we will detect if the cache has already been populated, and if
# not we will install packages, then create the cache afterwards.
# If the cache has been populated, we will just use it, and not run npm
# so that it will not erase our symlink
if [ ! -d "/cache/node_modules" ]
then
npm ci
fi
create_cache_symlink "/app/node_modules" "/cache"
else
npm ci
fi

# Can't use npm scripts here because npm doesn't handle SIGTERM properly.
# Using exec, becaus it replaces the shell process with the node process
# so that it receives the SIGTERM signal properly. Otherwise the shell just
# eats the signal.
exec ./node_modules/.bin/ts-node --transpileOnly ./src/index.ts
10 changes: 10 additions & 0 deletions voluming.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:18-alpine AS base

WORKDIR /app

RUN rm -rf /root/.npm

EXPOSE 8100

COPY ./voluming-start.sh /voluming-start.sh
CMD [ "sh", "/voluming-start.sh" ]

0 comments on commit a3a12a4

Please sign in to comment.