DockerCompose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Compose test

   1 cd ~/Documents
   2 mkdir composetest
   3 cd composetest
   4 nano app.py
   5 nano requirements.txt
   6 nano Dockerfile
   7 nano docker-compose.yml
   8 docker-compose up
   9 http://localhost:5000/
  10 
  11 # nano app.py change hello world message. the change should be deployed in the docker container through the mount volume in /code
  12 # refresh browser page 
  13 ctrl+c

app.py

   1 import time
   2 
   3 import redis
   4 from flask import Flask
   5 
   6 app = Flask(__name__)
   7 cache = redis.Redis(host='redis', port=6379)
   8 
   9 def get_hit_count():
  10     retries = 5
  11     while True:
  12         try:
  13             return cache.incr('hits')
  14         except redis.exceptions.ConnectionError as exc:
  15             if retries == 0:
  16                 raise exc
  17             retries -= 1
  18             time.sleep(0.5)
  19 
  20 @app.route('/')
  21 def hello():
  22     count = get_hit_count()
  23     return 'Hello World! I have been seen {} times.\n'.format(count)

requirements.txt

flask
redis

Dockerfile

FROM python:3.7-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"]

docker-compose.yml

   1 version: "3.3"
   2 services:
   3   web:
   4     build: .
   5     ports:
   6       - "5000:5000"
   7     volumes:
   8       - .:/code
   9     environment:
  10       FLASK_ENV: development
  11   redis:
  12     image: "redis:alpine"

DockerCompose (last edited 2021-07-31 20:24:07 by localhost)