Skip to content

Commit 8c5a2e7

Browse files
committed
Update Dockerfile to support Vite
Building the files in a node container but deploying using nginx, removing the need for npm server package and reducing the container size
1 parent 51b6ff9 commit 8c5a2e7

2 files changed

Lines changed: 47 additions & 21 deletions

File tree

Dockerfile

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
1-
FROM node:23-alpine3.21 AS base
2-
3-
# Install dependencies
4-
FROM base AS deps
1+
# Build stage
2+
FROM node:22-alpine3.21 AS builder
53

64
WORKDIR /app
75

8-
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
6+
# Install libc6-compat if needed
97
RUN apk add --no-cache libc6-compat
108

9+
# Install dependencies
1110
COPY package.json package-lock.json ./
12-
1311
RUN npm ci
1412

15-
# Build the app with the node modules installed
16-
FROM base AS builder
17-
18-
WORKDIR /app
19-
20-
COPY --from=deps /app/node_modules ./node_modules
13+
# Build the Vite app
2114
COPY . .
22-
2315
RUN npm run build
2416

25-
# Create a new image with the build files
26-
FROM base AS runner
17+
# Final stage: nginx serving dist/
18+
FROM nginx:stable-alpine as runner
2719

28-
WORKDIR /app
20+
# Remove default nginx website
21+
RUN rm -rf /usr/share/nginx/html/*
22+
23+
# Copy custom nginx config
24+
COPY nginx.conf /etc/nginx/nginx.conf
2925

30-
COPY --from=builder /app/build ./build
31-
RUN npm install -g serve
26+
# Copy built Vite files
27+
COPY --from=builder /app/dist /usr/share/nginx/html
3228

29+
# Healthcheck for nginx
3330
HEALTHCHECK --interval=10s --timeout=5s --start-period=5s --retries=3 \
34-
CMD wget --no-verbose --tries=1 --spider http://0.0.0.0:3000/health || exit 1
31+
CMD wget --no-verbose --tries=1 --spider http://0.0.0.0/ || exit 1
3532

36-
EXPOSE 3000
33+
# Expose port 80 (standard HTTP)
34+
EXPOSE 80
3735

38-
CMD ["serve", "-s", "build"]
36+
# Start nginx automatically
37+
CMD ["nginx", "-g", "daemon off;"]

nginx.conf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
worker_processes 1;
2+
3+
events { worker_connections 1024; }
4+
5+
http {
6+
include mime.types;
7+
default_type application/octet-stream;
8+
sendfile on;
9+
keepalive_timeout 65;
10+
11+
server {
12+
listen 80;
13+
14+
server_name _;
15+
16+
root /usr/share/nginx/html;
17+
index index.html;
18+
19+
location / {
20+
try_files $uri $uri/ /index.html;
21+
}
22+
23+
# Enable gzip compression
24+
gzip on;
25+
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
26+
}
27+
}

0 commit comments

Comments
 (0)