File size: 2,666 Bytes
b4f755d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec0654b
 
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

#  Deployment 

This project is containerized and deployed on **Hugging Face Spaces** using a custom `Dockerfile`. This guide explains the structure of the Dockerfile and key considerations for deploying FastAPI apps on Spaces with Docker SDK.

---

## πŸ“¦ Base Image

```dockerfile
FROM python:3.9
````

We use the official Python 3.9 image for compatibility and stability across most Python libraries and tools.

---

## πŸ‘€ Create a Non-Root User

```dockerfile
RUN useradd -m -u 1000 user
USER user
ENV PATH="/home/user/.local/bin:$PATH"
```

* Hugging Face Spaces **requires** that containers run as a non-root user with UID `1000`.
* We also prepend the user's local binary path to `PATH` for Python package accessibility.

---

## πŸ—‚οΈ Set Working Directory

```dockerfile
WORKDIR /app
```

All application files will reside under `/app` for consistency and clarity.

---

## πŸ“‹ Install Dependencies

```dockerfile
COPY --chown=user ./requirements.txt requirements.txt
RUN pip install --no-cache-dir --upgrade -r requirements.txt
```

* Copies the dependency list with correct file ownership.
* Uses `--no-cache-dir` to reduce image size.
* Ensures the latest compatible versions are installed.

---

## πŸ”‘ Download Language Model (Optional)

```dockerfile
RUN python -m spacy download en_core_web_sm || echo "Failed to download model"
```

* Downloads the small English NLP model required by SpaCy.
* Uses `|| echo ...` to prevent build failure if the download fails (optional safeguard).

---

## πŸ“ Copy Project Files

```dockerfile
COPY --chown=user . /app
```

Copies the entire project source into the container, setting correct ownership for Hugging Face's user-based execution.

---

## 🌐 Start the FastAPI Server

```dockerfile
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
```

* Launches the FastAPI app using `uvicorn`.
* **Port 7860 is mandatory** for Docker-based Hugging Face Spaces deployments.
* `app:app` refers to the `FastAPI()` instance in `app.py`.

---

## βœ… Deployment Checklist

* [x] Ensure your main file is named `app.py` or adjust `CMD` accordingly.
* [x] All dependencies should be listed in `requirements.txt`.
* [x] If using models like SpaCy, verify they are downloaded or bundled.
* [x] Test your Dockerfile locally with `docker build` before pushing to Hugging Face.

---

## πŸ“š References

* Hugging Face Docs: [Spaces Docker SDK](https://huggingface.co/docs/hub/spaces-sdks-docker)
* Uvicorn Docs: [https://www.uvicorn.org/](https://www.uvicorn.org/)
* SpaCy Models: [https://spacy.io/models](https://spacy.io/models)

---

Happy deploying!  
**P.S.** Try not to break stuff. πŸ˜