Skip to content
Open

add #11

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__pycache__
*.pyc
.env
venv
.git

31 changes: 31 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# ---------- Build Stage ----------
FROM python:3.11-slim AS builder

WORKDIR /app
RUN apt-get update && apt-get install -y build-essential

COPY requirements.txt .
RUN pip install --user -r requirements.txt


# ---------- Runtime Stage ----------
FROM python:3.11-slim

WORKDIR /app

# Create non-root user
RUN useradd -m appuser

COPY --from=builder /root/.local /home/appuser/.local
COPY . .

# 🔑 THIS IS THE FIX
RUN chown -R appuser:appuser /app

ENV PATH=/home/appuser/.local/bin:$PATH

USER appuser

EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

2 changes: 1 addition & 1 deletion backend/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["*"]


# Application definition
Expand Down
3 changes: 3 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Django==4.2.7
djangorestframework==3.14.0
django-cors-headers==4.3.1
28 changes: 28 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: "3.9"

services:
backend:
build: ./backend
container_name: django-backend
ports:
- "8000:8000"
environment:
DEBUG: "True"
networks:
- app-network

frontend:
build: ./frontend
container_name: react-frontend
ports:
- "3000:80"
environment:
VITE_API_URL: "http://backend:8000"
depends_on:
- backend
networks:
- app-network

networks:
app-network:

4 changes: 4 additions & 0 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
.git

25 changes: 25 additions & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ---------- Build Stage ----------
FROM node:20-alpine AS builder

WORKDIR /app
COPY package*.json ./
RUN npm install

COPY . .
RUN npm run build

# ---------- Runtime Stage ----------
FROM nginx:alpine

# Create non-root user
RUN adduser -D appuser \
&& mkdir -p /var/cache/nginx /var/run /run \
&& chown -R appuser:appuser /var/cache/nginx /var/run /run /etc/nginx /usr/share/nginx/html

COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf

USER appuser

EXPOSE 80

12 changes: 12 additions & 0 deletions frontend/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server {
listen 80;
server_name localhost;

root /usr/share/nginx/html;
index index.html;

location / {
try_files $uri /index.html;
}
}

2 changes: 1 addition & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function App() {
setLoading(true)
setError(null)
try {
const response = await axios.get('http://localhost:8000/api/hello/')
const response = await axios.get(`${import.meta.env.VITE_API_URL}/api/hello/`)
setMessage(response.data.message)
} catch (err) {
console.error(err)
Expand Down