首页 > 其他 > 详细

docker-compose hello-world

时间:2021-07-04 23:39:39      阅读:32      评论:0      收藏:0      [点我收藏+]

参考官网: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

原文:https://www.cnblogs.com/luckygxf/p/14969219.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!