Skip to content

Commit

Permalink
fix docker dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mrzaizai2k committed Nov 18, 2024
1 parent 3d571f7 commit 8b17d15
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 12 deletions.
10 changes: 7 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ services:
environment:
- REACT_APP_SERVER_IP=${SERVER_IP}


nginx:
build:
context: ./nginx
container_name: nginx
volumes:
- ./nginx/nginx.conf.template:/etc/nginx/nginx.conf.template:ro
# Remove the dynamic path in volume mounting
- ./nginx/dev/nginx.conf.template:/etc/nginx/dev/nginx.conf.template:ro
- ./nginx/prod/nginx.conf.template:/etc/nginx/prod/nginx.conf.template:ro
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
ports:
Expand All @@ -97,9 +98,12 @@ services:
environment:
- CLIENT_MAX_BODY_SIZE=${CLIENT_MAX_BODY_SIZE}
- SERVER_IP=${SERVER_IP}
- DOMAIN_NAME=${DOMAIN_NAME}
- DEBUG=${DEBUG:-false}
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"




certbot:
image: certbot/certbot
container_name: certbot
Expand Down
11 changes: 10 additions & 1 deletion docs/set_up_EC2.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This document provides step-by-step instructions to set up the Multilanguage Inv
- [4. Optional: Add user to Docker group](#4-optional-add-your-user-to-the-docker-group)
- [5. Verify Docker Installation](#5-verify-docker-installation)
- [6. Reboot the EC2 instance](#6-reboot-the-ec2-instance-optional)
- [7. Using bash scripts (optional)](#7-using-bash-scripts-optional)
3. [Clone Code, Setup .env File, and Run](#clone-code-setup-env-file-and-run)
4. [SSH to EC2 instance](#ssh-to-ec2-instance)

Expand Down Expand Up @@ -122,6 +123,13 @@ If necessary, reboot your EC2 instance:
sudo reboot
```
### 7. Using bash scripts (optional)
You can do all the commands above by running bash script
```bash
chmod +x ./scripts/setup-docker.sh
./scripts/setup-docker.sh
```
---
## Clone Code, Setup .env File, and Run
Expand Down Expand Up @@ -149,7 +157,8 @@ SECRET_KEY=
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
CLIENT_MAX_BODY_SIZE=5M
SERVER_IP=
SERVER_IP= # Can be public IP or domain name like xyz.com
DEBUG=True # Debuf True will use nginx config in dev folder, True/False
```
#### Explanation:
Expand Down
7 changes: 7 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
FROM nginx:latest

# Create directories for both configurations
RUN mkdir -p /etc/nginx/dev /etc/nginx/prod

# Copy configuration files for both environments
COPY dev/nginx.conf.template /etc/nginx/dev/
COPY prod/nginx.conf.template /etc/nginx/prod/

# Copy the startup script into the container
COPY start-nginx.sh /start-nginx.sh

Expand Down
37 changes: 37 additions & 0 deletions nginx/dev/nginx.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
events {
worker_connections 1024;
}

http {
# Placeholder for max file transfer size
client_max_body_size ${CLIENT_MAX_BODY_SIZE};

server {
listen 80;
server_name localhost ${SERVER_IP}; # Handles both localhost and server IP

location / {
proxy_pass http://jwt-frontend:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location /api/ {
proxy_pass http://fastapi-app:8149/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300s;
proxy_connect_timeout 75s;
}

location /mongo/ {
proxy_pass http://mongodb:27017/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
14 changes: 7 additions & 7 deletions nginx/nginx.conf.template → nginx/prod/nginx.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ http {
# HTTP server (redirect to HTTPS)
server {
listen 80;
server_name mrzaizai2k.xyz;
server_name ${SERVER_IP};

# Certbot challenges
location /.well-known/acme-challenge/ {
Expand All @@ -29,19 +29,19 @@ http {

# Redirect all HTTP traffic to HTTPS
location / {
return 301 https://$host$request_uri;
return 301 https://${SERVER_IP}$request_uri;
}
}

# HTTPS server
server {
listen 443 ssl;
server_name mrzaizai2k.xyz;
server_name ${SERVER_IP};

# SSL configuration
ssl_certificate /etc/letsencrypt/live/mrzaizai2k.xyz/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mrzaizai2k.xyz/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/mrzaizai2k.xyz/chain.pem;
ssl_certificate /etc/letsencrypt/live/${SERVER_IP}/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/${SERVER_IP}/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/${SERVER_IP}/chain.pem;

# Frontend
location / {
Expand Down Expand Up @@ -70,4 +70,4 @@ http {
proxy_set_header X-Real-IP $remote_addr;
}
}
}
}
9 changes: 8 additions & 1 deletion nginx/start-nginx.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#!/bin/bash

# Determine which config to use based on DEBUG value
if [ "$DEBUG" = "True" ]; then
CONFIG_PATH="/etc/nginx/dev/nginx.conf.template"
else
CONFIG_PATH="/etc/nginx/prod/nginx.conf.template"
fi

# Substitute environment variables in the nginx template
envsubst '${CLIENT_MAX_BODY_SIZE} ${DOMAIN_NAME}' < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf
envsubst '${CLIENT_MAX_BODY_SIZE} ${SERVER_IP}' < $CONFIG_PATH > /etc/nginx/nginx.conf

# Start NGINX
exec nginx -g 'daemon off;'

0 comments on commit 8b17d15

Please sign in to comment.