Skip to content

Commit 87b78f6

Browse files
matrixiseclaude
andcommitted
feat: add Docker branch isolation for parallel development
Implement automatic environment isolation based on git branch names, allowing developers to work on multiple branches simultaneously without conflicts between databases, containers, or Docker volumes. Key changes: - Auto-detect git branch and normalize name (replace special chars) - Tag Docker images by branch (python.ie/website:{branch}) - Isolate PostgreSQL databases per branch (pythonie_{branch}) - Create separate Docker volumes per branch - Add configurable ports for parallel development (WEB_PORT, PG_PORT) - Upgrade Redis from 6.2 to 7-alpine with persistence - Add healthchecks for PostgreSQL and Redis - Remove unused minio and mc services New Taskfile commands: - task branch:info - Show current branch environment - task branch:volumes - List all Docker volumes by branch - task branch:clean - Remove volumes for current branch - task env:write - Generate .env file for docker-compose Documentation: - Add DOCKER-BRANCHES.md with usage examples and troubleshooting - Add .env.example template for custom port overrides - Update .gitignore to exclude generated .env file This enables developers to switch branches or run multiple instances in parallel without losing data or experiencing port conflicts. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent cc27c4d commit 87b78f6

5 files changed

Lines changed: 344 additions & 39 deletions

File tree

.env.example

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Docker Compose Environment Variables
2+
# Copy this file to .env and customize as needed
3+
4+
# Override ports for parallel development (optional)
5+
# Useful when running multiple branches simultaneously
6+
# WEB_PORT=8001
7+
# PG_PORT=5433
8+
9+
# Override git branch detection (optional - normally auto-detected)
10+
# GIT_BRANCH=my-custom-branch
11+
12+
# Override Docker image name (optional)
13+
# DOCKER_IMAGE=python.ie/website:custom-tag
14+
15+
# Override database name (optional)
16+
# PGDATABASE=pythonie_custom

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ project.db
6666

6767
# Environment files with sensitive credentials
6868
.envrc
69+
.env
6970
development.env
7071
production.env
7172

