Skip to content

Commit c720a56

Browse files
committed
feat: Add Docker deployment guide and update docker-compose configuration
1 parent 5b0c66a commit c720a56

4 files changed

Lines changed: 212 additions & 127 deletions

File tree

.env.docker.example

Whitespace-only changes.

.github/workflows/release.yml

Lines changed: 34 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ permissions:
1515
contents: write
1616
pages: write
1717
id-token: write
18+
packages: write
1819

1920
jobs:
2021
create-release:
@@ -175,141 +176,47 @@ jobs:
175176
asset_name: deploy.sh
176177
asset_content_type: application/x-shellscript
177178

178-
- name: Generate Docker image (if Dockerfile exists)
179-
if: hashFiles('Dockerfile') != ''
180-
run: |
181-
docker build -t seccodesmith/backend:${{ steps.version.outputs.version_number }} .
182-
docker build -t seccodesmith/backend:latest .
183-
184-
# Save Docker image as artifact
185-
docker save seccodesmith/backend:${{ steps.version.outputs.version_number }} | gzip > dist/seccodesmith-backend-${{ steps.version.outputs.version_number }}-docker.tar.gz
186-
187-
- name: Upload Docker Image
188-
if: hashFiles('Dockerfile') != ''
189-
uses: actions/upload-release-asset@v1
190-
env:
191-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
192-
with:
193-
upload_url: ${{ steps.create_release.outputs.upload_url }}
194-
asset_path: ./dist/seccodesmith-backend-${{ steps.version.outputs.version_number }}-docker.tar.gz
195-
asset_name: seccodesmith-backend-${{ steps.version.outputs.version_number }}-docker.tar.gz
196-
asset_content_type: application/gzip
197-
198-
create-documentation:
179+
docker-release:
199180
runs-on: ubuntu-latest
200181
needs: create-release
201-
if: github.ref == 'refs/heads/main' || github.ref_type == 'tag'
182+
if: github.ref_type == 'tag' || github.event_name == 'workflow_dispatch'
202183

203184
steps:
204185
- name: Checkout code
205186
uses: actions/checkout@v4
206187

207-
- name: Set up Python
208-
uses: actions/setup-python@v4
188+
- name: Set up Docker Buildx
189+
uses: docker/setup-buildx-action@v3
190+
191+
- name: Log in to GitHub Container Registry
192+
uses: docker/login-action@v3
209193
with:
210-
python-version: 3.11.x
194+
registry: ghcr.io
195+
username: ${{ github.actor }}
196+
password: ${{ secrets.GITHUB_TOKEN }}
197+
198+
- name: Extract metadata
199+
id: meta
200+
uses: docker/metadata-action@v5
201+
with:
202+
images: |
203+
ghcr.io/${{ github.repository }}
204+
tags: |
205+
type=ref,event=tag
206+
type=raw,value=latest,enable={{is_default_branch}}
207+
type=semver,pattern={{version}}
208+
type=semver,pattern={{major}}.{{minor}}
209+
210+
- name: Build and push Docker image
211+
uses: docker/build-push-action@v5
212+
with:
213+
context: .
214+
platforms: linux/amd64,linux/arm64
215+
push: true
216+
tags: ${{ steps.meta.outputs.tags }}
217+
labels: ${{ steps.meta.outputs.labels }}
218+
cache-from: type=gha
219+
cache-to: type=gha,mode=max
211220

212-
- name: Install documentation dependencies
213-
run: |
214-
python -m pip install --upgrade pip
215-
pip install sphinx sphinx-rtd-theme
216221

