小知识:Docker批量容器编排的实现

简介

Docker

file build run 是手动操作单个容器,假如使用微服务架构,需要启动 100 + 个容器,他们之间的依赖关系如何维护?

Docker Compose 用来轻松高效地管理容器,定义运行多个容器。

三个步骤:

Dockerfile Services & docker-compose.yml docker-compose up

初体验

1.Dockerfile

?
1
2
3
4
5
6
7
8
9
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
COPY . .
CMD [“flask”, “run“]

2.Service

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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)

docker-compose.yml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
version: 3
services:
web:
build: .
ports:
– “5000:5000”
volumes:
– .:/code
– logvolume01:/var/log
links:
– redis
redis:
image: redis
volumes:
logvolume01: {}
docker-compose up
Starting compose-demo_web_1  … done
Starting compose-demo_redis_1 … done
Attaching to compose-demo_redis_1, compose-demo_web_1
redis_1 | 1:C 12 Sep 2020 07:34:09.654 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 12 Sep 2020 07:34:09.655 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 12 Sep 2020 07:34:09.655 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 12 Sep 2020 07:34:09.657 * Running mode=standalone, port=6379.
redis_1 | 1:M 12 Sep 2020 07:34:09.657 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 12 Sep 2020 07:34:09.657 # Server initialized
redis_1 | 1:M 12 Sep 2020 07:34:09.658 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add vm.overcommit_memory = 1 to /etc/sysctl.conf and then reboot or run the command sysctl vm.overcommit_memory=1 for this to take effect.
redis_1 | 1:M 12 Sep 2020 07:34:09.658 * Loading RDB produced by version 6.0.7
redis_1 | 1:M 12 Sep 2020 07:34:09.658 * RDB age 156 seconds
redis_1 | 1:M 12 Sep 2020 07:34:09.658 * RDB memory usage when created 0.77 Mb
redis_1 | 1:M 12 Sep 2020 07:34:09.658 * DB loaded from disk: 0.000 seconds
web_1  | * Serving Flask app “app.py”
web_1  | * Environment: production
web_1  |  WARNING: This is a development server. Do not use it in a production deployment.
web_1  |  Use a production WSGI server instead.
web_1  | * Debug mode: off
YML 文件规则
version: “1.0” #版本
services: #服务列表
service1:
#服务配置
container_name: #容器名称
depends_on: #依赖列表
– depend1
– depend2
images: #镜像
– image1
– image2
build:. #构建目录
network: #网络
……
service2: test2
……
volumnes: #挂载目录列表
networks: #网络列表
configs: #其他配置

到此这篇关于Docker批量容器编排的实现的文章就介绍到这了,更多相关Docker批量容器编排内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://learnku.com/articles/49114

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Docker部署Mysql集群的实现

2023-3-27 16:59:28

建站知识

小知识:docker 安装solr8.6.2 配置中文分词器的方法

2023-3-27 17:14:09

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索