Docker-Compose初体验

一、Docker Compose

1.Compose简介

我们之前的Docker流程是这样的

DockerFile–>build–>run 都是手动操作,单个容器!

思考:如果我们有100个、1000个微服务,我们也要一个个的手动操作嘛?

结果:为了解决这样的问题,我们引入一个Docker Compose 来轻松高效的管理容器—>定义运行多个容器

官方文档说明

Docker Compose is a tool that was developed to help define and running multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

  • define and share 定义,运行多个容器
  • YAML file 配置文件
  • single command 简单的命令

三步骤

1.Dockerfile保证我们的项目在任何地方可以运行

2.如何写docker-compose.yml

3.启动项目 docker-compose up

2.Compose理解

Compose是Docker官方的开源项目,需要安装!

Dockerfile 让程序在任何地方运行。

Compose:重要的概念

  • 服务services,容器。应用(web、redis、mysql)
  • 项目project。一组关联得到容器。

作用

批量容器编排

3.Compose安装

在这里插入图片描述

下载

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

在这里插入图片描述

授权

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

在这里插入图片描述

4.Compose体验

步骤一

官方地址:https://docs.docker.com/compose/gettingstarted/

1.Create a directory for the project

 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)

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

flask
redis

在这里插入图片描述

步骤二

编写一个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"]

在这里插入图片描述

步骤三

定义服务到Compose文件

Define services in a Compose file

Create a file called docker-compose.yml in your project directory and paste the following:

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

在这里插入图片描述

步骤四

Build and run your app with Compose

From your project directory, start up your application by running docker-compose up
在这里插入图片描述

总结

1.应用 app.py

2.Dockerfile 应用打包为镜像(单机)

3.Docker-compose.yml文件(定义整个服务,需要的环境。web、redis等)

4.启动compose 项目(docker-compose up)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值