docker新手。python
目的:開發運行多個web api項目的時候,方便開發,兼顧部署。git
方案大概這樣:web
1 隔離:系統運行在容器裏,1容器只起1進程,代替在host起多個venv或虛擬機;sql
2調試和備份:代碼和數據仍然保存在host,經過-v掛載到容器,用host的編輯器開發和git。docker
3部署:用dockerfile+docker-compose.yaml把配環境的工做固化下來,節省時間。dockerfile=安裝虛擬機+pip install,docker-compose=啓動虛擬機+命令行python3 xx.pyjson
具體到docker, 分紅2個鏡像來作flask
1 ubuntu+python3.6基礎鏡像bootstrap
2 在1的基礎上pip一些python庫ubuntu
dockerhub上官方的python鏡像是基於debian的,啓動CMD默認起一個解釋器。vim
鏡像太精簡,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 .
由於開頭幾步用了國內源,因此很是快。
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種
1 手動敲docker命令2 docker-compose
先試試用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同樣了。
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了。