Skip to content

Instantly share code, notes, and snippets.

@7ute
Created March 16, 2024 22:28
Show Gist options
  • Save 7ute/6fa767de0bdc494c82037c11b7e89d40 to your computer and use it in GitHub Desktop.
Save 7ute/6fa767de0bdc494c82037c11b7e89d40 to your computer and use it in GitHub Desktop.
PNPM + Astro as node middleware + Docker with production and development environment
version: "3"
services:
astro:
image: my-app:0.0.1-dev
build:
target: development
volumes:
- "./public:/app/public"
- "./src:/app/src"
# Add whatever needs to be watched by the "astro dev" watcher
version: "3"
services:
astro:
image: my-app:0.0.1
build:
context: .
dockerfile: ./Dockerfile
target: production
expose:
- "4321:4321"
FROM node:lts-slim AS base
WORKDIR /app
EXPOSE 4321
ENV HOST=0.0.0.0
ENV PORT=4321
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
# Stage: prod-deps
# Preparing dependencies to include with production production
FROM base AS prod-deps
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
--mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile
# Stage: build
# Pre-building the app
FROM base AS build
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
--mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
COPY . .
RUN pnpm run build
# Stage: production
# Preparing the runtime with the pre-built app and production dependencies
FROM build AS production
COPY --from=prod-deps /app/node_modules /app/node_modules
COPY --from=build /app/dist /app/dist
COPY server.mjs ./
CMD node ./server.mjs
# Stage: development
# Running the app in development mode with a watcher
FROM base AS development
COPY . .
RUN pnpm install
CMD pnpm run astro dev --host
{
"type": "module",
"version": "0.0.1",
"scripts": {
[…]
"docker:up": "docker compose up",
"docker:up:dev": "docker compose -f compose.yml -f compose-dev.yml up",
"docker:down": "docker compose down",
[…]
},
}
import express from 'express';
import { handler as ssrHandler } from './dist/server/entry.mjs';
const app = express();
const base = '/';
app.use(base, express.static('dist/client/'));
app.use(ssrHandler);
app.listen(process.env.PORT ?? 4321);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment