一、传统方式容器编排实例(Docker官网实例)
(1)容器整体的框架
如果按照传统的方式
(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)通过DockerFIle创建镜像
ls mkdir -p /tmp/composetest cd /tmp/composetest vi app.py vi requirements.txt vi Dockerfile docker build -t python-app-image .
补充文件:
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 COPY . . CMD ["flask", "run"]
requirements.txt文件
flask redis
(4)获取Redis的image
- 获取Redis的image
docker pull redis:alpine
创建两个container
创建网络 docker network ls docker network create --subnet=172.20.0.0/24 app-net 创建python程序的container,并指定网段和端口 docker run -d --name web -p 5000:5000 --network app-net python-app-image 创建redis的container,并指定网段 docker run -d --name redis --network app-net redis:alpine
访问测试
ip[centos]:5000