|
| 1 | +# Docker Setup for DeepForest |
| 2 | + |
| 3 | +This guide explains how to use Docker to run DeepForest locally in a containerized environment. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- [Docker](https://docs.docker.com/get-docker/) installed on your system |
| 8 | +- [Docker Desktop](https://www.docker.com/products/docker-desktop/) (Windows/Mac) or Docker Engine (Linux) |
| 9 | +- For GPU support: [NVIDIA Docker runtime](https://github.com/NVIDIA/nvidia-docker) |
| 10 | + |
| 11 | +> [!NOTE] |
| 12 | +> This guide uses the modern `docker compose` command. If you are using an older version of Docker, you might need to use `docker-compose` (with a hyphen) instead. |
| 13 | +
|
| 14 | +## Quick Start |
| 15 | + |
| 16 | +### 1. Build the Docker Image |
| 17 | + |
| 18 | +```bash |
| 19 | +docker compose build |
| 20 | +``` |
| 21 | + |
| 22 | +This will create a Docker image with all DeepForest dependencies installed. |
| 23 | + |
| 24 | +### 2. Run the Container |
| 25 | + |
| 26 | +**Interactive Shell:** |
| 27 | +```bash |
| 28 | +docker compose run --rm deepforest |
| 29 | +``` |
| 30 | + |
| 31 | +This starts an interactive bash shell inside the container where you can run Python scripts and tests. |
| 32 | + |
| 33 | +**Run Specific Commands:** |
| 34 | +```bash |
| 35 | +# Test DeepForest installation |
| 36 | +docker compose run --rm deepforest python -c "from deepforest import main; model = main.deepforest(); print('DeepForest successfully installed!')" |
| 37 | + |
| 38 | +# Run tests |
| 39 | +docker compose run --rm deepforest pytest -v |
| 40 | + |
| 41 | +# Run a specific test file |
| 42 | +docker compose run --rm deepforest pytest tests/test_main.py -v |
| 43 | +``` |
| 44 | + |
| 45 | +### 3. Start Jupyter Notebook (Optional) |
| 46 | + |
| 47 | +```bash |
| 48 | +docker compose run --rm -p 8888:8888 deepforest jupyter notebook --ip=0.0.0.0 --no-browser --allow-root |
| 49 | +``` |
| 50 | + |
| 51 | +Then open the URL shown in the terminal (e.g., `http://127.0.0.1:8888/?token=...`) in your browser. |
| 52 | + |
| 53 | +## Development Workflow |
| 54 | + |
| 55 | +The project directory is mounted as a volume in the container, so any changes you make to the code on your host machine will be immediately reflected inside the container. |
| 56 | + |
| 57 | +### Typical Workflow: |
| 58 | + |
| 59 | +1. **Edit code** on your host machine using your favorite editor |
| 60 | +2. **Run tests** inside the container: |
| 61 | + ```bash |
| 62 | + docker compose run --rm deepforest pytest -v |
| 63 | + ``` |
| 64 | +3. **Run scripts** inside the container: |
| 65 | + ```bash |
| 66 | + docker compose run --rm deepforest python your_script.py |
| 67 | + ``` |
| 68 | + |
| 69 | +## GPU Support |
| 70 | + |
| 71 | +To use GPU acceleration with DeepForest in Docker: |
| 72 | + |
| 73 | +1. **Install NVIDIA Docker runtime** on your host machine: |
| 74 | + - Follow instructions at: https://github.com/NVIDIA/nvidia-docker |
| 75 | + |
| 76 | +2. **Uncomment GPU configuration** in `docker-compose.yml`: |
| 77 | + ```yaml |
| 78 | + deploy: |
| 79 | + resources: |
| 80 | + reservations: |
| 81 | + devices: |
| 82 | + - driver: nvidia |
| 83 | + count: all |
| 84 | + capabilities: [gpu] |
| 85 | + ``` |
| 86 | +
|
| 87 | +3. **Rebuild the image** (optional - use CUDA base image): |
| 88 | + - Modify the Dockerfile to use a CUDA-enabled base image like `nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu22.04` |
| 89 | + |
| 90 | +4. **Verify GPU access**: |
| 91 | + ```bash |
| 92 | + docker compose run --rm deepforest python -c "import torch; print(f'CUDA available: {torch.cuda.is_available()}')" |
| 93 | + ``` |
| 94 | + |
| 95 | +## Common Commands |
| 96 | + |
| 97 | +### Container Management |
| 98 | + |
| 99 | +```bash |
| 100 | +# Build/rebuild the image |
| 101 | +docker compose build |
| 102 | +
|
| 103 | +# Build without cache (fresh build) |
| 104 | +docker compose build --no-cache |
| 105 | +
|
| 106 | +# Start container in background |
| 107 | +docker compose up -d |
| 108 | +
|
| 109 | +# Stop running containers |
| 110 | +docker compose down |
| 111 | +
|
| 112 | +# View running containers |
| 113 | +docker ps |
| 114 | +
|
| 115 | +# View logs |
| 116 | +docker compose logs -f deepforest |
| 117 | +``` |
| 118 | + |
| 119 | +### Running Python Code |
| 120 | + |
| 121 | +```bash |
| 122 | +# Interactive Python shell |
| 123 | +docker compose run --rm deepforest python |
| 124 | +
|
| 125 | +# Run a Python script |
| 126 | +docker compose run --rm deepforest python scripts/your_script.py |
| 127 | +
|
| 128 | +# Run with custom arguments |
| 129 | +docker compose run --rm deepforest python -m deepforest.scripts.cli --help |
| 130 | +``` |
| 131 | + |
| 132 | +### Testing |
| 133 | + |
| 134 | +```bash |
| 135 | +# Run all tests |
| 136 | +docker compose run --rm deepforest pytest -v |
| 137 | +
|
| 138 | +# Run specific test file |
| 139 | +docker compose run --rm deepforest pytest tests/test_main.py -v |
| 140 | +
|
| 141 | +# Run tests with coverage |
| 142 | +docker compose run --rm deepforest pytest --cov=src/deepforest --cov-report=html |
| 143 | +
|
| 144 | +# Run specific test function |
| 145 | +docker compose run --rm deepforest pytest tests/test_main.py::test_function_name -v |
| 146 | +``` |
| 147 | + |
| 148 | +### Code Quality |
| 149 | + |
| 150 | +```bash |
| 151 | +# Run pre-commit checks |
| 152 | +docker compose run --rm deepforest pre-commit run --all-files |
| 153 | +
|
| 154 | +# Format code with ruff |
| 155 | +docker compose run --rm deepforest ruff format . |
| 156 | +
|
| 157 | +# Lint code |
| 158 | +docker compose run --rm deepforest ruff check . |
| 159 | +``` |
| 160 | + |
| 161 | +## Troubleshooting |
| 162 | + |
| 163 | +### 'docker' or 'docker-compose' command not found |
| 164 | + |
| 165 | +If you see errors like `The term 'docker' is not recognized` or `command not found`: |
| 166 | + |
| 167 | +1. **Verify Installation**: Ensure Docker Desktop (Windows/Mac) or Docker Engine (Linux) is installed. |
| 168 | +2. **Check PATH**: Ensure the Docker executable directory is in your system's PATH environment variable. |
| 169 | + * **Windows**: Usually `C:\Program Files\Docker\Docker\resources\bin`. |
| 170 | +3. **Restart Shell**: Close and reopen your terminal/powershell window after installing. |
| 171 | + |
| 172 | +### Permission Issues |
| 173 | + |
| 174 | +If you encounter permission errors when running the container: |
| 175 | + |
| 176 | +```bash |
| 177 | +# On Linux/Mac, ensure the container user has the same UID as your host user |
| 178 | +docker compose run --rm -u $(id -u):$(id -g) deepforest bash |
| 179 | +``` |
| 180 | + |
| 181 | +### Out of Memory |
| 182 | + |
| 183 | +If you run out of memory during build or execution: |
| 184 | + |
| 185 | +```bash |
| 186 | +# Increase Docker memory limit in Docker Desktop settings |
| 187 | +# Or use --memory flag |
| 188 | +docker compose run --rm --memory=8g deepforest pytest |
| 189 | +``` |
| 190 | + |
| 191 | +### Slow Build Times |
| 192 | + |
| 193 | +To speed up builds: |
| 194 | + |
| 195 | +1. Ensure `.dockerignore` is properly configured |
| 196 | +2. Use Docker BuildKit: |
| 197 | + ```bash |
| 198 | + DOCKER_BUILDKIT=1 docker compose build |
| 199 | + ``` |
| 200 | + |
| 201 | +### Container Won't Start |
| 202 | + |
| 203 | +```bash |
| 204 | +# Check logs |
| 205 | +docker compose logs deepforest |
| 206 | +
|
| 207 | +# Remove old containers and volumes |
| 208 | +docker compose down -v |
| 209 | +
|
| 210 | +# Rebuild from scratch |
| 211 | +docker compose build --no-cache |
| 212 | +``` |
| 213 | + |
| 214 | +## Data Persistence |
| 215 | + |
| 216 | +The `deepforest-data` volume is created to persist data between container runs. You can: |
| 217 | + |
| 218 | +```bash |
| 219 | +# List volumes |
| 220 | +docker volume ls |
| 221 | +
|
| 222 | +# Inspect volume |
| 223 | +docker volume inspect deepforest_deepforest-data |
| 224 | +
|
| 225 | +# Remove volume (WARNING: deletes all data) |
| 226 | +docker volume rm deepforest_deepforest-data |
| 227 | +``` |
| 228 | + |
| 229 | +## Advanced Usage |
| 230 | + |
| 231 | +### Custom Environment Variables |
| 232 | + |
| 233 | +Create a `.env` file in the project root: |
| 234 | + |
| 235 | +```env |
| 236 | +PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:512 |
| 237 | +PYTHONPATH=/home/deepforest/app/src |
| 238 | +``` |
| 239 | + |
| 240 | +### Running Multiple Containers |
| 241 | + |
| 242 | +```bash |
| 243 | +# Scale the service |
| 244 | +docker compose up -d --scale deepforest=3 |
| 245 | +``` |
| 246 | + |
| 247 | +### Accessing Container Shell |
| 248 | + |
| 249 | +```bash |
| 250 | +# Start container in background |
| 251 | +docker compose up -d |
| 252 | +
|
| 253 | +# Execute bash in running container |
| 254 | +docker compose exec deepforest bash |
| 255 | +
|
| 256 | +# Or use docker directly |
| 257 | +docker exec -it deepforest-dev bash |
| 258 | +``` |
| 259 | + |
| 260 | +## Cleaning Up |
| 261 | + |
| 262 | +```bash |
| 263 | +# Remove containers and networks |
| 264 | +docker compose down |
| 265 | +
|
| 266 | +# Remove containers, networks, and volumes |
| 267 | +docker compose down -v |
| 268 | +
|
| 269 | +# Remove images |
| 270 | +docker rmi deepforest:latest |
| 271 | +
|
| 272 | +# Clean up all Docker resources (use with caution) |
| 273 | +docker system prune -a |
| 274 | +``` |
| 275 | + |
| 276 | +## Additional Resources |
| 277 | + |
| 278 | +- [DeepForest Documentation](https://deepforest.readthedocs.io/) |
| 279 | +- [Docker Documentation](https://docs.docker.com/) |
| 280 | +- [Docker Compose Documentation](https://docs.docker.com/compose/) |
| 281 | +- [NVIDIA Docker](https://github.com/NVIDIA/nvidia-docker) |
0 commit comments