【Docker】項目實戰(zhàn),部署自己的APP
點擊上方“AI算法與圖像處理”,選擇加"星標"或“置頂”
重磅干貨,第一時間送達
來源:機器視覺CV
之前我們介紹了 Docker 命令使用大全,今天就來進行實戰(zhàn)一下
目標:
-
編寫自己的 Dockerfile 鏡像 -
創(chuàng)建一個簡單的 Web 界面 -
使用鏡像創(chuàng)建一個 Flask APP
有關 Dockerfile 的相關知識,我在后面的文章會進行講解,今天主要是實際操作
所需工具:安裝好 Docker 的服務器或者本地電腦,筆者使用的是服務器:Ubuntu 系統(tǒng)
創(chuàng)建一個 Flask APP
首先創(chuàng)建一個 Flask app
-
app.py
from flask import Flask, render_template
import random
app = Flask(__name__)
# list of fox images,用來進行頁面展示的
images = [
"https://media0.giphy.com/media/Ko5dZRMv9uJFu/giphy.gif",
"https://media.tenor.com/images/6461359b4205a95bf1f4374a3aa2acec/tenor.gif",
"https://i.imgur.com/dUBv79d.gif",
"https://media2.giphy.com/media/dvBgr7pA6FTJOMOALY/giphy.gif",
"https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/45dfcad0-23ff-4af4-8c3f-b4b68f6edab4/d5hxh3z-aac8f004-e5db-4030-8e0c-62b899b4d0ce.gif"
]
@app.route('/')
def index():
url = random.choice(images)
return render_template('index.html', url=url)
if __name__ == "__main__":
app.run(host="0.0.0.0")
創(chuàng)建一個 requestment.txt 文件,把 Python 需要的包及其版本放進去,方便后續(xù)安裝
-
requestment.txt
Flask==0.10.1
創(chuàng)建一個簡單的 Web 頁面
-
templates/index.html
創(chuàng)建一個 templates 的文件夾,并在此文件夾下創(chuàng)建 index.html 文件
<html>
<head>
<style type="text/css">
body {
background: black;
color: white;
}
div.container {
max-width: 500px;
margin: 100px auto;
border: 20px solid white;
padding: 10px;
text-align: center;
}
h4 {
text-transform: uppercase;
}
style>
head>
<body>
<div class="container">
<h4>Fox Gif of the dayh4>
<img src="{{url}}" />
<p><small>Courtesy: <a href="https://leonglearnai.com/">AICVa>small>p>
div>
body>
html>
Dockerfile
我們基于 Alpine 構建一個鏡像 Alpine:Alpine Linux 的最小 Docker 映像,具有完整的包索引,大小只有 5mb,非常實用。
對命令的含義進行了注釋
# 基礎鏡像
FROM alpine:3.9
#因為我們需要運行 Python,所以需要配置環(huán)境:安裝 Python 和 pip 到Apline Linux 中,該命令不僅會安裝 pip 包,也會安裝其他的依賴(如 Python 的解釋器)
# RUN 是 Docker 的命令, apk add --update py2-pip 類似于 Linux 命令
RUN apk add --update py2-pip
# 拷貝本地文件 requirements.txt(默認與 Dockerfile 同一文件夾)到 容器的 /usr/src/app/ 文件夾下,會自動創(chuàng)建
COPY requirements.txt /usr/src/app/
# 安裝 所需要的 python 包
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
# 拷貝其他文件
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/
# 容器需要暴露端口,F(xiàn)lask 程序運行的端口
EXPOSE 5000
# 運行 python程序,該命令的意思是 python /usr/src/app/app.py
CMD ["python", "/usr/src/app/app.py"]
目錄結構如下所示:
編譯鏡像
編寫完 Dockerfile,接下來就是進行編譯了,使用 docker bulid
是 Docker hub 的用戶名 命令最后有個 . 不要漏了,其代表的含義是 Dockerfile 所在的路徑
docker build -t
/myapp .
下面是編譯過程中的一些輸出:
Sending build context to Docker daemon 6.656kB
Step 1/8 : FROM alpine:3.9
---> 78a2ce922f86
Step 2/8 : RUN apk add --update py2-pip
---> Using cache
---> ba2fa67ca853
Step 3/8 : COPY requirements.txt /usr/src/app/
---> Using cache
---> 43511e5ced4b
Step 4/8 : RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
---> Running in 97289c7eda9d
Collecting Flask==0.10.1 (from -r /usr/src/app/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/db/9c/149ba60c47d107f85fe52564133348458f093dd5e6b57a5b60ab9ac517bb/Flask-0.10.1.tar.gz (544kB)
Collecting Werkzeug>=0.7 (from Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298kB)
Collecting Jinja2>=2.4 (from Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/30/9e/f663a2aa66a09d838042ae1a2c5659828bb9b41ea3a6efa20a20fd92b121/Jinja2-2.11.2-py2.py3-none-any.whl (125kB)
Collecting itsdangerous>=0.21 (from Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/b9/2e/64db92e53b86efccfaea71321f597fa2e1b2bd3853d8ce658568f7a13094/MarkupSafe-1.1.1.tar.gz
Installing collected packages: Werkzeug, MarkupSafe, Jinja2, itsdangerous, Flask
Running setup.py install for MarkupSafe: started
Running setup.py install for MarkupSafe: finished with status 'done'
Running setup.py install for Flask: started
Running setup.py install for Flask: finished with status 'done'
Successfully installed Flask-0.10.1 Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 itsdangerous-1.1.0
Removing intermediate container 97289c7eda9d
---> 9dbc17abb6f7
Step 5/8 : COPY app.py /usr/src/app/
---> 0c69faca84cb
Step 6/8 : COPY templates/index.html /usr/src/app/templates/
---> a0e7ce10250b
Step 7/8 : EXPOSE 5000
---> Running in f570b863937d
Removing intermediate container f570b863937d
---> ba48b6b1c4bd
Step 8/8 : CMD ["python", "/usr/src/app/app.py"]
---> Running in 2a73d498ea52
Removing intermediate container 2a73d498ea52
---> b64a5a0d5dd0
Successfully built b64a5a0d5dd0
Successfully tagged aicv/myfirstapp:latest
編譯完成后,我們可以看到鏡像出現(xiàn)了
運行鏡像為容器
我們使用創(chuàng)建的鏡像運行一個容器,將容器中的 5000 端口映射到宿主機的 8899 端口
docker run -p -d 8899:5000 --name myfirstapp aicv/myfirstapp
打開 http://localhost:8899 或者 http://ip:8899 就能看到頁面了,刷新頁面可以看到不同的畫面。
推送到遠程倉庫
要推送到遠程倉庫,首先需要登錄你自己的 Docker hub 賬號
docker login
dokcker push YOUR_USERNAME/myfirstapp
本節(jié)我們完成了一個簡單的 Flask APP 的部署工作,了解了 Dockerfile 的基本使用,并將鏡像推送到我們的遠程倉庫中。
下載1:OpenCV黑魔法
在「AI算法與圖像處理」公眾號后臺回復:OpenCV黑魔法,即可下載小編精心編寫整理的計算機視覺趣味實戰(zhàn)教程
下載2 CVPR2020
在「AI算法與圖像處理」公眾號后臺回復:CVPR2020,即可下載1467篇CVPR 2020論文 個人微信(如果沒有備注不拉群!) 請注明:地區(qū)+學校/企業(yè)+研究方向+昵稱
覺得有趣就點亮在看吧

