其他分享
首页 > 其他分享> > Docker-第八课,进阶-Docker Compose

Docker-第八课,进阶-Docker Compose

作者:互联网

Docker Compose

简介:
Docker Compose 轻松高效的管理容器。定义运行多个容器。
官方介绍:
定义、运行多个容器
yaml file 配置文件
single command。命令有哪些?
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
所有的环境都可以使用Compose
Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
Using Compose is basically a three-step process:

  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.
    三步骤:
    1、Docker保证我们的项目可以运行在任何地方
    2、services什么是服务。docker-compose.yml这个文件怎么写
    3、启动项目

作用:批量容器编排。

理解:
compose是docker官方的开源项目。需要安装!
dockerfile让程序在任何地方运行。web服务。redis、mysql、nginx…多个容器。run

Compose

A docker-compose.yml looks like this:

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: {}

docker-compose up 100个服务
Compose:重要的概念。

安装Docker Compose
1、下载

方式一:GitHub下载
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
方式二:国内镜像下载
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、授权

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

在这里插入图片描述
3、体验(Getting started)
https://docs.docker.com/compose/gettingstarted/

Step 1:Setup
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)、创建一个requirements.txt文件,内容为:

flask
redis

Step 2:Create a Dockerfile

# 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"]

Step 3:Define services in a Compose file
创建一个docker-compose.yml文件

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

在这里插入图片描述

Docker Swarm

以集群的方式部署、4台阿里云服务器、2 4G。

Docker Stack

Docker Secret

Docker Config

容器单独没有什么意义,有意义的是容器编排!

k8s

未完待续。。。。。。

标签:compose,redis,第八课,Compose,services,Docker,docker
来源: https://blog.csdn.net/weixin_42997698/article/details/116111164