python的docker环境配置

docker环境配置

Docker基本操作指令, 下载安装参考

https://duoke360.com/tutorial/docker/install-docker

docker + flask + uwsgi + conda 实战部署

目录结构

config.ini

[uwsgi]
http = 0.0.0.0:5000
wsgi-file = /app/src/flask_main.py
callable=app
processes = 2
threads = 8
py-autoreload = true
master = true

flask_main.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World! '

if __name__ == '__main__':
    app.run()

Dockerfile

FROM continuumio/miniconda3

COPY env.yml /app/

WORKDIR /app

SHELL ["/bin/bash", "--login", "-c"]

RUN conda init bash && conda env create -f env.yml


CMD ["/app/src/sh/start.sh"]

在Dockerfile中起不了指定的conda,所以需要在脚本去起
参考

start.sh

#!/bin/bash
server_start(){
        cd src
        source activate
        export PYTHONPATH=`pwd`
        conda activate py36
        uwsgi config.ini
}
server_start

env.yml(仅供参考,以conda export为准)

name: py36
channels:
  - conda-forge
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
dependencies:
  - _libgcc_mutex=0.1
  - _openmp_mutex=4.5
  - ca-certificates=2021.10.8
  - certifi=2021.5.30
  - dataclasses=0.8
  - flask=2.0.2
  - icu=68.1
  - itsdangerous=2.0.1
  - jansson=2.13.1
  - ld_impl_linux-64=2.35.1
  - libffi=3.3
  - libgcc-ng=9.3.0
  - libgomp=9.3.0
  - libiconv=1.16
  - libstdcxx-ng=9.3.0
  - libxml2=2.9.12
  - markupsafe=2.0.1
  - ncurses=6.3
  - openssl=1.1.1k
  - pcre=8.45
  - pip=21.2.2
  - python=3.6.13
  - python_abi=3.6
  - readline=8.1
  - setuptools=58.0.4
  - sqlite=3.36.0
  - tk=8.6.11
  - uwsgi=2.0.19.1
  - werkzeug=2.0.2
  - wheel=0.37.0
  - xz=5.2.5
  - yaml=0.2.5
  - zlib=1.2.11
  - pip:
    - click==8.0.3
    - importlib-metadata==4.8.3
    - jinja2==3.0.3
    - typing-extensions==4.0.1
    - zipp==3.6.0
prefix: /root/miniconda3/envs/py36

执行代码

  • 生成conda环境:conda create -n uwsgi_flask python=3.6
  • 激活conda环境:conda activate uwsgi_flask
  • 使用conda安装uwsgi:conda install uwsgi (不能直接pip安装)
  • 安装falsk:pip install flask
  • 导出yml: conda env export --no-builds > env.yml
  • 构建image:docker build -t uwsgi_flask:v0.1 -f Dockerfile .
  • 启动服务:docker run --rm -it --name uwsgi_flask -p 5000:5000 -v /project/path:/app/src uwsgi_flask:v0.1 ;/project/path 为存放flask服务的地址,映射到/app/src目录中

启动成功如下

posted @ 2021-12-27 15:18  陈科科  阅读(357)  评论(0编辑  收藏  举报