Docker批次容器編排的實現介紹

佚名發表於2020-10-24

章主要介紹了Docker批次容器編排的實現,文中透過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。

簡介

Dockerfile build run 是手動操作單個容器,假如使用微服務架構,需要啟動 100 + 個容器,他們之間的依賴關係如何維護?
Docker Compose 用來輕鬆高效地管理容器,定義執行多個容器。

三個步驟:

  • Dockerfile
  • Services & docker-compose.yml
  • docker-compose up

初體驗

1.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"]

2.Service

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)

docker-compose.yml

version: '3'
services:
web:
 build: .
 ports:
- "5000:5000"
 volumes:
- .:/code
 - logvolume01:/var/log
 links:
- redis
redis:
 image: redis
volumes:
logvolume01: {}
docker-compose up
Starting compose-demo_web_1  ... done
Starting compose-demo_redis_1 ... done
Attaching to compose-demo_redis_1, compose-demo_web_1
redis_1 | 1:C 12 Sep 2020 07:34:09.654 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 12 Sep 2020 07:34:09.655 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 12 Sep 2020 07:34:09.655 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 12 Sep 2020 07:34:09.657 * Running mode=standalone, port=6379.
redis_1 | 1:M 12 Sep 2020 07:34:09.657 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 12 Sep 2020 07:34:09.657 # Server initialized
redis_1 | 1:M 12 Sep 2020 07:34:09.658 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 12 Sep 2020 07:34:09.658 * Loading RDB produced by version 6.0.7
redis_1 | 1:M 12 Sep 2020 07:34:09.658 * RDB age 156 seconds
redis_1 | 1:M 12 Sep 2020 07:34:09.658 * RDB memory usage when created 0.77 Mb
redis_1 | 1:M 12 Sep 2020 07:34:09.658 * DB loaded from disk: 0.000 seconds
web_1  | * Serving Flask app "app.py"
web_1  | * Environment: production
web_1  |  WARNING: This is a development server. Do not use it in a production deployment.
web_1  |  Use a production WSGI server instead.
web_1  | * Debug mode: off
YML 檔案規則
version: "1.0" #版本
services: #服務列表
  service1:
    #服務配置
    container_name: #容器名稱
    depends_on: #依賴列表
    - depend1
    - depend2
    images: #映象
    - image1
    - image2
    build:. #構建目錄
    network: #網路
    ......
  service2: test2
    ......
volumnes: #掛載目錄列表
networks: #網路列表
configs: #其他配置

到此這篇關於Docker批次容器編排的實現的文章就介紹到這了,更多相關Docker批次容器編排內容請搜尋指令碼之家以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援指令碼之家!

相關文章