Skip to content

Commit 382e300

Browse files
committed
Fix inefficient double build by using runtime-only Docker image
- Stop building twice: CodeBuild builds, Docker just packages runtime - Create simple Dockerfile.runtime that only copies pre-built artifacts - Use lightweight Node.js Alpine image for runtime - Eliminate database connection issues in Docker build phase - Much faster builds: no duplicate Next.js compilation or Prisma generation This fixes the build failure by avoiding the need for database access during Docker build, since all compilation is done in CodeBuild phase.
1 parent 569ca5d commit 382e300

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

apps/web/buildspec.yml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,36 @@ phases:
5555
- ls -la .next/standalone/public/ || echo "❌ .next/standalone/public/ not found"
5656
- ls -la .next/standalone/server.js || echo "❌ server.js not found"
5757

58-
- echo "=== BUILDING DOCKER IMAGE WITH BUILD ARTIFACTS ==="
59-
- echo "Using proper Docker build process with Prisma generation..."
60-
- docker build --build-arg DATABASE_URL="$DATABASE_URL" --build-arg NODE_ENV=production -t $IMAGE_REPO_NAME:$IMAGE_TAG .
58+
- echo "=== BUILDING DOCKER RUNTIME IMAGE WITH PRE-BUILT ARTIFACTS ==="
59+
- echo "Creating runtime-only Docker image with build artifacts..."
60+
- |
61+
cat > Dockerfile.runtime << 'EOF'
62+
# Use Node.js Alpine for smaller runtime image
63+
FROM node:18-alpine AS runtime
64+
65+
WORKDIR /app
66+
67+
ENV NODE_ENV=production
68+
ENV NEXT_TELEMETRY_DISABLED=1
69+
ENV PORT=3000
70+
ENV HOSTNAME="0.0.0.0"
71+
72+
# Copy the complete standalone build from CodeBuild
73+
COPY .next/standalone ./
74+
75+
# Create non-root user for security
76+
RUN addgroup -g 1001 -S nodejs && \
77+
adduser -S nextjs -u 1001 && \
78+
chown -R nextjs:nodejs /app
79+
80+
USER nextjs
81+
82+
EXPOSE 3000
83+
84+
# Use node to run the standalone server.js
85+
CMD ["node", "server.js"]
86+
EOF
87+
- docker build -f Dockerfile.runtime -t $IMAGE_REPO_NAME:$IMAGE_TAG .
6188
- docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $REPOSITORY_URI:$IMAGE_TAG
6289
- docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $REPOSITORY_URI:latest
6390

0 commit comments

Comments
 (0)