217-
- name: Generate API documentation
218-
run: |
219-
# Create basic documentation structure
220-
mkdir -p docs/
221-
222-
cat > docs/index.html << 'EOF'
223-
<!DOCTYPE html>
224-
<html lang="en">
225-
<head>
226-
<meta charset="UTF-8">
227-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
228-
<title>SecCodeSmith Backend Documentation</title>
229-
<style>
230-
body { font-family: Arial, sans-serif; margin: 40px; line-height: 1.6; }
231-
.header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 8px; }
232-
.section { margin: 20px 0; }
233-
.code { background: #f4f4f4; padding: 10px; border-radius: 4px; font-family: monospace; }
234-
.endpoint { background: #e8f5e8; padding: 10px; margin: 5px 0; border-left: 4px solid #4CAF50; }
235-
</style>
236-
</head>
237-
<body>
238-
<div class="header">
239-
<h1>🔥 SecCodeSmith Backend API</h1>
240-
<p>Django-powered REST API for the SecCodeSmith portfolio website</p>
241-
</div>
242-
243-
<div class="section">
244-
<h2>📚 API Endpoints</h2>
245-
246-
<div class="endpoint">
247-
<strong>GET /api/csrf</strong><br>
248-
Retrieve CSRF token for secure form submissions
249-
</div>
250-
251-
<div class="endpoint">
252-
<strong>GET /api/skills-cards</strong><br>
253-
List skill cards for frontend display
254-
</div>
255-
256-
<div class="endpoint">
257-
<strong>GET /api/about/</strong><br>
258-
Get content for the About page
259-
</div>
260-
261-
<div class="endpoint">
262-
<strong>GET /api/footer-links</strong><br>
263-
List social and footer links
264-
</div>
265-
266-
<div class="endpoint">
267-
<strong>GET /api/contact/</strong><br>
268-
Get content for the Contact page
269-
</div>
270-
271-
<div class="endpoint">
272-
<strong>GET /blog-api/post/</strong><br>
273-
List all blog posts
274-
</div>
275-
276-
<div class="endpoint">
277-
<strong>GET /project-api/projects/</strong><br>
278-
List all projects
279-
</div>
280-
</div>
281-
282-
<div class="section">
283-
<h2>🚀 Quick Start</h2>
284-
<div class="code">
285-
# Clone the repository<br>
286-
git clone https://github.com/SecCodeSmith/SecCodeSmith-backend.git<br>
287-
cd SecCodeSmith-backend<br><br>
288-
289-
# Set up virtual environment<br>
290-
python -m venv .venv<br>
291-
source .venv/bin/activate # On Windows: .venv\Scripts\activate<br><br>
292-
293-
# Install dependencies<br>
294-
pip install -r requirements.txt<br><br>
295-
296-
# Run migrations and start server<br>
297-
python manage.py migrate<br>
298-
python manage.py runserver
299-
</div>
300-
</div>
301-
302-
<div class="section">
303-
<h2>📖 More Information</h2>
304-
<p>For detailed documentation, please refer to the <a href="https://github.com/SecCodeSmith/SecCodeSmith-backend">GitHub Repository</a>.</p>
305-
</div>
306-
</body>
307-
</html>
308-
EOF
309222

310-
- name: Deploy to GitHub Pages
311-
uses: peaceiris/actions-gh-pages@v3
312-
with:
313-
github_token: ${{ secrets.GITHUB_TOKEN }}
314-
publish_dir: ./docs
315-
destination_dir: docs

docker-compose.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
services:
2+
backend:
3+
build: .
4+
# For production, use the published image:
5+
# image: ghcr.io/seccodesmith/seccodesmith-backend:latest
6+
container_name: seccodesmith-backend
7+
hostname: backend
8+
networks:
9+
- redis-net
10+
- db
11+
ports:
12+
- "8000:8000"
13+
environment:
14+
- DJANGO_DEBUG=False
15+
- SECRET_KEY=${SECRET_KEY:-django-insecure-change-me-in-production}
16+
- ALLOWED_HOSTS=localhost,127.0.0.1,backend
17+
- DATABASE_TYPE=pgsql
18+
- DATABASE_NAME=${DATABASE_NAME}
19+
- DATABASE_USER=${DATABASE_USER}
20+
- DATABASE_PASSWORD=${DATABASE_PASSWORD}
21+
- DATABASE_HOST=postgres
22+
- DATABASE_PORT=5432
23+
- REDIS_HOST=redis
24+
- REDIS_PORT=6379
25+
- REDIS_PASSWORD=${REDIS_PASSWORD}
26+
depends_on:
27+
postgres:
28+
condition: service_healthy
29+
redis:
30+
condition: service_started
31+
healthcheck:
32+
test: ["CMD", "python", "manage.py", "check"]
33+
interval: 30s
34+
timeout: 10s
35+
retries: 3
36+
restart: unless-stopped
37+
238
redis:
339
image: redis:8.0.2-alpine
440
container_name: redis-Backend

docs/DOCKER_DEPLOYMENT.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Docker Deployment Guide
2+
3+
## 🐳 Container Registry Setup
4+
5+
This project automatically builds and pushes Docker containers to container registries when a release is created.
6+
7+
### Supported Registries
8+
9+
1. **GitHub Container Registry (ghcr.io)** - Automatic, no setup required
10+
2. **Docker Hub** - Requires setup (optional)
11+
12+
### GitHub Container Registry
13+
14+
The Docker image is automatically published to:
15+
```
16+
ghcr.io/seccodesmith/seccodesmith-backend:latest
17+
ghcr.io/seccodesmith/seccodesmith-backend:v0.2.0
18+
```
19+
20+
### Docker Hub Setup (Optional)
21+
22+
To enable Docker Hub publishing, add these secrets to your GitHub repository:
23+
24+
1. Go to your repository → Settings → Secrets and variables → Actions
25+
2. Add the following secrets:
26+
- `DOCKERHUB_USERNAME`: Your Docker Hub username
27+
- `DOCKERHUB_TOKEN`: Your Docker Hub access token
28+
29+
**Creating a Docker Hub Access Token:**
30+
1. Log in to Docker Hub
31+
2. Go to Account Settings → Security → Access Tokens
32+
3. Create a new access token with Read, Write, Delete permissions
33+
34+
## 🚀 Using the Container
35+
36+
### Pull and Run
37+
38+
```bash
39+
# From GitHub Container Registry
40+
docker pull ghcr.io/seccodesmith/seccodesmith-backend:latest
41+
docker run -p 8000:8000 ghcr.io/seccodesmith/seccodesmith-backend:latest
42+
43+
# From Docker Hub (if configured)
44+
docker pull seccodesmith/seccodesmith-backend:latest
45+
docker run -p 8000:8000 seccodesmith/seccodesmith-backend:latest
46+
```
47+
48+
### With Environment Variables
49+
50+
```bash
51+
docker run -p 8000:8000 \
52+
-e DJANGO_DEBUG=False \
53+
-e SECRET_KEY=your-secret-key \
54+
-e DATABASE_URL=postgres://user:pass@host:5432/db \
55+
ghcr.io/seccodesmith/seccodesmith-backend:latest
56+
```
57+
58+
### Using Docker Compose
59+
60+
```yaml
61+
version: '3.8'
62+
services:
63+
backend:
64+
image: ghcr.io/seccodesmith/seccodesmith-backend:latest
65+
ports:
66+
- "8000:8000"
67+
environment:
68+
- DJANGO_DEBUG=False
69+
- SECRET_KEY=${SECRET_KEY}
70+
- DATABASE_URL=${DATABASE_URL}
71+
depends_on:
72+
- postgres
73+
- redis
74+
75+
postgres:
76+
image: postgres:15
77+
environment:
78+
POSTGRES_DB: backend
79+
POSTGRES_USER: postgres
80+
POSTGRES_PASSWORD: postgres
81+
volumes:
82+
- postgres_data:/var/lib/postgresql/data
83+
84+
redis:
85+
image: redis:7-alpine
86+
87+
volumes:
88+
postgres_data:
89+
```
90+
91+
## 🔧 Development
92+
93+
### Building Locally
94+
95+
```bash
96+
# Build the image
97+
docker build -t seccodesmith-backend .
98+
99+
# Run locally
100+
docker run -p 8000:8000 seccodesmith-backend
101+
```
102+
103+
### Multi-architecture Build
104+
105+
The release workflow builds for both AMD64 and ARM64 architectures:
106+
107+
```bash
108+
docker buildx build --platform linux/amd64,linux/arm64 -t seccodesmith-backend .
109+
```
110+
111+
## 📋 Container Features
112+
113+
- **Base Image**: Alpine Linux 3.22 (lightweight and secure)
114+
- **Python**: 3.x with virtual environment
115+
- **Web Server**: Gunicorn with optimized configuration
116+
- **Database Support**: PostgreSQL client included
117+
- **Security**: Non-root user, minimal attack surface
118+
- **Caching**: Docker layer caching enabled in CI/CD
119+
120+
## 🛡️ Security
121+
122+
- Container runs as non-root user
123+
- Minimal base image (Alpine Linux)
124+
- Security scanning included in CI/CD pipeline
125+
- Regular base image updates
126+
- Secrets managed through environment variables
127+
128+
## 📦 Release Process
129+
130+
1. Create a new tag: `git tag -a v1.0.0 -m "Release v1.0.0"`
131+
2. Push the tag: `git push origin v1.0.0`
132+
3. GitHub Actions automatically:
133+
- Builds multi-architecture containers
134+
- Pushes to configured registries
135+
- Updates the release with container information
136+
137+
## 🔍 Monitoring
138+
139+
Container health can be monitored through:
140+
- Django health check endpoint: `/admin/`
141+
- Container logs: `docker logs <container_id>`
142+
- Resource usage: `docker stats <container_id>`

0 commit comments

Comments
 (0)