FROM node:lts-bookworm-slim as base
# Install essential SSL and security certificates
RUN apt-get update && \
apt-get install -y --no-install-recommends openssl ca-certificates && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Build stage for compiling and preparing the application
FROM base as build
WORKDIR /myapp
# Install dependencies using package files
COPY package*.json .npmrc ./
RUN npm ci
# Copy all source files
COPY . .
# Patch the workerd configuration for container compatibility
# This is necessary because:
# 1. Default localhost binding doesn't work in containers
# 2. External access requires 0.0.0.0 binding
# 3. Both Shopify CLI and Hydrogen files need patching
RUN set -e && \
WORKERD_FILE=$(find node_modules/@shopify/cli/dist -type f -name "workerd-*.js") && \
if [ -f "$WORKERD_FILE" ]; then \
sed -i -e 's|host: "localhost"|host: "0.0.0.0"|' "$WORKERD_FILE"; \
else \
echo "workerd file not found" && exit 1; \
fi && \
HYDROGEN_WORKERD_FILE="node_modules/@shopify/cli-hydrogen/dist/lib/mini-oxygen/workerd.js" && \
if [ -f "$HYDROGEN_WORKERD_FILE" ]; then \
sed -i -e 's|host: "localhost"|host: "0.0.0.0"|' "$HYDROGEN_WORKERD_FILE"; \
else \
echo "hydrogen workerd file not found" && exit 1; \
fi
# Build the application
RUN npm run build
# Production stage with minimal footprint
FROM base as production
WORKDIR /myapp
# Set runtime environment
ENV NODE_ENV=production
ENV FLY="true"
ENV PORT="3000"
# Copy only necessary files from build stage
COPY --from=build /myapp/dist /myapp/dist
COPY --from=build /myapp/node_modules /myapp/node_modules
COPY --from=build /myapp/package.json /myapp/package.json
COPY --from=build /myapp/.env /myapp/.env
EXPOSE 3000
CMD ["npm", "run", "start"]