創建 ubuntu 並安裝 nginx 的 image
FROM ubuntu:16.04
MAINTAINER demo@gmail.com
RUN apt-get update
RUN apt-get install –y nginx
CMD ["echo", "Nginx Image created"]
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 get_index():
count = get_hit_count()
return 'Yo! 你是第 {} 次瀏覽\n'.format(count)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
建立套件 requirements.txt 安裝資訊讓 Dockerfile 可以下指令安裝套件
FROM Python
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Docker Compose file 描述 services 運作狀況
gedit docker-compose.yml &
version: '3'
services:
web:
build: .
ports:
-"5000:5000"
volumes:
- .:/code
redis:
image: "redis:alpine"