1. 程式人生 > 其它 >docker進階使用

docker進階使用

Docker Compose

Docker Compose 進行批量容器編排,無需單獨啟動多個容器

安裝

官網的安裝

#官方下載
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#授權
chmod +x /usr/local/bin/docker-compose
# 加速下載
curl -L https://get.daocloud.io/docker/compose/releases/download/1.26.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
#授權
chmod +x /usr/local/bin/docker-compose

Compose配置變寫規則

官方文件

  • docker-compose.yaml核心(三層)
#1、版本號
version: "3.9"
#2、服務名
services:
#可以通過dockerfile構建映象
  web:
    build: .
    ports:
      - "5000:5000"
#可以通過官方映象引用
  redis:
    image: "redis:alpine"
#3、其他配置 網路/卷、全域性規則
volumes:
networks:
...

使用例子

  • 官方文件的快速開始

    1、建立對應資料夾並進入

     mkdir composetest
     cd composetest
    

    2、編寫app.py

    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)
    

    3、編寫python需匯入的包名的requirements.txt

    flask
    redis
    

    4、配置Dockerfile檔案將app.py打包成映象

    # syntax=docker/dockerfile:1
    FROM python:3.7-alpine
    #配置國內pip映象源
    RUN echo -e http://mirrors.ustc.edu.cn/alpine/v3.7/main/ > /etc/apk/repositories
    WORKDIR /code
    ENV FLASK_APP=app.py
    ENV FLASK_RUN_HOST=0.0.0.0
    COPY requirements.txt requirements.txt
    RUN pip install -r requirements.txt
    EXPOSE 5000
    COPY . .
    CMD ["flask", "run"]
    

    5、編寫docker-compose.yaml進行容器編排

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

    6、docker-compose up啟動,docker-compose up -d 後臺啟動

  • 使用docker-compose部署wp部落格

    官方文件

    具體步驟同官方快速開始例子

    [root@localhost home]# mkdir my_wordpress
    [root@localhost home]# cd my_wordpress/
    [root@localhost my_wordpress]# vi docker-compose.yml
    [root@localhost my_wordpress]# vi docker-compose.yml
    
    version: '3.3'
    
    services:
       db:
         image: mysql:5.7
         volumes:
           - db_data:/var/lib/mysql
         restart: always
         environment:
           MYSQL_ROOT_PASSWORD: somewordpress
           MYSQL_DATABASE: wordpress
           MYSQL_USER: wordpress
           MYSQL_PASSWORD: wordpress
    
       wordpress:
         depends_on:
           - db
         image: wordpress:latest
         ports:
           - "8000:80"
         restart: always
         environment:
           WORDPRESS_DB_HOST: db:3306
           WORDPRESS_DB_USER: wordpress
           WORDPRESS_DB_PASSWORD: wordpress
           WORDPRESS_DB_NAME: wordpress
    volumes:
        db_data: {}
    
    docker-compose up