docker-compose簡單使用


1,下載安裝docker-compose

# http://get.daocloud.io/
curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

2,官網案例

#1
$ mkdir composetest
$ cd composetest

#2 Create a file called app.py in your project directory and paste this in

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)



#3Create another file called requirements.txt in your project directory and paste this in:
flask
redis



#4create a file named Dockerfile and paste the following
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"]


#5Create a file called docker-compose.yml

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

#6啟動
$ docker-compose up

#7修改docker-compose.yml
version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/code
    environment:
      FLASK_ENV: development
  redis:
    image: "redis:alpine"


#7 查看變量
$ docker-compose run web env


## 
停止服務
$ docker-compose stop

刪除所有數據
$ docker-compose down --volumes



docker-compose 配置文件 v3

1,構建方式

1) 指定構建dockerfile在當前目錄下

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

  1. 指定構建上下文 ,構建鏡像時的必須文件都要在該目錄下 ./dir
version: "3.7"
services:
  webapp:
    build: ./dir
    image: webapp:tag1  
#指定鏡像名稱

3)指定dockerfile名稱及變量

version: "3.7"
services:
  webapp:
    build:
      context: ./dir
      dockerfile: Dockerfile-alternate
      args:
        buildno: 1

注意:不指定 dockerfile: 時默認的dockerfile為 Dockerfile

參數格式

#1
build:
  context: .
  args:
    buildno: 1
    gitcommithash: cdc3b19

#2

build:
  context: .
  args:
    - buildno=1
    - gitcommithash=cdc3b19



為鏡像添加標簽 docker-compose 版本為3.3

version: '3.3'
services:
  web:
    build: 
      context: ./dir
      labels:
         - "com.example.description=Accounting webapp"
    ports:
      - "5000:5000"
    volumes:
      - .:/code
    environment:
      FLASK_ENV: development
  redis:
    image: "redis:alpine"


查看標簽:
docker  inspect composetest_web | grep  -i  -A  3  labels 

命令

Override the default command.   重寫dockerfile 內的命令

command: bundle exec thin -p 3000
或
command: ["bundle", "exec", "thin", "-p", "3000"]

配置configs

depends_on

官網介紹
Express dependency between services, Service dependencies cause the following behaviors:

    docker-compose up starts services in dependency order. In the following example, db and redis are started before web.

    docker-compose up SERVICE automatically includes SERVICE’s dependencies. In the following example, docker-compose up web also creates and starts db and redis.

    docker-compose stop stops services in dependency order. In the following example, web is stopped before db and redis.
version: "3.7"
services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres

deploy

Specify configuration related to the deployment and running of services. This only takes effect when deploying to a swarm with docker stack deploy, and is ignored by docker-compose up and docker-compose run.

version: "3.7"
services:
  redis:
    image: redis:alpine
    deploy:
      replicas: 6
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

ENDPOINT_MODE

為連接到群集的外部客戶端指定服務發現方法

endpoint_mode: vip - Docker assigns the service a virtual IP (VIP) that acts as the front end for clients to reach the service on a network. Docker routes requests between the client and available worker nodes for the service, without client knowledge of how many nodes are participating in the service or their IP addresses or ports. (This is the default.)

endpoint_mode: dnsrr - DNS round-robin (DNSRR) service discovery does not use a single virtual IP. Docker sets up DNS entries for the service such that a DNS query for the service name returns a list of IP addresses, and the client connects directly to one of these. DNS round-robin is useful in cases where you want to use your own load balancer, or for Hybrid Windows and Linux applications.

endpoint_mode:vip-Docker為服務分配虛擬IP(VIP),該IP作為客戶端訪問網絡上服務的前端。 Docker在客戶端和服務的可用工作節點之間路由請求,而無需客戶端知道有多少節點正在參與服務或其IP地址或端口。 (這是默認設置。)
endpoint_mode:dnsrr-DNS輪詢(DNSRR)服務發現不使用單個虛擬IP。 Docker設置服務的DNS條目,以便對服務名稱的DNS查詢返回IP地址列表,並且客戶端直接連接到其中之一。在想要使用自己的負載平衡器或混合Windows和Linux應用程序的情況下,DNS輪詢很有用。

version: "3.7"

services:
  wordpress:
    image: wordpress
    ports:
      - "8080:80"
    networks:
      - overlay
    deploy:
      mode: replicated
      replicas: 2
      endpoint_mode: vip

  mysql:
    image: mysql
    volumes:
       - db-data:/var/lib/mysql/data
    networks:
       - overlay
    deploy:
      mode: replicated
      replicas: 2
      endpoint_mode: dnsrr

volumes:
  db-data:

networks:
  overlay:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM