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安裝

下載
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.pyin 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.txtin 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.ymlin 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/279951.html
標籤:其他
上一篇:Java面試進階指北

