-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_3_docker_compose.html
593 lines (425 loc) · 33.3 KB
/
docker_3_docker_compose.html
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
<meta name="baidu-site-verification" content="codeva-PBoQMHcqc8" />
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
<link rel="mask-icon" href="/images/logo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"lfeng.tech","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
</script>
<meta name="description" content="上一篇文章介绍了docker镜像的制作与发布,本文主要介绍实际docker工程部署中经常用到的docker-compose工具,以及docker的网络配置和负载均衡。 Docker-compose介绍实际开发过程中,在一个项目中,我们常将不同的模块放在单独的docker中,方便维护和扩展。比如我们一个项目可能有MySQL镜像、Nginx镜像、Spring Boot后端镜像,我们在实际部署中可以采">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker系列-(3) Docker-compose使用与负载均衡">
<meta property="og:url" content="https://lfeng.tech/docker_3_docker_compose.html">
<meta property="og:site_name" content="Vincent's Notes">
<meta property="og:description" content="上一篇文章介绍了docker镜像的制作与发布,本文主要介绍实际docker工程部署中经常用到的docker-compose工具,以及docker的网络配置和负载均衡。 Docker-compose介绍实际开发过程中,在一个项目中,我们常将不同的模块放在单独的docker中,方便维护和扩展。比如我们一个项目可能有MySQL镜像、Nginx镜像、Spring Boot后端镜像,我们在实际部署中可以采">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://imgs.lfeng.tech/images/blog/20221211223340_eeacd.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/a18449c6gy1g9br9bt5dnj20iz0pmwja.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/large/a18449c6gy1g9bsyru8emj20rs0dwaaz.jpg">
<meta property="og:image" content="https://imgs.lfeng.tech/images/blog/20221211223340_0a4df.jpg">
<meta property="article:published_time" content="2019-11-26T23:34:49.000Z">
<meta property="article:modified_time" content="2022-12-11T14:33:41.036Z">
<meta property="article:author" content="Vincent">
<meta property="article:tag" content="Docker">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://imgs.lfeng.tech/images/blog/20221211223340_eeacd.jpg">
<link rel="canonical" href="https://lfeng.tech/docker_3_docker_compose.html">
<script id="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : false,
isPost : true,
lang : 'zh-CN'
};
</script>
<title>Docker系列-(3) Docker-compose使用与负载均衡 | Vincent's Notes</title>
<noscript>
<style>
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
</head>
<body itemscope itemtype="http://schema.org/WebPage">
<div class="container use-motion">
<div class="headband"></div>
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="切换导航栏">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<h1 class="site-title">Vincent's Notes</h1>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
</div>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="main-menu menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>
</li>
</ul>
</nav>
</div>
</header>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<main class="main">
<div class="main-inner">
<div class="content-wrap">
<div class="content post posts-expand">
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://lfeng.tech/docker_3_docker_compose.html">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.jpg">
<meta itemprop="name" content="Vincent">
<meta itemprop="description" content="Vincent's tech blog">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Vincent's Notes">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
Docker系列-(3) Docker-compose使用与负载均衡
</h1>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2019-11-26 23:34:49" itemprop="dateCreated datePublished" datetime="2019-11-26T23:34:49Z">2019-11-26</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2022-12-11 14:33:41" itemprop="dateModified" datetime="2022-12-11T14:33:41Z">2022-12-11</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Linux/" itemprop="url" rel="index"><span itemprop="name">Linux</span></a>
</span>
</span>
<span id="/docker_3_docker_compose.html" class="post-meta-item leancloud_visitors" data-flag-title="Docker系列-(3) Docker-compose使用与负载均衡" title="阅读次数">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-comment"></i>
</span>
<span class="post-meta-item-text">Valine:</span>
<a title="valine" href="/docker_3_docker_compose.html#valine-comments" itemprop="discussionUrl">
<span class="post-comments-count valine-comment-count" data-xid="/docker_3_docker_compose.html" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p><img src="https://imgs.lfeng.tech/images/blog/20221211223340_eeacd.jpg" alt="docker.png"></p>
<p>上一篇文章介绍了docker镜像的制作与发布,本文主要介绍实际docker工程部署中经常用到的docker-compose工具,以及docker的网络配置和负载均衡。</p>
<h2 id="Docker-compose介绍"><a href="#Docker-compose介绍" class="headerlink" title="Docker-compose介绍"></a>Docker-compose介绍</h2><p>实际开发过程中,在一个项目中,我们常将不同的模块放在单独的docker中,方便维护和扩展。比如我们一个项目可能有MySQL镜像、Nginx镜像、Spring Boot后端镜像,我们在实际部署中可以采用上篇文章介绍的方法,分别打包,分别启动;但是这样太费精力,而且还容易出错。<br>因此Docker-compose工具应运而生,就是为了解决工程部署中的多个Docker镜像的管理问题。</p>
<h3 id="docker-compose-yaml-文件"><a href="#docker-compose-yaml-文件" class="headerlink" title="docker-compose.yaml 文件"></a>docker-compose.yaml 文件</h3><p>类似于build镜像需要Dockerfile一样,使用docker-compose时也需要类似的配置文件,叫做docker-compose.yaml。</p>
<span id="more"></span>
<p>下面是一个Docker-file的例子:</p>
<img src="http://ww1.sinaimg.cn/mw690/a18449c6gy1g9br9bt5dnj20iz0pmwja.jpg" width="400">
<p>一般docker-compose文件中需要注意如下几点:</p>
<ul>
<li>version: ‘3’: 表示使用第三代语法,每代的语法稍有不同,我们选择最新的就好。</li>
<li>services: 表示compose需要启动的服务,一般一个docker镜像就可以作为一个单独的service。</li>
<li>container_name: 容器名称</li>
<li>environment: 容器环境变量,可以使用这个参数往容器传递一些变量,这个在我们切换不同环境配置时非常有用,不用每次去改变代码中的配置</li>
<li>ports: 对外开放的端口</li>
<li>restart: always表示如果服务启动不成功一直尝试。</li>
<li>volumes: 加载本地目录到容器目标路径,也就是将本地路径共享给容器,方便进行数据交互,也可以利用这个功能,将docker的log输出到本机指定的位置。</li>
<li>depends_on:依赖服务,先启动 depends_on 服务。</li>
<li>command: mvn clean spring-boot:run : 表示以这个命令来启动项目</li>
</ul>
<p>上面提到了depends_on,通常用于需要顺序启动不同镜像,比如后台需要等待数据库初始化完毕以后才启动,但是这个depends_on并不能保证前序容器完全初始化,意思是只要前序容器启动了,就会尝试启动当前容器。这样还是会造成前序容器未初始化完成,后续容器启动后出现连接失败等错误。</p>
<p>因此最好的方式是官方(<a target="_blank" rel="noopener" href="https://docs.docker.com/compose/startup-order/)%E6%AF%94%E8%BE%83%E6%8E%A8%E8%8D%90%E7%9A%84wait%E8%84%9A%E6%9C%AC%EF%BC%8C%E8%84%9A%E6%9C%AC%E7%9A%84%E8%AF%A6%E7%BB%86%E4%BD%BF%E7%94%A8%E5%8F%AF%E4%BB%A5%E7%9C%8B%E8%BF%99%E9%87%8C">https://docs.docker.com/compose/startup-order/)比较推荐的wait脚本,脚本的详细使用可以看这里</a> <a target="_blank" rel="noopener" href="https://github.com/vishnubob/wait-for-it">https://github.com/vishnubob/wait-for-it</a>,</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">wait-for-it.sh host:port [-s] [-t timeout] [-- command args]</span><br><span class="line">-h HOST | --host=HOST Host or IP under test</span><br><span class="line">-p PORT | --port=PORT TCP port under test</span><br><span class="line"> Alternatively, you specify the host and port as host:port</span><br><span class="line">-s | --strict Only execute subcommand if the test succeeds</span><br><span class="line">-q | --quiet Don't output any status messages</span><br><span class="line">-t TIMEOUT | --timeout=TIMEOUT</span><br><span class="line"> Timeout in seconds, zero for no timeout</span><br><span class="line">-- COMMAND ARGS Execute command with args after the test finishes</span><br></pre></td></tr></table></figure>
<p>一般也可以在制作镜像的时候指定等待一定时间,在下面的例子中,在运行test.jar之前,会一直等到elasticsearch初始化完成。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">FROM openjdk:8</span><br><span class="line">ADD ["target/test-1.0.0.jar", "test.jar"]</span><br><span class="line">ADD ["wait-for-it.sh", "wait-for-it.sh"]</span><br><span class="line">RUN ["chmod", "+x", "wait-for-it.sh"]</span><br><span class="line">EXPOSE 1234</span><br><span class="line">ENTRYPOINT ["./wait-for-it.sh", "elastic:9200", "--", "java", "-jar", "/test.jar"]</span><br></pre></td></tr></table></figure>
<blockquote>
<p>关于更多docker-compose文件的详细介绍,可以参考官方的文档 <a target="_blank" rel="noopener" href="https://docs.docker.com/compose/gettingstarted/%E3%80%82">https://docs.docker.com/compose/gettingstarted/。</a></p>
</blockquote>
<h3 id="docker-compose的启动-停止"><a href="#docker-compose的启动-停止" class="headerlink" title="docker-compose的启动/停止"></a>docker-compose的启动/停止</h3><p>docker-compose的启动与停止非常简单,直接使用<code>docker-compose up</code>启动,<code>docker-compose down</code>关闭工程,同时会删除所有容器。</p>
<h2 id="Docker的网络通信"><a href="#Docker的网络通信" class="headerlink" title="Docker的网络通信"></a>Docker的网络通信</h2><p>docker的网络方面是相对较弱的,新手如果不清楚,经常会出现为什么容器A访问不到容器B的问题。</p>
<h3 id="容器间的通信"><a href="#容器间的通信" class="headerlink" title="容器间的通信"></a>容器间的通信</h3><p>有个很常见的场景,我们需要在本机同时调试前端和后端程序,通常我们会在localhost某个端口启动前端,比如8080;同时在local的另一端口比如1234启动后端。如果不用容器,前端直接在代码里访问localhost:1234的所有接口都是可以的,没有任何问题。</p>
<p>但是如果前端和后端都封装在不同的容器中,这个时候前端直接访问localhost:1234,会报错,无法访问。原因就是两个容器之间是独立的,无法直接localhost访问。</p>
<p>官方推荐的解决办法就是,先新建一个网络,然后启动前后端容器的时候,分别指定在该网络中运行。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker network create mynet</span><br><span class="line">docker run --network mynet --name frontend ...</span><br><span class="line">docker run --network mynet --name backend ...</span><br></pre></td></tr></table></figure>
<p>现在前端就可以通过 <a target="_blank" rel="noopener" href="http://backend:1234/">http://backend:1234</a> 访问后端的接口了。</p>
<blockquote>
<p>注意:有些博客指出,可以通过<code>docker inspect [container_name]</code>来获取后端容器的ip,然后直接访问ip加端口就可以了。这个在Linux上也许可行,但是mac下由于容器的实现原理不同,无法直接通过ip访问。</p>
</blockquote>
<h3 id="docker-compose下的网络环境"><a href="#docker-compose下的网络环境" class="headerlink" title="docker-compose下的网络环境"></a>docker-compose下的网络环境</h3><p>默认情况下,当你利用docker-compose启动多个容器时,docker-compose会自动帮你创建一个网络,并将多个service在这个网络中运行。因此不同容器之间,直接利用容器名+端口就可以访问了。</p>
<h2 id="Docker-compose实现负载均衡"><a href="#Docker-compose实现负载均衡" class="headerlink" title="Docker-compose实现负载均衡"></a>Docker-compose实现负载均衡</h2><p>实际生产环境中,肯定会启动多个后端service,同时进行负载均衡以保证服务质量。利用docker-compose我们可以方便的实现这种功能。</p>
<img src="http://ww1.sinaimg.cn/large/a18449c6gy1g9bsyru8emj20rs0dwaaz.jpg" width="700">
<p>下面是一个很简单的docker-compose文件,启动之后可以在本地打开localhost:5000,来访问后端的service;在front容器内部,我们也可以通过backend:5000来访问backend service。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">version: "3"</span><br><span class="line"></span><br><span class="line">services:</span><br><span class="line"> front:</span><br><span class="line"> image: front:latest</span><br><span class="line"> backend:</span><br><span class="line"> image: "backend:latest"</span><br><span class="line"> ports:</span><br><span class="line"> - "5000:5000"</span><br></pre></td></tr></table></figure>
<p>为了启动多个后端程序以实现负载均衡,docker-compose已经为我们提供<code>--scale</code>参数,利用这个参数可以轻松实现同时启动多个service。<br>下面是该参数的使用说明。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">--scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the</span><br><span class="line"> `scale` setting in the Compose file if present.</span><br></pre></td></tr></table></figure>
<p>在这里,我们直接在启动的时候,指定生成3个后端的service,</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker-compose up --scale backend=3</span><br></pre></td></tr></table></figure>
<p>但是这样会报错,因为本机上的5000端口,只能分配给一个service,如果你想同时在本机上调试,可以将上面的ports改为5000,修改后的docker-compose文件如下:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">version: "3"</span><br><span class="line"></span><br><span class="line">services:</span><br><span class="line"> front:</span><br><span class="line"> image: front:latest</span><br><span class="line"> backend:</span><br><span class="line"> image: "backend:latest"</span><br><span class="line"> ports:</span><br><span class="line"> - "5000"</span><br></pre></td></tr></table></figure>
<p>这样按照上面同样的指令启动之后,你你可以看到如下的输出结果,有3个不同的本机端口绑定到容器的5000端口,</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"> Name Command State Ports</span><br><span class="line">--------------------------------------------------------------------------------------------</span><br><span class="line">example_backend_1 /usr/bin/dumb-init -- /sbi ... Up (healthy) 0.0.0.0:32776->5000/tcp</span><br><span class="line">example_backend_2 /usr/bin/dumb-init -- /sbi ... Up (healthy) 0.0.0.0:32775->5000/tcp</span><br><span class="line">example_backend_3 /usr/bin/dumb-init -- /sbi ... Up (healthy) 0.0.0.0:32777->5000/tcp</span><br></pre></td></tr></table></figure>
<p>当然,如果你只想从容器内部访问后端的5000端口,你可以去掉port配置,改成如下的配置:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">version: "3"</span><br><span class="line"></span><br><span class="line">services:</span><br><span class="line"> front:</span><br><span class="line"> image: front:latest</span><br><span class="line"> backend:</span><br><span class="line"> image: "backend:latest"</span><br></pre></td></tr></table></figure>
<p>这样<code>docker-compose up --scale backend=3</code>启动后,我们就只能在容器内部访问<code>http:\\backend:5000</code>来访问后端service,因为我们启动了三个,前端的请求会随机的分配到后端的三个service上,保证了整体的服务质量。</p>
<p>参考链接:</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://stackoverflow.com/questions/48062134/connection-refused-with-two-microservices-in-docker">https://stackoverflow.com/questions/48062134/connection-refused-with-two-microservices-in-docker</a></li>
<li><a target="_blank" rel="noopener" href="https://pspdfkit.com/blog/2018/how-to-use-docker-compose-to-run-multiple-instances-of-a-service-in-development/">https://pspdfkit.com/blog/2018/how-to-use-docker-compose-to-run-multiple-instances-of-a-service-in-development/</a></li>
</ul>
<hr>
<p>搜索『后端精进之路』并关注,立刻获取最新文章和<strong>价值2000元的BATJ精品面试课程</strong>。</p>
<p><img src="https://imgs.lfeng.tech/images/blog/20221211223340_0a4df.jpg" alt="后端精进之路.png"></p>
</div>
<div>
<ul class="post-copyright">
<li class="post-copyright-author">
<strong>本文作者: </strong>Vincent
</li>
<li class="post-copyright-link">
<strong>本文链接:</strong>
<a href="https://lfeng.tech/docker_3_docker_compose.html" title="Docker系列-(3) Docker-compose使用与负载均衡">https://lfeng.tech/docker_3_docker_compose.html</a>
</li>
<li class="post-copyright-license">
<strong>版权声明: </strong>本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处!
</li>
</ul>
</div>
<footer class="post-footer">
<div class="post-tags">
<a href="/tags/Docker/" rel="tag"># Docker</a>
</div>
<div class="post-nav">
<div class="post-nav-item">
<a href="/docker_2_create_image.html" rel="prev" title="Docker系列-(2) 镜像制作与发布">
<i class="fa fa-chevron-left"></i> Docker系列-(2) 镜像制作与发布
</a></div>
<div class="post-nav-item">
<a href="/Java_concurrent_1_basic_knowledge.html" rel="next" title="Java并发编程系列-(1) 并发编程基础">
Java并发编程系列-(1) 并发编程基础 <i class="fa fa-chevron-right"></i>
</a></div>
</div>
</footer>
</article>
</div>
<div class="comments" id="valine-comments"></div>
<script>
window.addEventListener('tabs:register', () => {
let { activeClass } = CONFIG.comments;
if (CONFIG.comments.storage) {
activeClass = localStorage.getItem('comments_active') || activeClass;
}
if (activeClass) {
let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
if (activeTab) {
activeTab.click();
}
}
});
if (CONFIG.comments.storage) {
window.addEventListener('tabs:click', event => {
if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
let commentClass = event.target.classList[1];
localStorage.setItem('comments_active', commentClass);
});
}
</script>
</div>
<div class="toggle sidebar-toggle">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
<aside class="sidebar">
<div class="sidebar-inner">
<ul class="sidebar-nav motion-element">
<li class="sidebar-nav-toc">
文章目录
</li>
<li class="sidebar-nav-overview">
站点概览
</li>
</ul>
<!--noindex-->
<div class="post-toc-wrap sidebar-panel">
<div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Docker-compose%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.</span> <span class="nav-text">Docker-compose介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#docker-compose-yaml-%E6%96%87%E4%BB%B6"><span class="nav-number">1.1.</span> <span class="nav-text">docker-compose.yaml 文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#docker-compose%E7%9A%84%E5%90%AF%E5%8A%A8-%E5%81%9C%E6%AD%A2"><span class="nav-number">1.2.</span> <span class="nav-text">docker-compose的启动/停止</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Docker%E7%9A%84%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1"><span class="nav-number">2.</span> <span class="nav-text">Docker的网络通信</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%B9%E5%99%A8%E9%97%B4%E7%9A%84%E9%80%9A%E4%BF%A1"><span class="nav-number">2.1.</span> <span class="nav-text">容器间的通信</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#docker-compose%E4%B8%8B%E7%9A%84%E7%BD%91%E7%BB%9C%E7%8E%AF%E5%A2%83"><span class="nav-number">2.2.</span> <span class="nav-text">docker-compose下的网络环境</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Docker-compose%E5%AE%9E%E7%8E%B0%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1"><span class="nav-number">3.</span> <span class="nav-text">Docker-compose实现负载均衡</span></a></li></ol></div>
</div>
<!--/noindex-->
<div class="site-overview-wrap sidebar-panel">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image" alt="Vincent"
src="/images/avatar.jpg">
<p class="site-author-name" itemprop="name">Vincent</p>
<div class="site-description" itemprop="description">Vincent's tech blog</div>
</div>
<div class="site-state-wrap motion-element">
<nav class="site-state">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">65</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-categories">
<a href="/categories/">
<span class="site-state-item-count">15</span>
<span class="site-state-item-name">分类</span></a>
</div>
<div class="site-state-item site-state-tags">
<a href="/tags/">
<span class="site-state-item-count">23</span>
<span class="site-state-item-name">标签</span></a>
</div>
</nav>
</div>
<div class="links-of-author motion-element">
<span class="links-of-author-item">
<a href="https://github.com/teckee" title="GitHub → https://github.com/teckee" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
</span>
<span class="links-of-author-item">
<a href="mailto:h0ck@foxmail.com" title="E-Mail → mailto:h0ck@foxmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
</span>
</div>
</div>
</div>
</aside>
<div id="sidebar-dimmer"></div>
</div>
</main>
<footer class="footer">
<div class="footer-inner">
<div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">沪ICP备19042895号-2 </a>
</div>
<div class="copyright">
©
<span itemprop="copyrightYear">2023</span>
<span class="with-love">
<i class="fa fa-heart"></i>
</span>
<span class="author" itemprop="copyrightHolder">Vincent</span>
</div>
<div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
</div>
<div class="busuanzi-count">
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
<span class="post-meta-item-icon">
<i class="fa fa-user"></i>
</span>
<span class="site-uv" title="总访客量">
<span id="busuanzi_value_site_uv"></span>
</span>
</span>
<span class="post-meta-divider">|</span>
<span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="site-pv" title="总访问量">
<span id="busuanzi_value_site_pv"></span>
</span>
</span>
</div>
</div>
</footer>
</div>
<script src="/lib/anime.min.js"></script>
<script src="/lib/velocity/velocity.min.js"></script>
<script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script>
<script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>
<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
var GUEST = ['nick', 'mail', 'link'];
var guest = 'nick,mail,link';
guest = guest.split(',').filter(item => {
return GUEST.includes(item);
});
new Valine({
el : '#valine-comments',
verify : false,
notify : false,
appId : 'cgQ905Fj6xbazWtyi9b1hafr-gzGzoHsz',
appKey : '68EFTwnd9XYY352gRUJlJyMu',
placeholder: "Just go go",
avatar : 'mm',
meta : guest,
pageSize : '10' || 10,
visitor : true,
lang : '' || 'zh-cn',
path : location.pathname,
recordIP : false,
serverURLs : ''
});
}, window.Valine);
});
</script>
</body>
</html>