Commit
·
8502b63
1
Parent(s):
73874cd
chore: Optimize Dockerfile and add Nginx configuration for production deployment
Browse files- Dockerfile +16 -31
- nginx.conf +31 -0
- vite.config.ts +2 -1
Dockerfile
CHANGED
@@ -1,38 +1,23 @@
|
|
1 |
-
#
|
2 |
-
|
3 |
-
|
4 |
-
WORKDIR /usr/src/app
|
5 |
|
6 |
-
#
|
7 |
-
#
|
8 |
-
|
9 |
-
RUN
|
10 |
-
COPY package.json bun.lock /temp/dev/
|
11 |
-
RUN cd /temp/dev && bun install --frozen-lockfile
|
12 |
|
13 |
-
#
|
14 |
-
RUN mkdir -p /temp/prod
|
15 |
-
COPY package.json bun.lock /temp/prod/
|
16 |
-
RUN cd /temp/prod && bun install --frozen-lockfile --production
|
17 |
-
|
18 |
-
# copy node_modules from temp directory
|
19 |
-
# then copy all (non-ignored) project files into the image
|
20 |
-
FROM base AS prerelease
|
21 |
-
COPY --from=install /temp/dev/node_modules node_modules
|
22 |
COPY . .
|
23 |
-
|
24 |
-
# [optional] tests & build
|
25 |
ENV NODE_ENV=production
|
26 |
-
RUN bun test
|
27 |
RUN bun run build
|
28 |
|
29 |
-
#
|
30 |
-
FROM
|
31 |
-
|
32 |
-
COPY --from=
|
33 |
-
|
34 |
-
|
35 |
-
# run the app
|
36 |
-
USER bun
|
37 |
EXPOSE 7860
|
38 |
-
CMD
|
|
|
|
1 |
+
# Stage 1: Build the React/Vite application with Bun
|
2 |
+
FROM oven/bun:1 AS builder
|
3 |
+
WORKDIR /app
|
|
|
4 |
|
5 |
+
# Install dependencies into temp directory
|
6 |
+
# This will cache them and speed up future builds
|
7 |
+
COPY package.json bun.lock ./
|
8 |
+
RUN bun install --frozen-lockfile
|
|
|
|
|
9 |
|
10 |
+
# Copy all project files and build the application
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
COPY . .
|
|
|
|
|
12 |
ENV NODE_ENV=production
|
|
|
13 |
RUN bun run build
|
14 |
|
15 |
+
# Stage 2: Serve the application with Nginx
|
16 |
+
FROM nginx:alpine
|
17 |
+
# Copy the built assets from the builder stage
|
18 |
+
COPY --from=builder /app/dist /usr/share/nginx/html
|
19 |
+
# Use custom Nginx configuration
|
20 |
+
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
|
|
|
|
|
21 |
EXPOSE 7860
|
22 |
+
CMD ["nginx", "-g", "daemon off;"]
|
23 |
+
# docker build -t react-app . && docker run -p 7860:7860 react-app
|
nginx.conf
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
server {
|
2 |
+
listen 7860;
|
3 |
+
listen [::]:7860;
|
4 |
+
|
5 |
+
root /usr/share/nginx/html;
|
6 |
+
index index.html;
|
7 |
+
|
8 |
+
# Gzip compression
|
9 |
+
gzip on;
|
10 |
+
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
|
11 |
+
|
12 |
+
# Cache static assets
|
13 |
+
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
14 |
+
expires 30d;
|
15 |
+
add_header Cache-Control "public, max-age=2592000";
|
16 |
+
}
|
17 |
+
|
18 |
+
# Handle SPA routing - redirect all requests to index.html
|
19 |
+
location / {
|
20 |
+
try_files $uri $uri/ /index.html;
|
21 |
+
}
|
22 |
+
|
23 |
+
# Don't cache index.html
|
24 |
+
location = /index.html {
|
25 |
+
add_header Cache-Control "no-cache, no-store, must-revalidate";
|
26 |
+
expires 0;
|
27 |
+
}
|
28 |
+
|
29 |
+
# Handle 404 errors
|
30 |
+
error_page 404 /index.html;
|
31 |
+
}
|
vite.config.ts
CHANGED
@@ -14,7 +14,8 @@ export default defineConfig({
|
|
14 |
},
|
15 |
},
|
16 |
server: {
|
17 |
-
port: 7860
|
|
|
18 |
},
|
19 |
preview: {
|
20 |
port: 7860
|
|
|
14 |
},
|
15 |
},
|
16 |
server: {
|
17 |
+
port: 7860,
|
18 |
+
host: true,
|
19 |
},
|
20 |
preview: {
|
21 |
port: 7860
|