Skip to content

Instantly share code, notes, and snippets.

@phyllisstein
Last active August 15, 2022 14:02
Show Gist options
  • Save phyllisstein/81396219b246a76c505d2120cd2ce571 to your computer and use it in GitHub Desktop.
Save phyllisstein/81396219b246a76c505d2120cd2ce571 to your computer and use it in GitHub Desktop.
Dockerize Node app without installing or linking node_modules.
**/node_modules/**
**/.git/**
**/dist/**
#!/usr/bin/env bash
# scripts/develop.sh
set -euxo pipefail
args="$*"
restart_server() {
echo "Starting development server..."
pkill -fi "start:dev" || true
yarn start:dev &
}
configure_watches() {
echo "Configuring watches..."
/usr/local/bin/watchman watch /app
for j in scripts/watchman/*.json; do
echo "Setting watch $j"
watchman -j <"$j"
done
}
watch_watchman() {
tail -f /usr/local/var/run/watchman/root-state/log
}
yarn_install() {
echo "Running yarn install..."
/usr/local/bin/yarn install
}
case $args in
serve)
restart_server
;;
watch)
yarn_install
restart_server
watch_watchman
;;
watches)
configure_watches
;;
yarn)
yarn_install
restart_server
;;
*)
echo "Unknown command: $args"
;;
esac
version: "3.8"
services:
app:
build:
context: .
ports:
- "3000:3000"
init: true
volumes:
- type: bind
source: .
target: /app
- type: volume
source: node_modules
target: /app/node_modules
volumes:
node_modules:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Watchman Builder ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
FROM alpine:3.9 AS watchman
RUN apk add --no-cache \
autoconf \
automake \
bash \
build-base \
libcrypto1.1 \
libgcc \
libstdc++ \
libtool \
linux-headers \
openssl-dev \
python3-dev
ENV WATCHMAN_VERSION=4.9.0 \
WATCHMAN_SHA256=1f6402dc70b1d056fffc3748f2fdcecff730d8843bb6936de395b3443ce05322
RUN cd /tmp \
&& wget -O watchman.tar.gz "https://github.com/facebook/watchman/archive/v${WATCHMAN_VERSION}.tar.gz" \
&& echo "$WATCHMAN_SHA256 *watchman.tar.gz" | sha256sum -c - \
&& tar -xz -f watchman.tar.gz -C /tmp/ \
&& rm -rf watchman.tar.gz
RUN cd /tmp/watchman-${WATCHMAN_VERSION} \
&& ./autogen.sh \
&& ./configure --enable-lenient \
&& make \
&& make install \
&& cd $HOME \
&& rm -rf /tmp/*
RUN strip /usr/local/bin/watchman
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ App ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
FROM node:18-alpine AS app
COPY --from=watchman /usr/local/bin/watchman* /usr/local/bin/
COPY --from=watchman /usr/local/var/run/watchman /usr/local/var/run/watchman
RUN apk add --no-cache bash git \
&& mkdir -p /usr/local/var/run/watchman
ENV PATH="/app/node_modules/.bin:$PATH"
WORKDIR /app
COPY scripts/develop.sh ./scripts/develop.sh
COPY scripts/watchman ./scripts/watchman
RUN ./scripts/develop.sh watches
CMD ["./scripts/develop.sh", "watch"]
// scripts/watchman/server-restart.json
[
"trigger",
"/app",
{
"name": "server-restart",
"expression": [
"anyof",
[
"name",
"vite.config.ts"
],
[
"name",
"index.html"
]
],
"command": [
"./scripts/develop.sh",
"serve"
],
"append_files": false
}
]
// scripts/watchman/watchman-reboot.json
[
"trigger",
"/app",
{
"name": "watchman-reboot",
"expression": [
"dirname",
"scripts/watchman"
],
"command": [
"./scripts/develop.sh",
"watches"
],
"append_files": false
}
]
// scripts/watchman/yarn-install.jsonc
[
"trigger",
"/app",
{
"name": "yarn-install",
"expression": [
"name",
"yarn.lock"
],
"command": [
"./scripts/develop.sh",
"yarn"
],
"append_files": false
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment