docker中部署uwsgi+flask+nginx


Docker 運行python flask的web程序

1創建鏡像

1.1 ubuntu16.04+python3.6

18.04卡在了PPA環節,並且git安裝也沒安裝上,后來使用了dockerHub上搜素到的github倉庫中的16.04 Xenial就解決了。

注:鏡像TAG版本需要到dockerHub上才能查看,最初下載成18.04就是因為這個原因被坑了

18.04PPA問題:

aptsources.distro.NoDistroTemplateException: Error: could not find a distribution template for Ubuntu/bionic意思是18.04該PPA沒有資源.bionic是版本代號,如16.04的 Xenial

⑴使用下載好的Xenial的Dockerfile進行創建鏡像docker run 1604ubuntu .

為了使用國內源用阿里雲,先編輯一個sources.list,放在dokcerfile同目錄下,作為docker創建鏡像時的上下文。

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

⑵根據官方的鏡像來編寫自己的Dockerfile創建具有工具的Ubuntu1604

涉及交互式選擇項(如下),docker build的時候會報錯。設置 DEBIAN_FRONTEND=noninteractive

FROM 1604ubuntu
MAINTAINER mrli
#用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 update
#RUN apt upgrade

RUN apt install -y apt-utils apt-transport-https  vim iproute2 net-tools ca-certificates curl build-essential wget python-software-properties software-properties-common psmisc

#安裝python3.6 來自第三方
RUN add-apt-repository ppa:jonathonf/python-3.6
RUN apt update
RUN apt 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

#設置默認python為python3
RUN update-alternatives --install /usr/bin/python python /usr/bin/python2 100
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 150

#和自帶的3.5共存,設置python3默認為3.6
#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 uos:1604 .

因為開頭幾步用了國內源,所以非常快。

1.2 開發環境

再建一個dockerfile,開頭使用剛才建立的鏡像uos1604

FROM uos:1604
MAINTAINER mrli

#代碼復制過來后的路徑
RUN mkdir /app
# 指定容器啟動時執行的命令都在app目錄下執行
WORKDIR /app

# 將本地app目錄下的內容拷貝到容器的app目錄下
COPY ./app/ /app/

# 安裝nginx
RUN apt -y install nginx mysql-server 

RUN /etc/init.d/nginx start
# 替換nginx的配置
RUN rm  /etc/nginx/sites-enabled/default
RUN cp nginx.conf /etc/nginx/sites-enabled/nginx.conf

RUN pip3 install uwsgi

#安裝需要的python庫
# 啟動nginx和uwsgi
#ENTRYPOINT pip install -r requirements.txt  -i  https://pypi.tuna.tsinghua.edu.cn/simple some-package --no-cache-dir && service nginx restart && uwsgi --ini uwsgi.ini

# 為了保證能之后進入所以最后一個命令為/bin/sh
ENTRYPOINT pip install -r requirements.txt  -i  https://pypi.tuna.tsinghua.edu.cn/simple some-package --no-cache-dir && service nginx restart && uwsgi --ini uwsgi.ini & && /bin/sh


創建uflask鏡像:docker build -t uflask .

根據鏡像創建運行容器:docker run -tid -p 12345:80 flaskdemo IMAGE_ID

此時就可以通過VPS的IP地址:宿主機端口訪問這個應用程序

查看日志:docker logs 應用名(NAMES)docker logs flaskdemo

關於mysql的建議

mysql建議作為單獨容器來跑數據庫,然后遠程連接數據庫.或是使用數據卷

# 
# 搜索
# docker search mysql
# 拉取
# docker pull mysql:5.7
#運行
# docker run --name mysql5.7 -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -d mysql:5.7


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM