Docker部署用Python編寫的Web應(yīng)用的實踐
1. 安裝 docker
在 WSL2 中安裝 docker https://www.jb51.net/article/223179.htm
會報錯:
# Executing docker install script, commit: 93d2499759296ac1f9c510605fef85052a2c32be
WSL DETECTED: We recommend using Docker Desktop for Windows.
Please get Docker Desktop from https://www.docker.com/products/docker-desktop
You may press Ctrl+C now to abort this script.
+ sleep 20
去下載安裝 windows 下的 docker


2. 編寫代碼
使用 Flask 框架啟動了一個 Web 服務(wù)器,而它唯一的功能是:如果當(dāng)前環(huán)境中有 “NAME” 這個環(huán)境變量,就把它打印在 “Hello” 后,否則就打印 “Hello world”,最后再打印出當(dāng)前環(huán)境的 hostname
import os
from flask import Flask
import socket
from gevent import pywsgi
app = Flask(__name__)
@app.route('/')
def hello():
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname())
if __name__ == "__main__":
server = pywsgi.WSGIServer(('0.0.0.0', 12345), app)
server.serve_forever()
導(dǎo)出依賴包
pip freeze >requirements.txt
Flask==2.0.1 gevent==21.8.0 greenlet==1.1.1 itsdangerous==2.0.1 Jinja2==3.0.1 MarkupSafe==2.0.1 Werkzeug==2.0.1 zope.event==4.5.0 zope.interface==5.4.0
3. 編寫 Dockerfile
# 使用官方提供的 Python 開發(fā)鏡像作為基礎(chǔ)鏡像 FROM python:3.8-slim # 將工作目錄切換為 /app WORKDIR /app # 將當(dāng)前目錄下的所有內(nèi)容復(fù)制到 /app 下 ADD . /app # 使用 pip 命令安裝這個應(yīng)用所需要的依賴 # RUN pip install --trusted-host pypi.python.org -r requirements.txt RUN pip install --trusted-host https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt # 國內(nèi)的源更快 # 允許外界訪問容器的 12345 端口 EXPOSE 12345 # 設(shè)置環(huán)境變量 ENV NAME World # 設(shè)置容器進(jìn)程為:python app.py,即:這個 Python 應(yīng)用的啟動命令 CMD ["python", "app.py"] # CMD 前面 隱式的包含了 ENTRYPOINT , /bin/sh -c

在 WSL 里操作 :
讓 docker 制作鏡像,-t 加 tag,自動加載 Dockerfile,執(zhí)行里面的語句
docker build -t helloworld .
[+] Building 17.4s (10/10) FINISHED => [internal] load build definition from Dockerfile 0.1s => => transferring dockerfile: 757B 0.0s => [internal] load .dockerignore 0.1s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/python:3.8-slim 2.9s => [auth] library/python:pull token for registry-1.docker.io 0.0s => [1/4] FROM docker.io/library/python:3.8-slim@sha256:4dd66d1ccaddaa0587851cb92b365bf3090dccb41393c6f8b 0.0s => [internal] load build context 0.1s => => transferring context: 813B 0.0s => CACHED [2/4] WORKDIR /app 0.0s => [3/4] ADD . /app 0.1s => [4/4] RUN pip install --trusted-host https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt 13.6s => exporting to image 0.6s => => exporting layers0.6s => => writing image sha256:390d32b9f7a20ccd347361bd31450807d3e63d052e334865cf8460968ffceff4 0.0s => => naming to docker.io/library/helloworld 0.0s Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
查看鏡像
(k8s)PC:/mnt/d/gitcode/k8s$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE helloworld latest 390d32b9f7a2 About a minute ago 169MB
啟動容器
docker run -p 4000:12345 helloworld
因為在 Dockerfile 中已經(jīng)指定了 CMD。否則,就得把進(jìn)程的啟動命令加在后面 python app.py
查看容器啟動
(base) $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f6e051d1af6b helloworld "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0:4000->12345/tcp, :::4000->12345/tcp upbeat_elion
通過 -p 4000:12345 告訴 Docker,把容器內(nèi)的 12345 端口映射在宿主機(jī)的 4000 端口上
這樣做的目的是,只要訪問宿主機(jī)的 4000 端口,就可以看到容器里應(yīng)用 返回的結(jié)果
curl http://localhost:4000 # <h3>Hello World!</h3><b>Hostname:</b> dc1c1343e366<br/>
使用容器完成了一個應(yīng)用的開發(fā)與測試
4. 上傳鏡像
注冊 docker hub,docker login 命令登錄
docker tag helloworld kobe24o/helloworld:v0
kobe24o 是賬號名(鏡像倉庫),helloworld 鏡像名,v0自己分配的版本號
docker push kobe24o/helloworld:v0
(k8s) $ docker push kobe24o/helloworld:v0 The push refers to repository [docker.io/kobe24o/helloworld] 931022d457d6: Pushing [================> ] 16.07MB/47.27MB c76dc68917fc: Pushed 047ca6dfe9ab: Pushed d82f4c466b47: Mounted from library/python 5aa75f4e55e7: Mounted from library/python 74d6903a940b: Mounted from library/python 2f9c2b8e82bd: Mounted from library/python ba5a5fe43301: Mounted from library/python

