|
| 1 | +# Docker-in-Docker Build Setup for Kubernetes with Containerd |
| 2 | + |
| 3 | +## Overview |
| 4 | +This setup enables building Docker images within Kubernetes clusters that use containerd as the container runtime (instead of Docker). |
| 5 | + |
| 6 | +## How Deploy Builds Work |
| 7 | + |
| 8 | +1. **User triggers deployment** via Estela UI/API |
| 9 | +2. **Django API creates Kubernetes Job** using `BUILD_PROJECT_IMAGE` |
| 10 | +3. **Build pod starts** with privileged mode (required for Docker-in-Docker) |
| 11 | +4. **Entrypoint script runs:** |
| 12 | + - Starts Docker daemon inside container with VFS storage driver |
| 13 | + - Waits for daemon to be ready |
| 14 | + - Executes `build.py` |
| 15 | +5. **build.py process:** |
| 16 | + - Downloads project ZIP from S3 |
| 17 | + - Extracts project files |
| 18 | + - Builds Docker image using project's `Dockerfile-estela` |
| 19 | + - Attempts to detect spiders (gracefully fails if timeout) |
| 20 | + - Pushes built image to ECR |
| 21 | + - Updates deployment status |
| 22 | +6. **Pod completes** and is cleaned up automatically |
| 23 | + |
| 24 | +## Changes Made |
| 25 | + |
| 26 | +### 1. Dockerfile-build-project |
| 27 | +- Based on `python:3.9-slim` with Docker CE installed |
| 28 | +- Uses VFS storage driver for Kubernetes compatibility |
| 29 | +- Includes Docker-in-Docker capability with proper entrypoint |
| 30 | +- Verbose build logging for better debugging |
| 31 | + |
| 32 | +### 2. entrypoint-dind.sh |
| 33 | +- Starts Docker daemon inside the container |
| 34 | +- Waits for daemon to be ready before executing build.py |
| 35 | +- Provides proper error handling and logging |
| 36 | + |
| 37 | +### 3. kubernetes.py |
| 38 | +- Added privileged security context for build jobs (required for DinD) |
| 39 | +- Added resource limits (2-4GB memory, 1-2 CPU cores) |
| 40 | +- Build jobs now run with elevated privileges to allow Docker daemon |
| 41 | + |
| 42 | +### 4. core/views.py |
| 43 | +- Removed Docker socket volume mount (no longer needed) |
| 44 | +- Build containers are now self-contained with their own Docker daemon |
| 45 | + |
| 46 | +## Building the Image |
| 47 | + |
| 48 | +```bash |
| 49 | +# From the project root directory |
| 50 | +docker build -f docker-conf/Dockerfile-build-dind -t your-registry/estela-build-project:latest . |
| 51 | +docker push your-registry/estela-build-project:latest |
| 52 | +``` |
| 53 | + |
| 54 | +## Environment Variables |
| 55 | +Update your `BUILD_PROJECT_IMAGE` in settings to point to the new image: |
| 56 | +```python |
| 57 | +BUILD_PROJECT_IMAGE = "your-registry/estela-build-project:latest" |
| 58 | +``` |
| 59 | + |
| 60 | +## Kubernetes Requirements |
| 61 | + |
| 62 | +### Security Context |
| 63 | +The build pods require privileged mode to run Docker-in-Docker: |
| 64 | +```yaml |
| 65 | +securityContext: |
| 66 | + privileged: true |
| 67 | +``` |
| 68 | +
|
| 69 | +### Resource Requirements |
| 70 | +Recommended resources for build pods: |
| 71 | +- Memory: 2-4GB |
| 72 | +- CPU: 1-2 cores |
| 73 | +
|
| 74 | +## How It Works |
| 75 | +
|
| 76 | +1. When a deploy is triggered, the API creates a Kubernetes Job |
| 77 | +2. The Job runs the build container with privileged mode |
| 78 | +3. The entrypoint script starts Docker daemon inside the container |
| 79 | +4. build.py uses the internal Docker daemon to: |
| 80 | + - Build the project image |
| 81 | + - Push to ECR |
| 82 | +5. No dependency on host Docker socket or daemon |
| 83 | +
|
| 84 | +## Troubleshooting |
| 85 | +
|
| 86 | +### Docker daemon fails to start |
| 87 | +- Check pod logs: `kubectl logs <pod-name>` |
| 88 | +- Ensure the pod has privileged mode enabled |
| 89 | +- Verify sufficient resources are allocated |
| 90 | + |
| 91 | +### Build fails with permission errors |
| 92 | +- Ensure the Kubernetes namespace allows privileged pods |
| 93 | +- Check PodSecurityPolicy or PodSecurityStandards settings |
| 94 | + |
| 95 | +### Image push fails |
| 96 | +- Verify ECR credentials are correctly passed as environment variables |
| 97 | +- Check network connectivity from the pod to ECR |
| 98 | + |
| 99 | +## Security Considerations |
| 100 | + |
| 101 | +- Build pods run with privileged mode - ensure proper RBAC controls |
| 102 | +- Consider running build jobs in a dedicated namespace with restricted access |
| 103 | +- Monitor resource usage to prevent resource exhaustion |
0 commit comments