DOCKER-BRANCHES.md

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
# Docker Multi-Branch Development
2+
3+
This setup allows you to run isolated Docker environments for each git branch, preventing conflicts when switching branches or working on multiple features simultaneously.
4+
5+
## How It Works
6+
7+
### Automatic Branch Detection
8+
9+
The system automatically:
10+
- Detects your current git branch (`git rev-parse --abbrev-ref HEAD`)
11+
- Normalizes the branch name (e.g., `feature/add-auth``feature-add-auth`)
12+
- Uses this normalized name for:
13+
- Docker image tags: `python.ie/website:feature-add-auth`
14+
- Database names: `pythonie_feature-add-auth`
15+
- Container names: `pythonie-web-feature-add-auth`
16+
- Docker volumes: `postgres-data-feature-add-auth`
17+
18+
### Branch Isolation
19+
20+
Each branch gets:
21+
- ✅ Its own Docker image
22+
- ✅ Its own PostgreSQL database (isolated data)
23+
- ✅ Its own Redis instance (isolated cache)
24+
- ✅ Its own Docker volumes (persistent storage)
25+
- ✅ Unique container names (no conflicts)
26+
27+
## Usage
28+
29+
### Basic Commands
30+
31+
```bash
32+
# Check your current branch environment
33+
task branch:info
34+
35+
# Build image for current branch
36+
task docker:build
37+
38+
# Run development server (uses current branch)
39+
task run
40+
41+
# Run tests (uses current branch database)
42+
task tests
43+
44+
# Django shell for current branch
45+
task django:shell-plus
46+
```
47+
48+
### Working on Multiple Branches
49+
50+
**Scenario**: You want to work on `feature/auth` while keeping `main` running.
51+
52+
```bash
53+
# Terminal 1: Main branch
54+
git checkout main
55+
task run
56+
# → Uses: python.ie/website:main, pythonie_main database, port 8000
57+
58+
# Terminal 2: Feature branch (in another terminal)
59+
git checkout feature/auth
60+
task run
61+
# → ERROR: Port 8000 already in use!
62+
```
63+
64+
**Solution**: Use custom ports for parallel instances:
65+
66+
```bash
67+
# Terminal 1: Main branch
68+
git checkout main
69+
task run
70+
71+
# Terminal 2: Feature branch with custom port
72+
git checkout feature/auth
73+
WEB_PORT=8001 PG_PORT=5433 task run
74+
# → Uses: port 8001, separate database
75+
```
76+
77+
### Managing Volumes
78+
79+
```bash
80+
# List all volumes (all branches)
81+
task branch:volumes
82+
83+
# Example output:
84+
# pythonie-postgres-data-master
85+
# pythonie-postgres-data-feature-add-auth
86+
# pythonie-redis-data-master
87+
# pythonie-redis-data-feature-add-auth
88+
89+
# Clean volumes for current branch (DESTRUCTIVE!)
90+
task branch:clean
91+
# → Deletes all data for current branch
92+
```
93+
94+
### Switching Branches
95+
96+
When you switch branches, the system automatically uses the correct environment:
97+
98+
```bash
99+
# On main branch
100+
git checkout main
101+
task run # Uses main database
102+
103+
# Switch to feature branch
104+
git checkout feature/add-auth
105+
task run # Automatically uses feature-add-auth database
106+
```
107+
108+
**Important**: You must rebuild the image after switching branches if dependencies changed:
109+
110+
```bash
111+
git checkout feature/new-dependencies
112+
task docker:build # Rebuild image with new dependencies
113+
task run
114+
```
115+
116+
## Advanced: Custom Ports for Parallel Development
117+
118+
Create a `.env` file (git-ignored) to override default ports:
119+
120+
```bash
121+
# .env
122+
WEB_PORT=8001
123+
PG_PORT=5433
124+
```
125+
126+
Or pass them inline:
127+
128+
```bash
129+
WEB_PORT=8001 PG_PORT=5433 task run
130+
```
131+
132+
## Troubleshooting
133+
134+
### Port Already in Use
135+
136+
```
137+
Error: port 8000 already in use
138+
```
139+
140+
**Solution**: Stop other instances or use custom ports:
141+
```bash
142+
task down # Stop all containers for current branch
143+
# OR
144+
WEB_PORT=8001 PG_PORT=5433 task run
145+
```
146+
147+
### Database Not Found
148+
149+
```
150+
FATAL: database "pythonie_feature-xyz" does not exist
151+
```
152+
153+
**Solution**: Run migrations to create the database:
154+
```bash
155+
task django:migrate
156+
```
157+
158+
### Wrong Database Being Used
159+
160+
```bash
161+
# Check which environment is active
162+
task branch:info
163+
164+
# Ensure you're on the correct git branch
165+
git branch
166+
167+
# Rebuild if needed
168+
task docker:build
169+
```
170+
171+
### Cleaning Up Old Branches
172+
173+
After deleting a git branch, clean up its Docker resources:
174+
175+
```bash
176+
# Remove unused images
177+
docker image prune -a
178+
179+
# Remove volumes for deleted branches
180+
# (replace 'old-branch-name' with actual normalized branch name)
181+
docker volume rm pythonie-postgres-data-old-branch-name
182+
docker volume rm pythonie-redis-data-old-branch-name
183+
184+
# Or use the helper (must be on that branch)
185+
git checkout old-branch-name
186+
task branch:clean
187+
```
188+
189+
## Environment Variables Reference
190+
191+
| Variable | Default | Description |
192+
|----------|---------|-------------|
193+
| `GIT_BRANCH` | Auto-detected | Current git branch (normalized) |
194+
| `DOCKER_IMAGE` | `python.ie/website:{branch}` | Docker image name |
195+
| `PGDATABASE` | `pythonie_{branch}` | PostgreSQL database name |
196+
| `WEB_PORT` | `8000` | Web server port |
197+
| `PG_PORT` | `5432` | PostgreSQL port |
198+
199+
## Tips
200+
201+
1. **Branch Naming**: Use descriptive branch names. They'll appear in container names and logs.
202+
203+
2. **Disk Space**: Each branch creates separate volumes. Monitor disk usage:
204+
```bash
205+
docker system df
206+
```
207+
208+
3. **Production**: This system is for **development only**. Production uses fixed names and ports.
209+
210+
4. **Database Migrations**: Each branch has independent migrations. Remember to run `task django:migrate` after switching branches with schema changes.
211+
212+
5. **Shared Data**: If you need to copy data between branches:
213+
```bash
214+
# Export from main
215+
git checkout main
216+
task run:postgres # Start just postgres
217+
pg_dump -U postgres pythonie_main > /tmp/main.sql
218+
219+
# Import to feature branch
220+
git checkout feature/xyz
221+
task run:postgres
222+
psql -U postgres pythonie_feature-xyz < /tmp/main.sql
223+
```

0 commit comments

Comments
 (0)