5. 修改鏡像
(base) $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
dd3bf057cb09 helloworld "python app.py" 7 seconds ago Up 5 seconds 0.0.0.0:4000->12345/tcp, :::4000->12345/tcp compassionate_carver
(base) $ docker exec -it dd3bf057cb09 /bin/sh
# pwd
/app
# touch newfile.txt
# ls
Dockerfile app.py newfile.txt requirements.txt
# exit
(base) $ docker commit dd3bf057cb09 kobe24o/helloworld:v1
sha256:ca8880f84040f9bdd7ef13763b9c64f8bd4a513a74bc2b095be06aae5b60268a
上面操作,新加了一個文件到鏡像里,commit 保存
docker inspect --format '{{ .State.Pid}}' dd3bf057cb09
1763
# 查看正在運(yùn)行的容器的進(jìn)程號 PID
通過查看宿主機(jī)的 proc 文件,看到這個 進(jìn)程的所有 Namespace 對應(yīng)的文件
root:/# ls -l /proc/{PID}/ns/
total 0
lrwxrwxrwx 1 root root 0 Sep 14 11:15 cgroup -> 'cgroup:[4026531835]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 ipc -> 'ipc:[4026532220]'
lrwxrwxrwx 1 root root 0 Sep 14 09:49 mnt -> 'mnt:[4026532218]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 net -> 'net:[4026531992]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 pid -> 'pid:[4026532221]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 pid_for_children -> 'pid:[4026532221]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 user -> 'user:[4026531837]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 uts -> 'uts:[4026532219]'
一個進(jìn)程,可以選擇 加入 到某個進(jìn)程已有的 Namespace 當(dāng)中,從而達(dá)到 “進(jìn)入” 這個進(jìn)程所在容器的目的,這正是 docker exec 的實現(xiàn)原理
push 到 hub
(base) $ docker push kobe24o/helloworld:v1 The push refers to repository [docker.io/kobe24o/helloworld] dfee38b42dbb: Pushed 931022d457d6: Layer already exists c76dc68917fc: Layer already exists 047ca6dfe9ab: Layer already exists d82f4c466b47: Layer already exists 5aa75f4e55e7: Layer already exists 74d6903a940b: Layer already exists 2f9c2b8e82bd: Layer already exists ba5a5fe43301: Layer already exists v1: digest: sha256:7af7ff571ea9fd70d48abeaa2b38a1ed1c1a4e5a933b722d82af25d3e889f84e size: 2206

到此這篇關(guān)于Docker部署用Python編寫的Web應(yīng)用的文章就介紹到這了,更多相關(guān)Docker部署Python Web應(yīng)用內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信