一个flask-redis的案例
多容器复杂应用的部署:
Flask+redis, flask的container访问redis的container
cat app.py
from flask import Flask from redis import Redis import os import socket app = Flask(__name__) redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379) @app.route('/') def hello(): redis.incr('hits') return 'Hello Container World! I have been seen %s times and my hostname is %s.\n' % (redis.get('hits'),socket.gethostname()) if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=True)
cat Dockerfile
FROM python:2.7 LABEL maintaner="lin@gmail.com" COPY . /app WORKDIR /app RUN pip install flask redis EXPOSE 5000 CMD [ "python", "app.py" ]
1.创建redis的container:
docker run -d --name redis redis
2. dokcer build -t lin/flask-redis .
3.创建container
docker run -d -p 5000:5000 --link redis --name flask-redis -e REDIS_HOST=redis lin/flask-redis #和上面源码的相对应
4.进入上面的container,并执行env看一下:
docker exec -it flask-redis /bin/bash
env #环境变量
ping redis #在容器里可以ping redis
5.在主机访问curl 127.0.0.1:5000。可以访问到