flask项目ubuntu docker中运行
2023-05-15 17:37:43
安装docker
- sudo apt install docker.io
- sudo systemctl start docker
python先测试
- sudo apt install python3.10-venv
- python3 -m venv env
- . env/bin/activate
- pip install flask
- pip install gunicorn
- pip freeze > requirements.txt
编辑app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return '欢迎来到我的 Watchlist!'
运行flask
- gunicorn -b 0.0.0.0:9000 app:app
生成Dockerfile
FROM python:3.10
WORKDIR /Projects/flask1
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY *.py .
CMD ["gunicorn", "-b", "0.0.0.0:9000", "app:app" ]
docker build & run
- sudo docker build -t 'first_flask' .
- sudo docker run -itd -p 0.0.0.0:9000:9000 --name=first first_flask