docker新手。
目的:開發運行多個web api項目的時候,方便開發,兼顧部署。
方案大概這樣:
1 隔離:系統運行在容器里,1容器只起1進程,代替在host起多個venv或虛擬機;
2調試和備份:代碼和數據仍然保存在host,通過-v掛載到容器,用host的編輯器開發和git。
3部署:用dockerfile+docker-compose.yaml把配環境的工作固化下來,節省時間。dockerfile=安裝虛擬機+pip install,docker-compose=啟動虛擬機+命令行python3 xx.py
1創建鏡像
具體到docker, 分成2個鏡像來做
1 ubuntu+python3.6基礎鏡像
2 在1的基礎上pip一些python庫
1.1 ubuntu+python3.6
dockerhub上官方的python鏡像是基於debian的,啟動CMD默認起一個解釋器。
鏡像太精簡,apt ip ifconfig等等工具好像都沒有裝,稍微想裝點工具就不行,作為部署環境還可以,開發用的話,還是自己用ubuntu搞一個算了。
因為是開發用,所以重在隔離,不太在乎體積。
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
FROM ubuntu LABEL author="lazyman" #用ubuntu國內源替換默認源 RUN rm /etc/apt/sources.list COPY sources.list /etc/apt/sources.list #安裝python3.6必要的包。源鏡像太精簡了,ip ifconfig之類的都沒有。后續安裝python pip也需要一些。但是build_essential似乎不必須,先去了。如果后面安裝numpy之類需要gcc了,再加上 RUN apt-get update #RUN apt-get install -y apt-transport-https vim iproute2 net-tools build-essential ca-certificates curl wget software-properties-common RUN apt-get install -y apt-transport-https vim iproute2 net-tools ca-certificates curl wget software-properties-common #安裝python3.6 來自第三方 RUN add-apt-repository ppa:jonathonf/python-3.6 RUN apt-get update RUN apt-get install -y python3.6 RUN apt install -y python3.6-dev RUN apt install -y python3.6-venv #為3.6安裝pip RUN wget https://bootstrap.pypa.io/get-pip.py RUN python3.6 get-pip.py #和自帶的3.5共存 RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1 RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2 RUN update-alternatives --config python3 #print()時在控制台正常顯示中文 ENV PYTHONIOENCODING=utf-8
在dockerfile所在路徑下執行,建立image
docker build -t ubuntu-with-python .
因為開頭幾步用了國內源,所以非常快。
1.2 開發環境
FROM ubuntu-with-python LABEL author="lazyman" #代碼復制過來后的路徑 RUN mkdir /code WORKDIR /code #安裝需要的python庫 RUN pip3 install flask RUN pip3 install flask-sqlalchemy RUN pip3 install flask_restful
2啟動容器
也分2種
1 手動敲docker命令2 docker-compose
2.1手動敲docker命令
先試試用docker命令行啟動容器:
docker run --name quotation_api -itd -p 5000:5000 -v /home/quotation:/code quotation_dev:latest
用到的參數分別是
--name為容器命名;
-itd 輸入輸出終端,后台運行
-p host端口:容器端口 用5000是flask默認
-v host路徑:容器內路徑
最后是使用的鏡像名(前面剛用dockerfile build出來的)
docker attach quotation_api
用python3 main.py啟動flask,OK。
這樣flask運行在docker里了。
在host改代碼,可以看見docker的控制台在更新,和在host一樣了。
2.2使用dock-compose
dock-compose用來管理多個container的運行,特別適合1個host上跑多個container的情況。
得天獨厚,docker官網上dock-compose的gettingstarted文檔就是flask的(說明flask+docker代表了先進生產力的前進方向O Yeah!),看完了基本就能用了。
dock-compose采用yaml作為配置文件。查了一下,yaml參考了xml和json,以及python的語法,采用了python之縮進,無XML之標記,無json之括號,無字符串之引號。特別適合作為配置文件用。
建立docker-compose.yaml文件:
version: "3"
services:
quotation_api:
image: quotation_dev:latest
volumes:
- /home/quotation:/code
ports:
- "5000:5000"
command: python3 main.py
基本對應手動敲的docker命令,最后還省了敲python3 main.py。
當然如果是部署,這句可以用CMD 寫進Dockfile。但是開發過程,文件名之類的會改變,比如最終部署運行用可能是gunicorn+wsgi.py,所以還是寫在dockerfile外面比較方便
運行,在控制台執行:
docker-compose up
正常的話,已經能看見flask的控制台了
把手敲的命令基本都省了,包括(docker attach 容器名)之類。
但是每次有語法錯誤,容器會報錯退出。修改完,還得在控制台按“↑”和“enter”重新執行一次docker-compose up。
這是flask的原因:flask自帶的reload只能在語法沒毛病的情況下,如果有錯就會報錯退出,因為容器執行的是python3 main.py,這個退了,容器就會stop。
為了解決這個問題,可以用flask-failsafe插件。然后就完美啦。語法錯誤也會reload了。