Skip to content

Commit

Permalink
add docker
Browse files Browse the repository at this point in the history
  • Loading branch information
2pipopolam committed Jan 12, 2025
1 parent 7d11eaa commit a11d952
Show file tree
Hide file tree
Showing 13 changed files with 586 additions and 379 deletions.
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.git
.gitignore
.env
*.pyc
__pycache__
.DS_Store
node_modules
frontend/node_modules
frontend/build
.dockerignore
Dockerfile
docker-compose.yml
README.md
42 changes: 42 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM python:3.11-slim

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV NODE_VERSION 20.x
ENV DEBIAN_FRONTEND=noninteractive

WORKDIR /app

RUN apt-get update && apt-get install -y \
gcc \
postgresql-client \
libpq-dev \
curl \
gnupg \
bash \
git \
&& curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION} | bash - \
&& apt-get install -y nodejs \
&& npm install -g npm@latest \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

RUN mkdir -p /app/media /app/static /app/frontend \
&& chmod -R 755 /app/media /app/static

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

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

WORKDIR /app

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh && \
sed -i 's/\r$//g' /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
67 changes: 67 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
version: '3.8'

services:
db:
image: postgres:15
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=${DB_NAME:-pampdb}
- POSTGRES_USER=${DB_USER:-postgres}
- POSTGRES_PASSWORD=${DB_PASSWORD:-123456}
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-pampdb}"]
interval: 5s
timeout: 5s
retries: 5
ports:
- "5432:5432"

web:
build: .
volumes:
- .:/app
- static_volume:/app/static
- media_volume:/app/media
- frontend_build:/app/frontend/build
ports:
- "8000:8000"
- "3000:3000"
environment:
- DEBUG=${DEBUG:-1}
- SECRET_KEY=${SECRET_KEY:-your-secret-key-123}
- DJANGO_ALLOWED_HOSTS=${DJANGO_ALLOWED_HOSTS:-localhost 127.0.0.1 [::1]}
- DB_NAME=${DB_NAME:-pampdb}
- DB_USER=${DB_USER:-postgres}
- DB_PASSWORD=${DB_PASSWORD:-123456}
- DB_HOST=${DB_HOST:-db}
- DB_PORT=${DB_PORT:-5432}
- GOOGLE_OAUTH2_KEY=${GOOGLE_OAUTH2_KEY}
- GOOGLE_OAUTH2_SECRET=${GOOGLE_OAUTH2_SECRET}
- REACT_APP_API_URL=http://localhost:8000/api
- NODE_ENV=development
- STATIC_ROOT=/app/static
- MEDIA_ROOT=/app/media
depends_on:
db:
condition: service_healthy
restart: unless-stopped

nginx:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- static_volume:/app/static
- media_volume:/app/media
- frontend_build:/app/frontend/build
depends_on:
- web
restart: unless-stopped

volumes:
postgres_data:
static_volume:
media_volume:
frontend_build:
31 changes: 31 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
set -e

echo "Checking installed Python packages..."
pip freeze

echo "Waiting for PostgreSQL..."
while ! pg_isready -h $DB_HOST -p $DB_PORT -U $DB_USER; do
echo "PostgreSQL is unavailable - sleeping"
sleep 1
done
echo "PostgreSQL is up and running!"

echo "Building React application..."
cd frontend
npm install
npm run build
cd ..

echo "Running migrations..."
python manage.py migrate

echo "Collecting static files..."
python manage.py collectstatic --noinput

echo "Starting Django server..."
python manage.py runserver 0.0.0.0:8000 &

echo "Starting React development server..."
cd frontend
npm start
Loading

0 comments on commit a11d952

Please sign in to comment.