---
title: 雲端技術實務期末專題
tags: 期末專題
---
# 手寫辯識 Docker 部署
:::spoiler 目錄
[TOC]
:::
## 環境
作業系統:Arch Linux KDE
文字編輯器:Sublime Text 4
虛擬化部署:Docker 24.0.2
虛擬機器平台:OpenStack
手寫辯識:
- Tensorflow
- OpenCV
- Pillow
前端:p5.js
網頁框架:Flask
## 製作 Model
* [Colab](https://colab.research.google.com/drive/1uqs9DOVtDc3nOFwBiJ6K51fknN5MZvwc?usp=sharing)
## 安裝 Docker
* 更新 `keyring`
```bash
$ sudo pacman -Syy archlinux-keyring
```
* 下載 `docker`
```bash
$ sudo pacman -Sy docker
```
* 安裝套件三連
```bash
$ sudo systemctl enable docker
```
```bash
$ sudo systemctl start docker
```
```bash
$ sudo systemctl status docker
```

> Ative 顯示 actvie ( running ) 就代表安裝成功
* 開始下指令會噴錯

* 設定群組
```bash
$ sudo groupadd -f dcoker
$ sudo usermod -aG docker $USER
$ newgrp docker
```
* 最後下這個會看到 `docker` 會在最前面
```bash
$ groups
```

* `docker` 指令就可以正常下了

* 測試 `docker`
```bash
$ docker run hello-world
```

> 要先 `docker pull hello-world`
* 下載 `ubuntu 22.04`([連結](https://hub.docker.com/_/ubuntu))
```bash
$ docker pull ubuntu:22.04
```

> 看到這樣代表下載成功
## 製作 Image
### Dockerfile
```dockerfile
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y \
python3 \
python3-pip \
python3-dev \
&& apt-get clean
RUN pip3 install tensorflow
RUN pip3 install opencv-python-headless
RUN pip3 install flask
RUN pip3 install pillow
COPY model.h5 /app/cloud_model.h5
COPY static/main.js /app/static/main.js
COPY static/style.css /app/static/style.css
COPY templates/index.html /app/templates/index.html
COPY app.py /app/app.py
CMD python3 /app/app.py
```
### 其他 Code
放在 [qaz5823091/NumberRecognition - Github](https://github.com/qaz5823091/NumberRecognition)
### 檔案結構

### 試跑
* build
```bash
$ docker build -t number_recognition .
```
* run
```bash
$ docker run nubmer_recogntion
```
### 關閉 container
* 查看 ID
```bash
$ docker ps
```
* 再透過 `id` 停止
```bash
$ docker containter stop <id>
```
### 打包
* 把 `image` 附上 `tag`
```bash
$ docker tag number_recognition cpp1092020/web_dl_1092926
```
* 再來 `push`
```bash
$ docker push cpp1092020/web_dl_1092926
```
* 結果

### 執行
* 把 `image` 載下來
```bash
$ docker pull cpp1092020/web_dl_1092926
```
> 應該會跑很久,因為有 `tensorflow`
* 最後執行
```bash
$ docker run -v ./static:/app/static -p 5000:5000 number_recogition
```
> 要先到路徑內
> `-v` 是把使用者丟的圖片對應到 Host Computer,應該也可以不用
## 成果
* 輸入圖片

* 手寫數字

## 參考
* [Day4:用簡單的例子來說明如何使用 Docker 指令](https://ithelp.ithome.com.tw/articles/10190921)
* [Docker permission denied](https://phoenixnap.com/kb/docker-permission-denied)
* [TensorFlow 2 教學:MNIST--自訂 Model 進階教學](https://weikaiwei.com/tf/tensorflow-2-mnist-2/)
* [上傳 Docker Image 到 Docker Hub](https://ithelp.ithome.com.tw/articles/10192824)
* [強化訓練](https://github.com/SweetornotspicyMarathon/Learn-Tensorflow-Keras-Note/blob/main/U4%2BU6%2BU7.ipynb)
* [CV2 強化圖片](https://github.com/eesgetdegrees/format-your-own-images-to-match-MNIST-dataset/blob/main/image_format.py)