[docker] docker compose 記錄

docker compose

批處理
默認名稱 docker-compose.yml
推薦使用Version3版本
圖爲docker-compose於docker版本配對
https://docs.docker.com/compose/compose-file/
image.png
V2和V3的區別:V2單機,V3多機python

Service

一個service表明一個container,container能夠從dockerhub的image建立,也能夠從本地Dockerfile build出來的image建立
Service啓動相似docker run,咱們能夠給其指定network和volumemysql

  • dockerhub的image建立
services:
  db:   #名稱
    image: postgres:9.4   #鏡像
    volumes:
      - "db-data:/var/lib/postgresql/data"  
    network:
      - back-tier

docker run -d --network back-tier -v db-data:/var/lib/postgresql/data postgres:9.4
docker volume create db-datalinux

  • 本地Dockerfile build出來的image建立
services:
  worker:   
    build: ./worker  # 本地構建dockfiles路徑
    links:
      - db
      - redis
    network:
      - back-tier # 若是使用自定義網絡,其實不須要links

源碼示例:git

version: '3'
services:
  wordpress:
    image: wordpress
    ports:
      - 8080:80
    enviroment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_PASSWORD: root
    networks:
      - my-bridge
      
  mysql:
    image: mysql
    enviroment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress
    volumes:
      - mysql-data:/var/lib/mysql
    networks:
      - my-bridge
volumes:
  mysql-data:
  
networks:
  mybridge:
    driver: bridge

 

docker compose 使用

docker-compose --version
linux用戶須要單獨安裝docker-compose
`
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-composegithub

sudo chmod +x /usr/local/bin/docker-composeweb

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
`
指定要啓動的yml文件
docker-compose -f docker-compose.yml up
不打印log後臺啓動
docker-compose up -d
查看compose狀態:
docker-compose ps
中止啓動compose:
docker-compose stop/start
刪除compose,image保留:
docker-compose down
查看compose 鏡像:
docker-compose images
進入mysql容器命令行:
docker-compose exec mysql bash
docker-compose exec wordpress bashredis

docker-compose實例sql

version: "3"
services:
  redis: redis
web:
  build:
    context:
    dockerfile: Dockerfile # 使用dockerfile去build
  ports:
    - 8080:5000
  enviroment:
    REDIS_HOST: redis

 

scale 擴展服務數量

yml文件中綁定本地端口不能有,刪除port
docker-compose up --scale web=3 -ddocker

配合scale添加負載均衡

docker-compose.yml實例flask

version: "3"
services:
  redis: 
    image: redis
  web:
    build:
      context:
      dockerfile: Dockerfile # 使用dockerfile去build
    enviroment:
      REDIS_HOST: redis
  lb:
    image: dockercloud/haproxy
    links:
      - web
    ports:
      - 8080:80
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock   
app.py

from flask import Flask
from redis import Redis
import os
import socket

app = Flask(__name__)
redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379)

@app.reoute('/')
def hello():
    redis.incr('hits')
    return 'Hello container world!'

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=80, debug=True)

 

Dockerfile

FROM pyton:2.7
LABEL maintaner="wyx"
COPY . /app
WORKDIR /app
RUN pip install flask redis
EXPOSE 80
CMD ["python", "app.py"]

 

docker-compose up -docker-compose up --scale web=3 -d

相關文章
相關標籤/搜索