Skip to content

Commit c0ac5ec

Browse files
Refactored Dockerfile to Improve Efficiency for production (#3524)
* updated dockerfiles * Fixed unable to access api * Added sample prod nginx conf * update docs * Update eslintignore to include dockerfiles * Update docker pr workflow * Fixed typo * Fix PR workflow * Add default port * Added docker dir * Update docker pr workflow * Updated eslintignore * Added relative paths
1 parent 3104525 commit c0ac5ec

File tree

12 files changed

+264
-30
lines changed

12 files changed

+264
-30
lines changed

.eslintignore

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,13 @@ fix-repo-url.js
88
# Ignore the Docusaurus website subdirectory
99
docs/**
1010

11-
#Ignore markdown files from linting
11+
# Ignore markdown files from linting
1212
*.md
13+
14+
# Ignore dockerfile and related configs
15+
docker/docker-compose.prod.yaml
16+
docker/docker-compose.dev.yaml
17+
docker/Dockerfile.prod
18+
docker/Dockerfile.dev
19+
config/docker/setup/nginx.conf
20+
config/docker/setup/nginx.prod.conf

.github/workflows/pull-request.yml

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,12 @@ jobs:
117117
.prettierrc
118118
.prettierignore
119119
vite.config.ts
120-
docker-compose.yaml
121-
Dockerfile
120+
docker/docker-compose.prod.yaml
121+
docker/docker-compose.dev.yaml
122+
docker/Dockerfile.dev
123+
docker/Dockerfile.prod
124+
config/docker/setup/nginx.conf
125+
config/docker/setup/nginx.prod.conf
122126
CODEOWNERS
123127
LICENSE
124128
setup.ts
@@ -394,28 +398,50 @@ jobs:
394398
with:
395399
driver-opts: |
396400
image=moby/buildkit:latest
397-
- name: Build Docker image
401+
402+
- name: Build Docker images
398403
run: |
399404
set -e
400-
echo "Building Docker image..."
401-
docker build -t talawa-admin-app .
402-
echo "Docker image built successfully"
403-
- name: Run Docker Container
405+
echo "Building Docker images..."
406+
docker compose -f docker/docker-compose.prod.yaml build
407+
docker compose -f docker/docker-compose.dev.yaml build
408+
echo "Docker images built successfully"
409+
410+
- name: Run Docker Containers (Production)
404411
run: |
405412
set -e
406-
echo "Started Docker container..."
407-
docker run -d --name talawa-admin-app-container -p 4321:4321 talawa-admin-app
408-
echo "Docker container started successfully"
409-
- name: Check if Talawa Admin App is running
413+
echo "Starting Docker container for production..."
414+
docker compose -f docker/docker-compose.prod.yaml up -d
415+
echo "Production Docker container started successfully"
416+
417+
- name: Check if Talawa Admin App is running (Production)
410418
run: |
411419
chmod +x .github/workflows/scripts/app_health_check.sh
412420
.github/workflows/scripts/app_health_check.sh 4321 120 true
413-
- name: Stop Docker Container
421+
422+
- name: Stop prod Docker Containers
414423
if: always()
415424
run: |
416-
docker stop talawa-admin-app-container
417-
docker rm talawa-admin-app-container
418-
425+
docker compose -f docker/docker-compose.prod.yaml down
426+
echo "Prod Docker container stopped and removed"
427+
428+
- name: Run Docker Containers (Development)
429+
run: |
430+
set -e
431+
echo "Starting Docker container for development..."
432+
docker compose -f docker/docker-compose.dev.yaml up -d
433+
echo "Development Docker container started successfully"
434+
435+
- name: Check if Talawa Admin App is running (Development)
436+
run: |
437+
chmod +x .github/workflows/scripts/app_health_check.sh
438+
.github/workflows/scripts/app_health_check.sh 4321 120 true
439+
440+
- name: Stop dev Docker Containers
441+
if: always()
442+
run: |
443+
docker compose -f docker/docker-compose.dev.yaml down
444+
echo "Dev Docker containers stopped and removed"
419445
420446
Test-Docusaurus-Deployment:
421447
name: Test Deployment to https://docs-admin.talawa.io

.github/workflows/scripts/app_health_check.sh

100644100755
File mode changed.

config/docker/setup/nginx.conf

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
server {
2+
listen 80;
3+
server_name admin-demo.talawa.io;
4+
5+
root /usr/share/nginx/html;
6+
index index.html;
7+
8+
location / {
9+
try_files $uri /index.html;
10+
}
11+
12+
location /graphql {
13+
proxy_pass http://host.docker.internal:4000/graphql/;
14+
15+
# Enable CORS
16+
add_header Access-Control-Allow-Origin *;
17+
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
18+
add_header Access-Control-Allow-Headers "Content-Type, Authorization";
19+
20+
# Proxy headers
21+
proxy_http_version 1.1;
22+
proxy_set_header Upgrade $http_upgrade;
23+
proxy_set_header Connection "upgrade";
24+
proxy_set_header Host $host;
25+
proxy_set_header X-Real-IP $remote_addr;
26+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
27+
proxy_set_header X-Forwarded-Proto $scheme;
28+
proxy_cache_bypass $http_upgrade;
29+
}
30+
31+
# Gzip Compression
32+
gzip on;
33+
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
34+
gzip_min_length 256;
35+
gzip_vary on;
36+
37+
error_page 404 /index.html;
38+
}

config/docker/setup/nginx.prod.conf

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
server {
2+
listen 80;
3+
server_name admin-demo.talawa.io;
4+
5+
# Redirect HTTP to HTTPS
6+
return 301 https://$host$request_uri;
7+
}
8+
9+
server {
10+
listen 443 ssl http2;
11+
server_name admin-demo.talawa.io;
12+
13+
# SSL Configuration
14+
ssl_certificate /etc/ssl/certs/cert.pem;
15+
ssl_certificate_key /etc/ssl/private/key.pem;
16+
ssl_protocols TLSv1.2 TLSv1.3;
17+
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
18+
ssl_prefer_server_ciphers on;
19+
ssl_session_cache shared:SSL:10m;
20+
ssl_session_timeout 10m;
21+
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
22+
23+
root /usr/share/nginx/html;
24+
index index.html;
25+
26+
# Serve frontend app
27+
location / {
28+
try_files $uri /index.html;
29+
expires 1h;
30+
add_header Cache-Control "public, max-age=3600";
31+
}
32+
33+
# Reverse proxy to GraphQL API
34+
location /graphql {
35+
proxy_pass http://host.docker.internal:4000/graphql/;
36+
37+
# Security headers
38+
add_header Access-Control-Allow-Origin *;
39+
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
40+
add_header Access-Control-Allow-Headers "Content-Type, Authorization";
41+
add_header X-Frame-Options DENY;
42+
add_header X-Content-Type-Options nosniff;
43+
44+
# Proxy headers
45+
proxy_http_version 1.1;
46+
proxy_set_header Upgrade $http_upgrade;
47+
proxy_set_header Connection "upgrade";
48+
proxy_set_header Host $host;
49+
proxy_set_header X-Real-IP $remote_addr;
50+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
51+
proxy_set_header X-Forwarded-Proto $scheme;
52+
proxy_cache_bypass $http_upgrade;
53+
}
54+
55+
# Static file caching
56+
location ~* \.(?:ico|css|js|gif|jpe?g|png|woff2?|eot|ttf|svg|mp4|webm|ogg|mp3|wav|flac|aac)$ {
57+
expires 6M;
58+
add_header Cache-Control "public, max-age=15552000, immutable";
59+
}
60+
61+
# Brotli & Gzip compression
62+
gzip on;
63+
gzip_types text/plain text/css application/json application/javascript text/xml application/xml+rss text/javascript;
64+
gzip_min_length 256;
65+
gzip_vary on;
66+
67+
brotli on;
68+
brotli_types text/plain text/css application/json application/javascript text/xml application/xml+rss text/javascript image/svg+xml;
69+
brotli_comp_level 6;
70+
71+
# Error handling
72+
error_page 404 /index.html;
73+
error_page 500 502 503 504 /50x.html;
74+
75+
location = /50x.html {
76+
root /usr/share/nginx/html;
77+
}
78+
}

Dockerfile renamed to docker/Dockerfile.dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:20.10.0 AS build
1+
FROM node:22 AS build
22

33
WORKDIR /usr/src/app
44

docker/Dockerfile.prod

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Step 1: Build Stage
2+
FROM node:22 AS builder
3+
WORKDIR /talawa-admin
4+
5+
COPY package*.json ./
6+
RUN npm install
7+
8+
COPY . .
9+
10+
ENV NODE_ENV=production
11+
12+
RUN npm run build
13+
14+
#Step 2: Production
15+
FROM nginx:1.27.3-alpine AS production
16+
17+
ENV NODE_ENV=production
18+
19+
COPY config/docker/setup/nginx.conf /etc/nginx/conf.d/default.conf
20+
COPY --from=builder /talawa-admin/build /usr/share/nginx/html
21+
EXPOSE 80
22+
CMD ["nginx", "-g", "daemon off;"]

docker/docker-compose.dev.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
services:
2+
app:
3+
build:
4+
context: ..
5+
dockerfile: docker/Dockerfile.dev
6+
ports:
7+
- '${PORT:-4321}:4321'
8+
environment:
9+
- REACT_APP_TALAWA_URL=${REACT_APP_TALAWA_URL}
10+
- REACT_APP_BACKEND_WEBSOCKET_URL=${REACT_APP_BACKEND_WEBSOCKET_URL}
11+
- PORT=${PORT}
12+
- REACT_APP_USE_RECAPTCHA=${REACT_APP_USE_RECAPTCHA}
13+
- REACT_APP_RECAPTCHA_SITE_KEY=${REACT_APP_RECAPTCHA_SITE_KEY}
14+
volumes:
15+
- ..:/usr/src/app
16+
- /usr/src/app/node_modules
17+
healthcheck:
18+
test: ["CMD", "wget", "--spider", "-q", "http://localhost:4321"]
19+
interval: 30s
20+
timeout: 10s
21+
retries: 3
22+
restart: unless-stopped

docker/docker-compose.prod.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
services:
2+
app:
3+
build:
4+
context: ..
5+
dockerfile: docker/Dockerfile.prod
6+
ports:
7+
- '${PORT:-4321}:80'
8+
environment:
9+
- REACT_APP_TALAWA_URL=${REACT_APP_TALAWA_URL}
10+
- REACT_APP_BACKEND_WEBSOCKET_URL=${REACT_APP_BACKEND_WEBSOCKET_URL}
11+
- PORT=${PORT}
12+
- REACT_APP_USE_RECAPTCHA=${REACT_APP_USE_RECAPTCHA}
13+
- REACT_APP_RECAPTCHA_SITE_KEY=${REACT_APP_RECAPTCHA_SITE_KEY}
14+
healthcheck:
15+
test: ['CMD', 'curl', '-f', 'http://localhost:80']
16+
interval: 30s
17+
timeout: 10s
18+
retries: 3
19+
restart: unless-stopped

docs/docs/auto-docs/screens/MemberDetail/MemberDetail/functions/getLanguageName.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
> **getLanguageName**(`code`): `string`
88
9-
Defined in: [src/screens/MemberDetail/MemberDetail.tsx:742](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/screens/MemberDetail/MemberDetail.tsx#L742)
9+
Defined in: [src/screens/MemberDetail/MemberDetail.tsx:744](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/screens/MemberDetail/MemberDetail.tsx#L744)
1010

1111
## Parameters
1212

docs/docs/auto-docs/screens/MemberDetail/MemberDetail/functions/prettyDate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
> **prettyDate**(`param`): `string`
88
9-
Defined in: [src/screens/MemberDetail/MemberDetail.tsx:732](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/screens/MemberDetail/MemberDetail.tsx#L732)
9+
Defined in: [src/screens/MemberDetail/MemberDetail.tsx:734](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/screens/MemberDetail/MemberDetail.tsx#L734)
1010

1111
## Parameters
1212

docs/docs/docs/getting-started/installation.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,44 @@ You now need to setup the environment. This follows next.
140140
141141
If you prefer to use Docker, you can install the app using the following command:
142142
143-
1. Create a `.env` file as described in the Configuration section
143+
1. Create a `.env` file as described in the Configuration section
144144
145-
2. Build the Docker Image:
145+
2. Build and Run the Docker Image:
146146
147-
Run the following command to build the Docker image:
147+
Run the following command to run the Docker image:
148148
149-
```bash
150-
docker build -t talawa-admin .
151-
```
149+
```bash
150+
docker-compose -f docker/docker-compose.dev.yaml --env-file .env up
151+
```
152152
153-
3. Run the Docker container:
153+
3. To stop the container run the following command:
154154
155-
After the build is complete, run the Docker container using this command:
155+
```bash
156+
docker-compose -f docker/docker-compose.dev.yaml down
157+
```
156158
157-
```bash
158-
docker run -p 4321:4321 talawa-admin
159-
```
159+
The application will be accessible at `http://localhost:4321`
160+
161+
#### Production Setup
162+
163+
If you prefer to use Docker, you can install the app using the following command:
164+
165+
1. Create a `.env` file as described in the Configuration section
166+
167+
2. Configure `nginx.conf` file located at `config/docker/setup`. Modify it to fit your preferences before running the application.
168+
169+
3. Build and Run the Docker Image:
170+
171+
Run the following command to run the Docker image:
172+
173+
```bash
174+
docker-compose -f docker/docker-compose.prod.yaml --env-file .env up
175+
```
176+
4. To stop the container run the following command:
177+
178+
```bash
179+
docker-compose -f docker/docker-compose.prod.yaml down
180+
```
160181
161182
The application will be accessible at `http://localhost:4321`
162183

0 commit comments

Comments
 (0)