1、安装docker-compose
sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.20.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
2、创建一个文件夹
$ mkdir mybank
$ cd mybank
3、创建一个python的应用程序脚本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)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
4、准备requirements.txt文件,文件内容如下:
flask
redis
5、创建Dockerfile
FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
6、创建docker-compose脚本,创建文件docker-compose.yml,内容如下:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
7、执行docker-compose创建容器:
docker-compose up
启动两个容器如下:
可以先修改一下pip源,加快镜像构建速度
# 更改pip镜像源
mkdir ~/.pip
echo """
[global]
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple/
""" >> ~/.pip/pip.conf
8、执行容器的扩容操作
docker-compose scale redis=500
如下:
开始即时,四分钟后,容器创建完成,检查容器数量:477
9、停止所有的容器
docker-compose stop redis
10、批量删除所有redis容器
docker rm $(docker ps -a|grep redis|awk '{print $1}')