diff --git a/Dockerfile b/Dockerfile index dc973ea..ddf85e9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,36 +1,56 @@ +# Multi-stage build for GPX application + +# Using a specific version of Node +FROM node:18-alpine as base +# Install build dependencies +RUN apk add --no-cache python3 make g++ git + # Build stage for gpx library -FROM node:20-alpine AS gpx-builder +FROM base AS gpx-builder +WORKDIR /app +# Copy the gpx directory +COPY gpx/ ./gpx/ +# Install dependencies and build WORKDIR /app/gpx -COPY gpx/package*.json ./ -RUN npm install -COPY gpx/ ./ +RUN npm install --legacy-peer-deps RUN npm run build # Build stage for website -FROM node:20-alpine AS website-builder +FROM base AS website-builder +WORKDIR /app +# Copy the website directory +COPY website/ ./website/ +# Copy the built gpx library +COPY --from=gpx-builder /app/gpx/dist ./gpx/dist +# Install dependencies and build WORKDIR /app/website -COPY website/package*.json ./ -RUN npm install -COPY website/ ./ -COPY --from=gpx-builder /app/gpx/dist /app/gpx/dist -# RUN npm link /app/gpx - -ENV PUBLIC_MAPBOX_TOKEN="pk" +RUN npm install --legacy-peer-deps +# Use a dummy token for build time (will be replaced at runtime) +ENV PUBLIC_MAPBOX_TOKEN="pk.dummy_token_for_build" RUN npm run build -FROM node:20-alpine +# Production stage +FROM node:18-alpine WORKDIR /app -COPY --from=website-builder /app/website/package*.json ./ -COPY --from=website-builder /app/website/build ./build -COPY --from=website-builder /app/website/node_modules ./node_modules -COPY --from=website-builder /app/website/build/index.html ./build/index.html.template -RUN echo '#!/bin/sh\ncat ./build/index.html.template | sed "s/pk.dummy_token_for_build/$PUBLIC_MAPBOX_TOKEN/g" > ./build/index.html\nexec node build' > ./start.sh && \ +# Copy the built application +COPY --from=website-builder /app/website/build ./build +COPY --from=website-builder /app/website/package.json ./ +COPY --from=website-builder /app/website/node_modules ./node_modules + +# Create a startup script to replace the Mapbox token at runtime +COPY --from=website-builder /app/website/build/index.html ./build/index.html.template +RUN echo '#!/bin/sh' > ./start.sh && \ + echo 'sed "s/pk.dummy_token_for_build/$PUBLIC_MAPBOX_TOKEN/g" ./build/index.html.template > ./build/index.html' >> ./start.sh && \ + echo 'exec node build' >> ./start.sh && \ chmod +x ./start.sh +# Expose the port SvelteKit runs on EXPOSE 3000 +# Set environment variable for production ENV NODE_ENV=production ENV PUBLIC_MAPBOX_TOKEN="" -CMD ["./start.sh"] +# Command to run the application using our start script +CMD ["./start.sh"] \ No newline at end of file