Skip to content

Commit 4880f2d

Browse files
committed
convert dockerfile to three stage to shrink image size, and add serve static content via nginx
1 parent e2aefa4 commit 4880f2d

File tree

6 files changed

+126
-12
lines changed

6 files changed

+126
-12
lines changed

Dockerfile

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
1-
FROM node:19-alpine
1+
# Stage 1: Build
2+
FROM node:19-alpine as build
23

3-
#python: required dependency for node alpine, shrinks image size from 2.17GB to 1.67GB
4+
# python: required dependency for node alpine, shrinks image size from 2.17GB to 1.67GB
45
RUN apk add --no-cache --virtual .gyp \
56
python3 \
67
make \
7-
g++
8+
g++
89

910
WORKDIR /app
1011

1112
COPY package*.json ./
1213

13-
COPY app/dist /app
14+
RUN npm install --no-install-recommends
1415

15-
COPY .env .env
16+
COPY . .
1617

17-
COPY server ./server
18+
# Stage 2: Node Server
19+
FROM node:19-alpine as node_server
1820

19-
COPY config.js ./config.js
21+
WORKDIR /app
2022

21-
RUN npm install --no-install-recommends
23+
COPY --from=build /app/package*.json ./
24+
25+
RUN npm install --no-install-recommends --only=production
26+
27+
COPY --from=build /app/.env .env
28+
COPY --from=build /app/config.js ./config.js
29+
COPY --from=build /app/server ./server
2230

2331
EXPOSE 5656
2432

2533
ENV IS_DOCKER true
2634

2735
CMD [ "npm", "start" ]
2836

29-
# create docker image: "docker build -t reactype-container ." Note: if you want reactype-container with a different name (must be all lowercase)
30-
# run docker container: "docker run -d -p 5656:5656 reactype-container" docker run -d -p 5656:5656 reactype-v15
31-
# comment out window.api.formatCode(), but will break it for electron app.
37+
# Stage 3: Nginx Server
38+
FROM nginx:alpine as nginx_server
39+
40+
# Copy the build output to the Nginx html directory
41+
COPY --from=build /app/app/dist /usr/share/nginx/html
42+
43+
# Copy the Nginx configuration file
44+
COPY nginx.conf /etc/nginx/conf.d/default.conf
45+
46+
EXPOSE 80
47+
48+
CMD ["nginx", "-g", "daemon off;"]

Dockerfile-1stage

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM node:19-alpine
2+
3+
#python: required dependency for node alpine, shrinks image size from 2.17GB to 1.67GB
4+
RUN apk add --no-cache --virtual .gyp \
5+
python3 \
6+
make \
7+
g++
8+
9+
WORKDIR /app
10+
11+
COPY package*.json ./
12+
13+
COPY app/dist /app
14+
15+
COPY .env .env
16+
17+
COPY server ./server
18+
19+
COPY config.js ./config.js
20+
21+
RUN npm install --no-install-recommends
22+
23+
EXPOSE 5656
24+
25+
ENV IS_DOCKER true
26+
27+
CMD [ "npm", "start" ]
28+
29+
# create docker image: "docker build -t reactype-container ." Note: if you want reactype-container with a different name (must be all lowercase)
30+
# run docker container: "docker run -d -p 5656:5656 reactype-container" docker run -d -p 5656:5656 reactype-v15
31+
# comment out window.api.formatCode(), but will break it for electron app.

Dockerfile-2stage

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Stage 1: Build
2+
FROM node:19-alpine as build
3+
4+
# python: required dependency for node alpine, shrinks image size from 2.17GB to 1.67GB
5+
RUN apk add --no-cache --virtual .gyp \
6+
python3 \
7+
make \
8+
g++
9+
10+
WORKDIR /app
11+
12+
COPY package*.json ./
13+
14+
RUN npm install --no-install-recommends
15+
16+
COPY . .
17+
18+
# Stage 2: Runtime
19+
FROM node:19-alpine as runtime
20+
21+
WORKDIR /app
22+
23+
COPY --from=build /app/package*.json ./
24+
25+
RUN npm install --no-install-recommends --only=production
26+
27+
COPY --from=build /app/.env .env
28+
COPY --from=build /app/config.js ./config.js
29+
COPY --from=build /app/server ./server
30+
COPY --from=build /app/app/dist /app
31+
32+
EXPOSE 5656
33+
34+
ENV IS_DOCKER true
35+
36+
CMD [ "npm", "start" ]

app/src/components/top/NavBarButtons.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function navbarDropDown () {
132132
dispatch(toggleDarkMode());
133133
// Add your logic to update the style and theme based on darkMode
134134
isDarkMode ? dispatch(setStyle(null)) : dispatch(setStyle({ backgroundColor: '#21262c' }));
135-
props.setTheme(isDarkMode);
135+
// props.setTheme(isDarkMode);
136136
};
137137

138138
const handleClose = () => {

docker-compose.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: '3'
2+
3+
services:
4+
web:
5+
build:
6+
context: .
7+
target: nginx_server
8+
ports:
9+
- '80:80'
10+
api:
11+
build:
12+
context: .
13+
target: node_server
14+
ports:
15+
- '5656:5656'

nginx.conf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
server {
2+
listen 80;
3+
server_name localhost;
4+
5+
location / {
6+
root /usr/share/nginx/html;
7+
index index-prod.html index.htm;
8+
try_files $uri $uri/ /index-prod.html;
9+
}
10+
11+
error_page 500 502 503 504 /50x.html;
12+
location = /50x.html {
13+
root /usr/share/nginx/html;
14+
}
15+
}

0 commit comments

Comments
 (0)