docker檔案要有的結構:
app.py:
import os
from flask import Flask
from flask_redis import FlaskRedis
app = Flask(__name__)
app.config['REDIS_URL'] = os.environ['REDIS_URL']
r = FlaskRedis(app)
@app.route('/')
def index():
if not r.exists("count"):
r.set("count", 0)
r.incr("count")
return r.get("count")
if __name__ == "__main__":
app.run(host="0.0.0.0")
docker-compose.yml:
version: '3.8'
services:
backend:
build: .
container_name: backend_flask
networks:
- net
ports:
- 5000:5000
environment:
- REDIS_URL=redis://cache:6379/0
volumes:
- .:/project:ro
command: "python3 app.py"
cache:
image: redis
container_name: database_redis
networks:
- net
ports:
- 6379:6379
networks:
net:
name: frnet
driver: bridge
Dockerfile:
FROM python:3.9.10-alpine3.15
WORKDIR /test-project
COPY * .
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:app", "--preload"]
以及產生一個requirements檔