Hi, below are the files I used. However, when I tried to compose the docker with: docker compose up, it returned error.
Does anyone know the cause of this? Thank you in advance.
File I used:
docker-compose.yaml
version: “3.11”
services:
web:
build: .
ports:
- “20000:5000”
redis:
image: “redis:alpine”
app.py
import time
import redisfrom 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)
Dockerfile
syntax=docker/dockerfile:1
FROM python:3.11-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD [“flask”, “run”]
requirements.txt
flask
redis