Docker-Compose初体验

Docker Compose

  • 一、Docker Compose
      • 1.Compose简介
      • 2.Compose理解
      • 3.Compose安装
      • 4.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安装

Docker-Compose初体验_第1张图片

下载

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

Docker-Compose初体验_第2张图片

授权

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

Docker-Compose初体验_第3张图片

4.Compose体验

步骤一

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

1.Create a directory for the project

 mkdir composetest
 cd composetest

Docker-Compose初体验_第4张图片

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

Docker-Compose初体验_第5张图片

步骤二

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

Docker-Compose初体验_第6张图片

步骤三

定义服务到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"

Docker-Compose初体验_第7张图片

步骤四

Build and run your app with Compose

From your project directory, start up your application by running docker-compose up
Docker-Compose初体验_第8张图片

总结

1.应用 app.py

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

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

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

你可能感兴趣的:(java,笔记,docker,java)