第一步:新建flask项目 app.py
vim app.py
# 书写以下内容
from flask import Flask
from redis import Redis
import os
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
@app.route('/')
def hello():
redis.incr('hits')
return '你好! 查看 %s 次\n' % (redis.get('hits'))
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
第二步:编写Dockerfile
# 用于构建flask项目的镜像
vim Dockerfile
# 书写以下内容
FROM python:3.9
WORKDIR /app
COPY . /app
RUN pip install flask redis -i https://pypi.tuna.tsinghua.edu.cn/simple
EXPOSE 5000
CMD [ "python", "app.py" ]
第三步:编写docker-compose的yml文件 docker-compose.yml
vim docker-compose.yml
# 书写以下内容
version: "3"
services:
redis:
image: redis
web:
build:
context: .
dockerfile: Dockerfile
ports:
- 8080:5000
environment:
REDIS_HOST: redis
第四步:启动docker-compose
docker-compose up
测试
# 访问项目
10.0.0.100:8080
# 进入web,ping redis
docker-compose exec web /bin/bash
# 下载ping
apt-get update
apt-get install inetutils-ping
ping redis
标签:__,compose,flask,app,redis,docker
From: https://www.cnblogs.com/wellplayed/p/18077111