Docker Compose

Compose的使用步驟

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  3. Run docker compose up and the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up using the docker-compose binary.
version: "3.9"  # optional since v1.27.0
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/code
      - logvolume01:/var/log
    links:
      - redis
  redis:
    image: redis
volumes:
  logvolume01: {}

安裝

# 1.下載
# 官網(wǎng)提供 (沒有下載成功)
curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
 
# 國(guó)內(nèi)地址
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

# 2.授權(quán)
chmod +x /usr/local/bin/docker-compose

# 3.查看版本
[root@iz8vbd1ko98b0el6771a37z bin]# docker-compose version
docker-compose version 1.25.5, build 8a1c60f6
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

體驗(yàn)

  1. 創(chuàng)建項(xiàng)目目錄
[root@iz8vbd1ko98b0el6771a37z ~]# mkdir composetest
[root@iz8vbd1ko98b0el6771a37z ~]#  cd composetest
  1. 創(chuàng)建應(yīng)用文件app.py
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)
  1. 創(chuàng)建requirements.txt
flask
redis
  1. 編寫Dockerfile,將app.py應(yīng)用打包為精細(xì)
# 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"]
  1. 編寫docker-compose.yml文件,定義整個(gè)服務(wù),需要的環(huán)境。web、redis
version: "3.3"
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"
  1. 啟動(dòng)compose項(xiàng)目
[root@iz8vbd1ko98b0el6771a37z ~]#  docker-compose up
流程
  1. 創(chuàng)建網(wǎng)絡(luò)
    Creating network "composetest_default" with the default driver
  2. 執(zhí)行Docker-compose.yml
  3. 啟動(dòng)服務(wù)
Creating composetest_redis_1 ... done
Creating composetest_web_1   ... done

yaml規(guī)則

version: "2.4" # 版本
services:        # 服務(wù)
  服務(wù)1:web
  images
  build
  network
  ......
  服務(wù)2:redis
  images
  build
  network
  ......
# 其他配置
volumes:
netowrks:
configs:
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容