File size: 8,424 Bytes
9c639a1 de71074 9c639a1 de71074 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# Lessons Learned
## Table of Contents
1. [Docker](#1-docker)
2. [Dev Containers](#2-dev-containers)
3. [Redis](#3-redis)
4. [Postgres](#4-postgres)
5. [Unit Tests](#5-unit-tests)
6. [Locust](#6-locust)
7. [AWS](#7-aws)
8. [GitHub Actions](#8-github-actions)
9. [Streamlit](#9-streamlit)
## 1. Docker
- Install [Docker](https://docs.docker.com/engine/install/)
- Some basic Docker commands:
```bash
# List all containers
docker ps
# List all images
docker images
# Remove resources
docker rmi $(docker images -q)
docker rm $(docker ps -aq)
# Network
docker network ls
docker network rm <network_id>
# Volumes
docker volume ls
docker volume rm <volume_id>
# Build an image
docker build -t <image_name> .
# Build a test image (target)
docker build -t ui_test --progress=plain --target test .
# Run a container
docker run -p <port>:<port> -d <image_name>
```
### 1.1 Dockerfile
- A basic example of a Dockerfile
```Dockerfile
# Base image
FROM python:3.9
# Set working directory
WORKDIR /app
# Copy files
COPY . /app
# Install dependencies (during build time)
RUN pip install -r requirements.txt
# Run the app (after installing dependencies)
CMD ["python", "main.py"]
```
### 1.2 Docker Compose
- A basic example of a Docker Compose file running microservices:
```yml
services:
api: # Name of the service
build: # Build the image
context: ./ui
target: build # Target
ports: # Ports
- "8000:8000"
environment: # Env variables
POSTGRES_DB: $POSTGRES_DB
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
DATABASE_HOST: $DATABASE_HOST
depends_on: # Dependencies
- redis
- db
networks: # Network
- shared_network
volumes: # Volumes
- ./uploads:/src/uploads
db: # Another service
image: postgres:13-alpine
volumes: # Volumes
- postgres_data:/var/lib/postgresql/data
redis: # Another service
image: redis:6.2.6
networks:
- shared_network
networks:
shared_network:
volumes:
postgres_data:
```
## 2. Dev Containers
- Install [Dev Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension
- Create a `.devcontainer` folder
- Create folders for each service you want to develop (api, model, ui, etc.)
- Create a `devcontainer.json` file
- Optionally you can create Dockerfiles or Docker Compose files for each service
```json
{
"name": "ML Project - API",
"dockerComposeFile": "../../docker-compose-dev.yml",
"service": "api",
"workspaceFolder": "/src",
"customizations": {
"vscode": {
"extensions": ["ms-python.python"]
}
},
"shutdownAction": "none"
}
```
- Note: Don't use `COPY` command inside your Dockerfile, use `volumes` in your Docker Compose file to edit files inside the container. (for development stage)
```yml
# ...
volumes:
- ./ui:/src:cached
# ...
```
- Run the dev container by selecting `devcontainer: Reopen in Container` option (`ctrl + shift + P`)
## 3. Redis
- To connect to a Redis instance by terminal you can use the following command:
```bash
redis-cli
```
- Some basics commands:
```bash
# Set a key
SET key value
# Get a key
GET key
# Delete a key
DEL key
# List all keys
KEYS *
```
Review [Redis Commands Cheat Sheet](https://redis.io/learn/howtos/quick-start/cheat-sheet) for more commands.
- Monitor the Redis instance with the command:
```bash
redis-cli monitor
```
## 4. Postgres
- To connect to a Postgres instance by terminal you can use the following command:
```bash
psql -U <username> -p 5432 -d <database_name>
```
- Can also use a Postgres GUI client, like DBeaver. So you can use the connection values from the Docker Compose file.
- Some basics commands are:
```bash
# List all databases
\l
# Create a database
CREATE DATABASE <database_name>;
# List all tables
\dt
# Exit
\q
```
## 5. Unit Tests
### 5.1 Unittest
- To run unit tests you can use the following commands:
```bash
# Run test file
python3 -m unittest -vvv tests.test_model
# Run an individual test
python -m unittest -vvv tests.test_image_classifier_app.TestMLService.test_login_failure
```
- Additionally can run test from the python module:
```py
if __name__ == "__main__":
unittest.main(verbosity=2)
```
And then run the module
```bash
python tests/test_image_classifier_app.py
```
### 5.2 Pytest
- To run unit tests you can use the following commands:
```bash
# Run test file
pytest -v -s tests/test_model.py
# Run an individual test
pytest -v -s tests/test_image_classifier_app.py::TestMLService::test_login_failure
```
## 6. Locust
- Install [locust](https://docs.locust.io/en/stable/quickstart.html#locust-s-web-interface)
```bash
uv add locust
```
- Create a folder `stress_test` and `locustfile.py` file
```py
# Basic example
from locust import HttpUser, task
class HelloWorldUser(HttpUser):
@task
def hello_world(self):
self.client.get("/hello")
self.client.get("/world")
```
- Run `locust -f stress_test/locustfile.py`
- Open `http://127.0.0.1:8089`
- Start a load test and fill the number of users/ramp up
- Add the host, for example `http://localhost:8000`
- Start the load test
- Review the results, stats and charts
## 7. AWS
- Download the `epm` file from your AWS account
- Give read permissions to the file with:
```bash
chmod 400 file.epm
```
- Connect it via ssh
```bash
ssh -i file.epm <ec2_user>@<public_ip>
```
- To copy files from local host to remote host:
```bash
scp -i file.epm -r <local_path> <ec2_user>@<public_ip>: # Default home
scp -i file.epm -r <local_path> <ec2_user>@<public_ip>:<remote_path>
```
- Create a tunnel to the remote host:
```bash
ssh -L <local_port>:<remote_host>:<remote_port> -i file.epm <ec2_user>@<public_ip>
```
## 8. GitHub Actions
GitHub Actions is a **CI/CD (Continuous Integration and Continuous Deployment)** tool built into GitHub that allows you to **automate workflows** directly from your repository. With Actions, you can run tests, build your code, deploy applications, and perform other automated tasks whenever certain events occur (like pushing code, creating pull requests, or publishing releases).
Key points:
- **Event-driven**: Workflows run based on triggers like `push`, `pull_request`, or scheduled cron jobs.
- **YAML-based**: Workflows are defined in `.github/workflows/` using YAML syntax.
- **Cross-platform**: Supports Linux, Windows, and macOS runners.
- **Marketplace**: Offers reusable actions to speed up development.
---
### **Basic Example: CI Workflow for Python Project**
```yaml
# File: .github/workflows/python-ci.yml
name: Python CI
on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: |
pytest
```
**What this does:**
- Runs when you **push or create a pull request** to the `main` branch.
- Uses **Ubuntu** as the environment.
- Sets up **Python 3.10**, installs dependencies, and runs **pytest** for tests.
## 9. Streamlit
To fix **telemetry issues** and **403 errors on file uploads** during deployment (Docker/Hugging Face Spaces)., configure Streamlit with a `config.toml` in `/app/.streamlit/`:
```toml
[browser]
gatherUsageStats = false
[server]
enableCORS = false
enableXsrfProtection = false
```
In Dockerfile:
```dockerfile
RUN mkdir -p /app/.streamlit /app/tmp
COPY .streamlit/ /app/.streamlit/
```
Or generate the same configuration in build stage:
```dockerfile
RUN mkdir -p /app/.streamlit \
&& echo "[browser]\n" \
"gatherUsageStats = false\n\n" \
"[server]\n" \
"enableCORS = false\n" \
"enableXsrfProtection = false\n" \
> /app/.streamlit/config.toml
```
|