docker-compose hello-world
参考官网:https://docs.docker.com/compose/gettingstarted/
1. 创建测试目录,放测试文件
mkdir composetest
cd composetest
2. 当前目录创建app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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文件
1 2 | flask redis |
4. 当前目录创建一个Dockerfile
1 2 3 4 5 6 7 8 9 10 11 | # 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
1 2 3 4 5 6 7 8 | version: "3.9" services: web: build: . ports: - "5000:5000" redis: image: "redis:alpine" |
6. 当前目录执行docker-compose up
7. 启动完成,浏览器打开http://localhost:5000/
Please call me JiangYouDang!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步