docker-compose hello-world

参考官网:https://docs.docker.com/compose/gettingstarted/

1. 创建测试目录,放测试文件

mkdir composetest
 cd composetest

2. 当前目录创建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)

 

3. 当前目录创建requirements.txt文件

flask
redis

  

4. 当前目录创建一个Dockerfile

# syntax=docker/dockerfile:1
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"]

  

5. 当前目录创建文件docker-compose.yml

version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

  

6. 当前目录执行docker-compose up

7. 启动完成,浏览器打开http://localhost:5000/ 

docker-compose hello-world

 

docker-compose hello-world

上一篇:随机三角形平铺布局算法(iOS实现)


下一篇:如何在 iPad 上截屏