原文链接: docker-compose 简单例子
上一篇: Python whoosh 全文检索
下一篇: Python opencv 使用 摄像头录制保存视频和保存截图 镜像变换
安装,可参考官方
https://docs.docker.com/compose/install/
简单命令
apt-get install docker-compose
项目结构,requirements.txt 是Python的依赖包,由于网络原因使用pip手动安装
app.py
import time
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
requirements.txt
flask
redis
Dockerfile
FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
手动换源,使用豆瓣源加速
pip install -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com django
FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install flask -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com
RUN pip install redis -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com
CMD ["python", "app.py"]
docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
执行命令
docker-compose up
查看结果,每次刷新数目会加一
http://0.0.0.0:5000