什么是compose
- 是docker官方推出的一个容器编排工具
- 通过compose,你可以使用yml文件来配置应用程序需要的所有服务,然后,使用一个命令,就可以从yml文件配置中创建并启动所有服务。
同样的工具还有swarm和k8s,这里就不展开讲了,之后详细介绍
对容器进行什么编排操作呢?
- 像启动容器,指定端口,卷,链接,使用哪个镜像等
- 像我们一开始学的,docker run,docker container这些命令行命令,我们实际工作中用的很少,可以替代它
对多少容器进行编排操作呢?
对多少台宿主机上的容器进行编排操作?
个人理解
- compose可以理解为启动容器的脚本,在脚本里指明启动容器的顺序,启动多少容器等。
compose的好处
下载docker compose 1.29.2
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
查看compose版本
[root@yihelinux bin]
docker-compose version 1.29.2, build 5becea4c
[root@yihelinux bin]
给compose 添加可执行权限
[root@yihelinux bin]
操作一下compose
[root@yihelinux lianxi]
[root@yihelinux lianxi]
[root@yihelinux composetest]
[root@yihelinux composetest]
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)
[root@yihelinux composetest]
[root@yihelinux composetest]
flask
redis
[root@yihelinux composetest]
[root@yihelinux composetest]
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"]
[root@yihelinux composetest]
编写docker-compose.yml
https://www.runoob.com/w3cnote/yaml-intro.html
[root@yihelinux composetest]
[root@yihelinux composetest]
docker-compose.yml内容如下:
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
启动docker-compose
[root@yihelinux composetest]
Creating network "composetest_default" with the default driver
Building web
Sending build context to Docker daemon 5.632kB
Step 1/10 : FROM python:3.7-alpine
测试效果
[root@yihelinux ~]
Hello World! I have been seen 1 times.
[root@yihelinux ~]
Hello World! I have been seen 2 times.
[root@yihelinux ~]
Hello World! I have been seen 3 times.
[root@yihelinux ~]
Hello World! I have been seen 4 times.
[root@yihelinux ~]
Hello World! I have been seen 5 times.
[root@yihelinux ~]
compose常用命令
- docker-compose up
- docker-compose down
- docker-compose stop
- docker-compose run
|