亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

Compose是一個用于定義和運(yùn)行多容器Docker應(yīng)用程序的工具。使用Compose,您可以使用YAML文件來配置應(yīng)用程序的服務(wù)。然后,只需一個命令,就可以從配置中創(chuàng)建并啟動所有服務(wù)。

Install Docker Compose

  1. 下載docker compose
$  sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  1. 給docker compose設(shè)置可執(zhí)行權(quán)限
$ sudo chmod +x /usr/local/bin/docker-compose
  1. 驗證
$ docker-compose --version

Uninstallation

$ sudo rm /usr/local/bin/docker-compose

Getting Started

用Python構(gòu)建一個簡易網(wǎng)頁統(tǒng)計網(wǎng)頁點擊量,docker-compose進(jìn)行發(fā)布

Step1:創(chuàng)建項目

  1. 創(chuàng)建項目目錄
  2. $ mkdir test_web
    $ cd test_web
  3. 在項目目錄中創(chuàng)建App.py文件,并把下面代碼復(fù)制進(jìn)去
  4. 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)
  5. 創(chuàng)建requirements.txt文件,以下內(nèi)容復(fù)制進(jìn)去
flask
redis

Step2:創(chuàng)建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
EXPOSE 5000
COPY . .
CMD ["flask", "run"]

Step3:在docker-compose.yml中定義services

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

Step4:用Docker compose構(gòu)建和運(yùn)行app

  1. 進(jìn)入項目目錄,運(yùn)行docker-compose up
  2. $ docker-compose up
  3. 在瀏覽器訪問http://localhost:5000/ ,刷新頁面看變化
  4. 查看使用compose構(gòu)建的鏡像
  5. $ docker images

Step5:綁定一個數(shù)據(jù)卷

version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/code
    environment:
      FLASK_ENV: development
  redis:
    image: "redis:alpine"

將當(dāng)前目錄與容器的/code目錄綁定,這樣可以動態(tài)修改代碼

Step6:重新構(gòu)建和運(yùn)行app

先docker-compose down停止服務(wù),在構(gòu)建

$ docker-compose down
$ docker-compose up

Compose file

用YAML文件定義服務(wù),默認(rèn)文件是docker-compose.yml,包含4個頂級key,version、services、networks、volumes

參考compose-spec/spec.md at master · compose-spec/compose-spec · GitHub

version

指定本 yml 依從的 compose版本

services

定義多個應(yīng)用服務(wù),包含環(huán)境配置、鏡像構(gòu)建等

build

指定構(gòu)建鏡像的路徑

version: "3.9"
services:
  webapp:
    build: ./app

blkio_config

定義服務(wù)的block IO配置,參考compose-spec/spec.md at master · compose-spec/compose-spec · GitHub

container_name

指定自定義容器名稱

depends_on

定義服務(wù)間啟動或關(guān)閉的依賴關(guān)系

services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres

command

覆蓋容器啟動的默認(rèn)命令

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

domainname

domainname declares a custom domain name to use for the service container.

entrypoint

覆蓋容器默認(rèn)的entrypoint

env_file

從文件中添加環(huán)境變量到容器,可以是一個或多個文件

env_file: .env
env_file:
  - ./a.env
  - ./b.env

文件格式:

# Set Rails/Rack environment
RACK_ENV=development
VAR="quoted"

environment

添加環(huán)境變量

environment:
  RACK_ENV: development
  SHOW: "true"
  USER_INPUT:

expose

暴露端口,但不映射到宿主機(jī),只被連接的服務(wù)訪問,僅可以指定內(nèi)部端口

expose:
  - "3000"
  - "8000"

healthcheck

用于檢測 docker 服務(wù)是否健康運(yùn)行。

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost"] # 設(shè)置檢測程序
  interval: 1m30s  # 設(shè)置檢測間隔
  timeout: 10s # 設(shè)置檢測超時時間
  retries: 3 # 設(shè)置重試次數(shù)
  start_period: 40s # 啟動后,多少秒開始啟動檢測程序

