zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    LeetCode-Maximum Gap
    LintCode-Implement Queue by Stacks
    LintCode-Search Range in Binary Search Tree
    LintCode-BackPack II
    LintCode-Minimum Subarray
    LintCode-Sort Letters by Case
    LintCode-Longest Common Subsequence
    POJ 2226
    POJ 2724
    POJ 3692
  • 原文地址:https://www.cnblogs.com/nymrli/p/11251719.html
Copyright © 2011-2022 走看看