image

指定容器運(yùn)行的鏡像

image: redis:5

labels

設(shè)置容器標(biāo)簽

labels:
  com.example.description: "Accounting webapp"
  com.example.department: "Finance"
labels:
  - "com.example.description=Accounting webapp"
  - "com.example.department=Finance"

links

連接到另一個容器的網(wǎng)絡(luò),簡單將就是讓容器相互連通

web:
  links:
    - db
    - db:database
    - redis

logging

服務(wù)的日志記錄配置,driver:指定服務(wù)容器的日志記錄驅(qū)動程序,默認(rèn)值為json-file。有以下三個選項

driver: "json-file"
driver: "syslog"
driver: "none"

僅在 json-file 驅(qū)動程序下,可以使用以下參數(shù),限制日志得數(shù)量和大小。

logging:
  driver: json-file
  options:
    max-size: "200k" # 單個文件大小為200k
    max-file: "10" # 最多10個文件

syslog 驅(qū)動程序下,可以使用 syslog-address 指定日志接收地址。

logging:
  driver: syslog
  options:
    syslog-address: "tcp://192.168.0.42:123"

network_mode

設(shè)置網(wǎng)絡(luò)模式,格式如下:

network_mode: "bridge" #橋接模式
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"

networks

配置容器連接的網(wǎng)絡(luò)

services:
  some-service:
    networks:
      - some-network
      - other-network
networks:
  some-network:
    # Use a custom driver
    driver: custom-driver-1
  other-network:
    # Use a custom driver which takes special options
    driver: custom-driver-2
services:
  frontend:
    image: awesome/webapp
    networks:
      - front-tier
      - back-tier

  monitoring:
    image: awesome/monitoring
    networks:
      - admin

  backend:
    image: awesome/backend
    networks:
      back-tier:
        aliases:
          - database
      admin:
        aliases:
          - MySQL

networks:
  front-tier:
  back-tier:
  admin:

ipv4_address, ipv6_address

指定ip地址

services:
  frontend:
    image: awesome/webapp
    networks:
      front-tier:
        ipv4_address: 172.16.238.10
        ipv6_address: 2001:3984:3989::10

networks:
  front-tier:
    ipam:
      driver: default
      config:
        - subnet: "172.16.238.0/24"
        - subnet: "2001:3984:3989::/64"

ports

端口映射,映射主機(jī)與容器端口,格式:Host:ontainer

ports:
     - "5000:5000"

restart

容器重啟策略

restart: "no"
restart: always
restart: on-failure
restart: unless-stopped

secrets

存儲敏感數(shù)據(jù),比如密碼

services:
  frontend:
    image: awesome/webapp
    secrets:
      - server-certificate
secrets:
  server-certificate:
    file: ./server.cert

volumes

將主機(jī)數(shù)據(jù)卷掛載到容器

services:
  db:
    image: postgres:latest
    volumes:
      - "/localhost/postgres.sock:/var/run/postgres/postgres.sock"
      - "/localhost/data:/var/lib/postgresql/data"

working_dir

覆蓋容器工作目錄

Volumes 頂級目錄

services:
  backend:
    image: awesome/database
    volumes:
      - db-data:/etc/data

  backup:
    image: backup-service
    volumes:
      - db-data:/var/lib/backup/data

volumes:
  db-data:

Networks 頂級目錄

services:
  frontend:
    image: awesome/webapp
    networks:
      - front-tier
      - back-tier

networks:
  front-tier:
  back-tier:
    driver: bridge

docker-compose 命令

$ docker-compose --help
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [--profile <name>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  --profile NAME              Specify a profile to enable
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert deploy
                              keys in v3 files to their non-Swarm equivalent

Commands:
  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

Swarm集群

分享到:
標(biāo)簽:Docker Compose
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定