-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeed.xml
More file actions
969 lines (727 loc) · 38.4 KB
/
feed.xml
File metadata and controls
969 lines (727 loc) · 38.4 KB
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.6">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2019-08-17T03:39:46+00:00</updated><id>/feed.xml</id><title type="html">图解我的私房菜</title><subtitle>Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.</subtitle><entry><title type="html">Golang按行解析日志神器之ldetool</title><link href="/%E5%A4%A7%E6%95%B0%E6%8D%AE/2019/08/15/Golang%E6%8C%89%E8%A1%8C%E8%A7%A3%E6%9E%90%E6%97%A5%E5%BF%97%E7%A5%9E%E5%99%A8%E4%B9%8Bldetool.html" rel="alternate" type="text/html" title="Golang按行解析日志神器之ldetool" /><published>2019-08-15T00:00:00+00:00</published><updated>2019-08-15T00:00:00+00:00</updated><id>/%E5%A4%A7%E6%95%B0%E6%8D%AE/2019/08/15/Golang%E6%8C%89%E8%A1%8C%E8%A7%A3%E6%9E%90%E6%97%A5%E5%BF%97%E7%A5%9E%E5%99%A8%E4%B9%8Bldetool</id><content type="html" xml:base="/%E5%A4%A7%E6%95%B0%E6%8D%AE/2019/08/15/Golang%E6%8C%89%E8%A1%8C%E8%A7%A3%E6%9E%90%E6%97%A5%E5%BF%97%E7%A5%9E%E5%99%A8%E4%B9%8Bldetool.html"><p>为什么称为神器,<a href="https://blog.csdn.net/orangleliu/article/details/85038304">请看</a>
此文章解析日志的性能和使用方便测试</p>
<h2 id="0安装">0.安装</h2>
<pre><code class="language-bash">go get -u github.com/sirkon/ldetool
</code></pre>
<h2 id="1使用命令行如下">1.使用命令行如下</h2>
<pre><code class="language-bash">ldetool generate --package 包名 规则文件.lde
</code></pre>
<p>生成Golang规则类的文件,在Golang中调它的方法如下</p>
<pre><code class="language-go">//规则定义的类名
l := &amp;Line{}
str :="hello world"
if ok, err := l.Extract(str);ok &amp;&amp; err ==nil{
//Time是规则定的属性
fmt.Sprintf("%s",l.Time)
}
</code></pre>
<h2 id="2实际例子">2.实际例子</h2>
<h3 id="21日志内容">2.1日志内容</h3>
<pre><code class="language-bash">27.154.70.117 - - [20/Nov/2018:20:23:56 +0800] 2 "GET http://7img1.tianlaikge.com/tv/homeimage/201709/shengridangao.zip HTTP/1.1" 200 842186 841382 "-" "-" "Dalvik/2.1.0 (Linux; U; Android 7.1.2; vivo Y79A Build/N2G47H)" "-" 219082621 "HIT" 27.159.73.35
</code></pre>
<h3 id="22-ldetool规则文件">2.2 ldetool规则文件</h3>
<pre><code class="language-bash">vim nginx.lde
</code></pre>
<pre><code class="language-yml">Nginx =
IP(string) " "
^"- - [" Time(string) ']'
^" "Cost(uint32)" "
^'"'Request(string)'"'
^" "Status(int64)" "
ByteSend(int64)" "
BodyByteSend(int64)" "
^'"'Referer(string)'"'
^" "
^'"'Cookie(string)'"'
^' '
^'"'UserAgent(string)'"'
^' '
^'"'Xfor(string)'"'
^' 'ConnID(string)' '
^'"'Hit(string)'"'
^" "
ServerIp(string)
;
</code></pre>
<p>格式是</p>
<pre><code class="language-yml">类名=
##IP则它义定的属性,因为golang的原因,首字母要大写,如果是id则要ID都得大写
IP(string) " "
;
</code></pre>
<h2 id="3规则详解">3.规则详解</h2>
<h3 id="31-规则列表">3.1 规则列表</h3>
<p><img src="img/规则.png" alt="规则" title="ldetool 规则" /></p>
<h3 id="32-原理">3.2 原理</h3>
<p>排号系统,意思是说叫到号的人走出休息区。休息区是等待叫号的人,休息区则是要匹配的字符串.
比如:匹配字符串则休息区是【休息区是等待叫号的人】,排号系统叫到【等待】,那匹配规则是【_等待】,
匹配系统则会将等待之前的字符走到休息区,则休息区此时变为【叫号的人】.</p>
<h3 id="33-规则解释">3.3 规则解释</h3>
<ul>
<li><code>_[N:]</code>是匹配可以不重复N个字符,比如_“test”[:3] Filed(float64) “ “,那休息区开头必须是【[前面三个任意字符]test 878787】</li>
<li><code>*</code> 是匹配重复N个字符,也是<code>_[N:]</code>一种特例</li>
<li><code>^</code> 是匹配前缀的字符</li>
<li><code>^</code> 或 <code>_</code> 都是有序的,就是顺序是固定的,如果顺序是随机的话则在前面加上 <code>?</code></li>
<li><code>@</code> 或 <code>..</code> 则包括匹配词,<code>@</code> 是字符串,<code>..</code> 则是字符</li>
<li><code>_[N:M]</code> ,<code>?</code> <code>..|@</code> 可以组成使用</li>
</ul>
<h2 id="4参考文章">4.参考文章</h2>
<p><a href="https://github.com/sirkon/ldetool">ldetool官方源代码</a></p>
<p><a href="https://blog.csdn.net/orangleliu/article/details/85038304">ldetool性能分析</a></p></content><author><name></name></author><category term="["大数据"]" /><category term="golang" /><category term="工具" /><category term="日志" /><summary type="html">为什么称为神器,请看 此文章解析日志的性能和使用方便测试</summary></entry><entry><title type="html">玩转docker-composer基础篇</title><link href="/%E4%BA%91%E8%AE%A1%E7%AE%97/2019/07/18/docker-composer%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86.html" rel="alternate" type="text/html" title="玩转docker-composer基础篇" /><published>2019-07-18T00:00:00+00:00</published><updated>2019-07-18T00:00:00+00:00</updated><id>/%E4%BA%91%E8%AE%A1%E7%AE%97/2019/07/18/docker-composer%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86</id><content type="html" xml:base="/%E4%BA%91%E8%AE%A1%E7%AE%97/2019/07/18/docker-composer%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86.html"><h2 id="0docker-composer安装">0.docker-composer安装</h2>
<p>从github上下载docker-compose二进制文件安装</p>
<pre><code class="language-shell">##下载最新版的docker-compose文件
curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` \
-o /usr/local/bin/docker-compose
##添加可执行权限
chmod +x /usr/local/bin/docker-compose
##测试安装结果
docker-compose --version
</code></pre>
<h2 id="1基本概念">1.基本概念</h2>
<ul>
<li>使用Docker Compose不再需要使用shell脚本来启动容器</li>
<li>Compose 通过一个配置文件来管理多个Docker容器,在配置文件中,所有的容器通过services来定义,然后使用docker-compose脚本来启动,停止和重启应用,和应用中的服务以及所有依赖服务的容器</li>
<li>Compose 项目由 Python 编写,实现上调用了 Docker 服务提供的 API 来对容器进行管理</li>
<li>服务 (service):一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。</li>
<li>项目 (project):由一组关联的应用容器组成的一个完整业务单元,在 docker-compose.yml 文件中定义。</li>
</ul>
<h2 id="2基本命令的使用技巧">2.基本命令的使用技巧</h2>
<p>docker-compose –help 参数 可以查看调用的参数的格式</p>
<pre><code class="language-shell">##容器相关操作的命令
docker-compose --help
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f &lt;arg&gt;...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert deploy
keys in v3 files to their non-Swarm equivalent
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
</code></pre>
<h2 id="3-docker-composer-常用的命令">3. docker-composer 常用的命令</h2>
<ul>
<li>ps:列出所有运行容器</li>
</ul>
<pre><code class="language-shell">docker-compose ps
</code></pre>
<ul>
<li>logs:查看服务日志输出</li>
</ul>
<pre><code class="language-shell">docker-compose logs eureka
</code></pre>
<ul>
<li>build:构建或者重新构建服务</li>
</ul>
<pre><code class="language-shell">docker-compose build .
##或者
docker-compose build -f xx.yml
</code></pre>
<ul>
<li>rm:删除指定服务的容器</li>
</ul>
<pre><code class="language-shell">docker-compose rm eureka
</code></pre>
<ul>
<li>start:启动指定服务已存在的容器</li>
</ul>
<pre><code class="language-shell">docker-compose stop eureka
</code></pre>
<ul>
<li>stop:停止已经处于运行状态的容器,但不删除它</li>
</ul>
<pre><code class="language-shell">docker-compose stop eureka
</code></pre>
<ul>
<li>restart:重启项目中的服务</li>
</ul>
<pre><code class="language-shell">docker-compose restart eureka
</code></pre>
<ul>
<li>up:构建、启动容器</li>
</ul>
<pre><code class="language-shell">docker-compose up -d
</code></pre>
<ul>
<li>down:停用移除所有容器以及网络相关</li>
</ul>
<pre><code class="language-shell">docker-compose down
</code></pre>
<h2 id="4docker-composeyml-属性">4.docker-compose.yml 属性</h2>
<ul>
<li>version:指定 docker-compose.yml 文件的写法格式</li>
</ul>
<pre><code class="language-yml">version: '3'
</code></pre>
<ul>
<li>services:多个容器集合</li>
</ul>
<pre><code class="language-yml">version: '3'
services:
php-fpm:
java:
go:
</code></pre>
<ul>
<li>build:配置构建时,Compose 会利用它自动构建镜像,该值可以是一个路径,也可以是一个对象,用于指定 Dockerfile 参数</li>
</ul>
<pre><code class="language-yml">version: '3'
services:
php-fpm:
build:
context: ./php-fpm ##目录
dockerfile: Dockerfile ##配置文件
java:
build:
context: ./java
go:
build:
context: ./go
</code></pre>
<ul>
<li>command:覆盖容器启动后默认执行的命令</li>
</ul>
<pre><code class="language-yml">version: '3'
services:
php-fpm:
build:
context: ./php-fpm ##目录
dockerfile: Dockerfile ##配置文件
java:
build:
context: ./java
command: [bundle,exec,thin,-p,3000]
go:
build:
context: ./go
</code></pre>
<ul>
<li>environment:环境变量配置,可以用数组或字典两种方式</li>
</ul>
<pre><code class="language-yml">environment:
RACK_ENV: development
SHOW: 'ture'
-------------------------
environment:
- RACK_ENV=development
- SHOW=ture
</code></pre>
<pre><code class="language-yml">version: '3'
services:
php-fpm:
build:
context: ./php-fpm ##目录
dockerfile: Dockerfile ##配置文件
java:
build:
context: ./java
command: [bundle,exec,thin,-p,3000]
go:
build:
context: ./go
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
</code></pre>
<ul>
<li>expose:暴露端口,只将端口暴露给连接的服务,而不暴露给主机</li>
</ul>
<pre><code class="language-yml">version: '3'
services:
php-fpm:
build:
context: ./php-fpm ##目录
dockerfile: Dockerfile ##配置文件
expose:
- "3000"
- "8000"
java:
build:
context: ./java
command: [bundle,exec,thin,-p,3000]
go:
build:
context: ./go
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
</code></pre>
<ul>
<li>image:指定服务所使用的镜像</li>
</ul>
<pre><code class="language-yml">version: '3'
services:
nginx:
image: nginx
php-fpm:
build:
context: ./php-fpm ##目录
dockerfile: Dockerfile ##配置文件
expose:
- "3000"
- "8000"
java:
build:
context: ./java
command: [bundle,exec,thin,-p,3000]
go:
build:
context: ./go
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
</code></pre>
<ul>
<li>network_mode:设置网络模式</li>
</ul>
<pre><code class="language-yml">network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
</code></pre>
<pre><code class="language-yml">version: '3'
networks:
os_bridge:
driver: bridge
services:
nginx:
image: nginx
php-fpm:
build:
context: ./php-fpm ##目录
dockerfile: Dockerfile ##配置文件
expose:
- "3000"
- "8000"
networks:
- os_bridge
java:
build:
context: ./java
command: [bundle,exec,thin,-p,3000]
go:
build:
context: ./go
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
</code></pre>
<ul>
<li>ports:对外暴露的端口定义,和 expose 对应</li>
</ul>
<pre><code class="language-yml">version: '3'
services:
php-fpm:
build:
context: ./php-fpm ##目录
dockerfile: Dockerfile ##配置文件
expose:
- "3000"
- "8000"
ports:
- "59501:9501"
java:
build:
context: ./java
command: [bundle,exec,thin,-p,3000]
go:
build:
context: ./go
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
</code></pre>
<ul>
<li>volumes:卷挂载路径</li>
</ul>
<pre><code class="language-yml">version: '3'
services:
php-fpm:
build:
context: ./php-fpm ##目录
dockerfile: Dockerfile ##配置文件
expose:
- "3000"
- "8000"
ports:
- "59501:9501"
volumes:
- ./php-fpm/php${PHP_VERSION}.ini:/usr/local/etc/php/php.ini
- ./php-fpm/mod:/usr/local/etc/php/conf.d/mod/
- ./php-fpm/conf.d:/usr/local/etc/php-fpm.d/cfg.d/
java:
build:
context: ./java
command: [bundle,exec,thin,-p,3000]
go:
build:
context: ./go
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
</code></pre>
<ul>
<li>links:将指定容器连接到当前连接,可以设置别名,避免ip方式导致的容器重启动态改变的无法连接情况</li>
</ul>
<pre><code class="language-yml">version: '3'
services:
php-fpm:
build:
context: ./php-fpm ##目录
dockerfile: Dockerfile ##配置文件
expose:
- "3000"
- "8000"
ports:
- "59501:9501"
volumes:
- ./php-fpm/php${PHP_VERSION}.ini:/usr/local/etc/php/php.ini
- ./php-fpm/mod:/usr/local/etc/php/conf.d/mod/
- ./php-fpm/conf.d:/usr/local/etc/php-fpm.d/cfg.d/
links:
- mongo:db ##mongo是其他容器的服务名,php-fpm服务用db访问到mongo的容器的服务
- redis
java:
build:
context: ./java
command: [bundle,exec,thin,-p,3000]
go:
build:
context: ./go
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
</code></pre>
<h2 id="5出现的问题">5.出现的问题</h2>
<p>Q:当docker-compose.yml配置发生变化时,如何去更新容器?
A:可使用 docker-compose up 命令更新配置</p>
<p>Q:为什么yml配置总是报错呢?
A:yml配置是有格式的,YAML中允许表示三种格式,分别是常量值,对象和数组。格式如下:</p>
<pre><code class="language-yml">#即表示url属性值;
url: http://www.wolfcode.cn
#即表示server.host属性的值;
server:
host: http://www.wolfcode.cn
#数组,即表示server为[a,b,c]
server:
- 120.168.117.21
- 120.168.117.22
- 120.168.117.23
#常量
pi: 3.14 #定义一个数值3.14
hasChild: true #定义一个boolean值
name: '你好YAML' #定义一个字符串
</code></pre>
<p>基本格式要求</p>
<p>1,YAML大小写敏感;</p>
<p>2,使用缩进代表层级关系;</p>
<p>3,缩进只能使用空格,不能使用TAB,不要求空格个数,只需要相同层级左对齐(一般2个或4个空格)</p>
<h2 id="6参考文章">6.参考文章</h2>
<p><a href="https://docs.docker.com/">docker官方</a></p>
<p><a href="https://docs.docker.com/compose/">docker-composer官方</a></p>
<p><a href="https://docs.docker.com/compose/compose-file/">docker-composer文件配置参数</a></p></content><author><name></name></author><category term="["云计算"]" /><category term="docker" /><category term="docker-compose" /><summary type="html">0.docker-composer安装</summary></entry><entry><title type="html">玩转docker基础篇</title><link href="/%E4%BA%91%E8%AE%A1%E7%AE%97/2019/07/15/docker%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86.html" rel="alternate" type="text/html" title="玩转docker基础篇" /><published>2019-07-15T00:00:00+00:00</published><updated>2019-07-15T00:00:00+00:00</updated><id>/%E4%BA%91%E8%AE%A1%E7%AE%97/2019/07/15/docker%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86</id><content type="html" xml:base="/%E4%BA%91%E8%AE%A1%E7%AE%97/2019/07/15/docker%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86.html"><h2 id="0docker">0.docker</h2>
<p>Docker 分为 CE 和 EE 两大版本。CE 即社区版(免费,支持周期 7 个月),EE 即企业版,强调安全,付费使用,支持周期 24 个月。</p>
<p>Docker CE 分为 stable, test, 和 nightly 三个更新频道。每六个月发布一个 stable 版本 (18.09, 19.03, 19.09…)。</p>
<ul>
<li>
<p>安装</p>
<p>官方网站上有各种环境下的 <a href="https://docs.docker.com/install/linux/docker-ce/centos/">安装指南</a></p>
</li>
<li>
<p>与传统虚拟机的区别</p>
<p><img src="img/专业图片区别.png" alt="专业图片区别" title="专业图片区别" /></p>
<p><img src="img/看得懂的图.png" alt="看得懂的图" title="看得懂的图" /></p>
<p><img src="img/docker与虚拟机差异.png" alt="docker与虚拟机差异" title="docker与虚拟机差异" /></p>
</li>
</ul>
<h2 id="1基本概念">1.基本概念</h2>
<h3 id="10-集装箱对容器技术docker的启发">1.0 集装箱对容器技术(Docker)的启发</h3>
<p><img src="img/logo.png" alt="docker logo" title="docker logo" /></p>
<p><img src="img/docker与集装箱.png" alt="docker与集装箱" title="docker与集装箱" /></p>
<h3 id="11-镜像">1.1 镜像</h3>
<p>对于 Linux 而言,内核启动后,会挂载 root 文件系统为其提供用户空间支持。而 Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:18.04 就包含了完整的一套 Ubuntu 18.04 最小系统的 root 文件系统。</p>
<p>镜像构建时,会一层层构建,前一层是后一层的基础。每一层构建完就不会再发生改变,后一层上的任何改变只发生在自己这一层。比如,删除前一层文件的操作,实际不是真的删除前一层的文件,而是仅在当前层标记为该文件已删除。在最终容器运行的时候,虽然不会看到这个文件,但是实际上该文件会一直跟随镜像。因此,在构建镜像的时候,需要额外小心,每一层尽量只包含该层需要添加的东西,任何额外的东西应该在该层构建结束前清理掉。</p>
<h3 id="12容器">1.2容器</h3>
<p>镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的 类 和 实例 一样,镜像是静态的定义,容器是镜像运行时的实体
容器的实质是进程,但与直接在宿主执行的进程不同,容器进程运行于属于自己的独立的 命名空间</p>
<p>每一个容器运行时,是以镜像为基础层,在其上创建一个当前容器的存储层,我们可以称这个为容器运行时读写而准备的存储层为 容器存储层。
容器不应该向其存储层内写入任何数据,容器存储层要保持无状态化。所有的文件写入操作,都应该使用 数据卷(Volume)、或者绑定宿主目录,在这些位置的读写会跳过容器存储层,直接对宿主(或网络存储)发生读写,其性能和稳定性更高</p>
<p>数据卷的生存周期独立于容器,容器消亡,数据卷不会消亡。因此,使用数据卷后,容器删除或者重新运行之后,数据却不会丢失。</p>
<h3 id="13仓库">1.3仓库</h3>
<p>一个 Docker Registry 中可以包含多个 仓库(Repository);每个仓库可以包含多个 标签(Tag);每个标签对应一个镜像。</p>
<p>通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。我们可以通过 <仓库名>:<标签> 的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 latest 作为默认标签。</标签></仓库名></p>
<p>以 Ubuntu 镜像 为例,ubuntu 是仓库的名字,其内包含有不同的版本标签,如,16.04, 18.04。我们可以通过 ubuntu:16.04,或者 ubuntu:18.04 来具体指定所需哪个版本的镜像。如果忽略了标签,比如 ubuntu,那将视为 ubuntu:latest。</p>
<p>仓库名经常以 两段式路径 形式出现,比如 jwilder/nginx-proxy,前者往往意味着 Docker Registry 多用户环境下的用户名,后者则往往是对应的软件名。但这并非绝对,取决于所使用的具体 Docker Registry 的软件或服务。</p>
<p>Docker Registry 公开服务</p>
<p>私有 Docker Registry</p>
<h2 id="2docker-核心模块">2.docker 核心模块</h2>
<p><img src="img/docker核心模块.png" alt="docker核心模块" title="docker核心模块" /></p>
<p>核心功能简答介绍:</p>
<p>Docker是C/S模式</p>
<p>images:docker镜像,是Docker run的原材料</p>
<p>container: Docker运行的内容,是独立存在的</p>
<p>data volumes: 通过数据挂载的方式,实现数据共享</p>
<p>network:用户容器与外部、容器之间的通信,常用的方法有端口映射、link等</p>
<h2 id="3docker-使用流程">3.docker 使用流程</h2>
<p><img src="img/docker流程.png" alt="docker流程" title="docker流程" /></p>
<h2 id="4基本命令的使用技巧">4.基本命令的使用技巧</h2>
<p>docker 参数 –help 可以查看调用的参数的格式</p>
<pre><code class="language-shell">##容器相关操作的命令
docker container --help
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
</code></pre>
<h2 id="5-docker-常用的命令">5. docker 常用的命令</h2>
<h3 id="51-镜像操作">5.1 镜像操作</h3>
<ul>
<li>设置镜像[可选]</li>
</ul>
<pre><code class="language-shell">##配置大陆镜像
vim /etc/docker/daemon.json
##修改后如下:
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"live-restore": true
}
##修改后重启docker
systemctl restart docker.service
##向官方仓库search镜像
docker search centos
</code></pre>
<p>设置效果如下图:</p>
<p><img src="img/搜索.png" alt="docker 搜索" title="docker 搜索" /></p>
<ul>
<li>下载镜像</li>
</ul>
<pre><code class="language-shell">docker pull python:3
</code></pre>
<ul>
<li>查看镜像</li>
</ul>
<pre><code class="language-shell">docker images python:3
</code></pre>
<ul>
<li>保存正在运行的镜像</li>
</ul>
<pre><code class="language-shell">[root@localhost v]# docker run -it python:3 bash
root@34539696b356:/# ls
docker commit -a "xxx" -m "xxx" 34539696b356 my/test
</code></pre>
<ul>
<li>导出镜像</li>
</ul>
<pre><code class="language-shell">docker save -o m.tar.gz mysql:5.7.14
</code></pre>
<ul>
<li>导入镜像</li>
</ul>
<pre><code class="language-shell">docker load -i m.tar.gz
##或者
docker load &lt; m.tar.gz
</code></pre>
<ul>
<li>删除镜像</li>
</ul>
<pre><code class="language-shell">docker rmi -f 0f2d67697e38【镜像的ID】
</code></pre>
<h3 id="52-容器的操作">5.2 容器的操作</h3>
<ul>
<li>运行容器</li>
</ul>
<pre><code class="language-shell">docker container run -it -p27019:27017 --name ngo mongo:3.4.14
##后台运行
docker container run -it -d -p27019:27017 --name ngo mongo:3.4.14
</code></pre>
<ul>
<li>查看容器</li>
</ul>
<pre><code class="language-shell">docker container inspect ad2ec10fa879【容器的ID】
</code></pre>
<ul>
<li>进入容器</li>
</ul>
<pre><code class="language-shell">docker container exec -it 0f2d67697e38【容器的ID】 bash
</code></pre>
<ul>
<li>删除容器</li>
</ul>
<pre><code class="language-shell">##普通删除容器
docker container rm 0f2d67697e38【容器的ID】
##强制删除容器
docker container rm -f 0f2d67697e38【容器的ID】
</code></pre>
<ul>
<li>导出容器</li>
</ul>
<pre><code class="language-shell">##命令格式
docker container export container_id | gzip &gt; image_name.tar.gz
##运行5c7472359a7b容器的ID
docker container export 5c7472359a7b |gzip &gt;t.tar.gz
</code></pre>
<ul>
<li>导入容器</li>
</ul>
<pre><code class="language-shell">zcat t.tar.gz | docker container import - image_name
##查看镜像是否导入成功
docker images 0f2d67697e38【镜像的ID】
</code></pre>
<h2 id="6关注点">6.关注点</h2>
<ul>
<li>常用参数</li>
</ul>
<p>-it 常一起使用,以一个新的伪终端开启一个容易,使用后就能新开一个容器中的终端,相当于新开了一个虚拟机,接着在终端中的命令就是在容器中执行命令</p>
<p>–rm 删除容器</p>
<p>-f 强制操作</p>
<ul>
<li>端口</li>
</ul>
<p>端口开放通过启动参数 -p 来指定。
-p 宿主机端口:容器端口。
一般来说,为了方便管理,会设置宿主机端口和容器端口保持一致.
比如web端口:-p 80:80</p>
<ul>
<li>挂载</li>
</ul>
<p>数据卷通过启动参数 -v 来指定
-v后面的映射关系是”宿主机文件/目录:容器里对应的文件/目录”,其中,宿主机上的文件/目录是要提前存在的,容器里对应的文件/目录会自动创建。</p>
<ul>
<li>查看日志</li>
</ul>
<pre><code class="language-shell">docker logs ad2ec10fa879【容器的ID】
</code></pre>
<ul>
<li>容器于宿主拷贝文件</li>
</ul>
<pre><code class="language-shell">docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
# 本地文件上传到对应容器的目录
docker cp local.sh 【容器的ID】:【容器的路径】
</code></pre>
<ul>
<li>容器之间是如何通信的</li>
</ul>
<p>–link 参数 底层是从过hosts文件来配置相对的IP</p>
<pre><code class="language-shell">
##开启mysql服务
docker container run --name=mysql_server -d -P kongxx/mysql_server
##连接mysql_server的容器1
docker container run --name=mysql_client1 --link=mysql_server:db -t -i kongxx/mysql_client /usr/bin/mysql -h db -u root -pletmein
##连接mysql_server的容器2
docker container run --name=mysql_client2 --link=mysql_server:db -t -i kongxx/mysql_client /usr/bin/mysql -h db -u root -pletmein
</code></pre>
<p>–link=mysql_server:db 其实是在/etc/hosts文件配置
mysql_server的IP db</p>
<ul>
<li>查看容器的进程</li>
</ul>
<pre><code class="language-shell">docker ps [-a]
</code></pre>
<ul>
<li>限制内存</li>
</ul>
<p>限制容器可以使用的最大内存为 300M,并且 swap 空间使用不受限制的参数如下:</p>
<p>-m 300M –memory-swap -1</p>
<p>强调一下 –memory-swap 是必须要与 –memory 一起使用的。</p>
<p>正常情况下, –memory-swap 的值包含容器可用内存和可用 swap。所以 –memory=”300m” –memory-swap=”1g” 的含义为:
容器可以使用 300M 的物理内存,并且可以使用 700M(1G -330M) 的 swap。–memory-swap 居然是容器可以使用的物理内存和可以使用的 swap 之和!</p>
<p>把 –memory-swap 设置为 0 和不设置是一样的,此时如果设置了 –memory,容器可以使用的 swap 大小为 –memory 值的两倍。</p>
<ul>
<li>绑定CPU资源</li>
</ul>
<p>–cpus=2 表示容器最多可以使用主机上两个 CPU
–cpuset-cpus=”1,3”一次指定多个 CPU
–cpu-shares 选项用来设置 CPU 权重,它的默认值为 1024。我们可以把它设置为 2 表示很低的权重,但是设置为 0 表示使用默认值 1024</p>
<ul>
<li>查看容器占用资源</li>
</ul>
<pre><code class="language-shell">docker stats
##只返回当前的状态
docker stats --no-stream
##只输出指定的容器
docker stats --no-stream ad2ec10fa879【容器ID】
</code></pre>
<h2 id="7出现的问题">7.出现的问题</h2>
<p>Q:如何去掉每次sudo运行docker命令?
A:需要添加组</p>
<pre><code class="language-shell"># Add the docker group if it doesn't already exist.
$ sudo groupadd docker
#改完后需要重新登陆用户
$ sudo gpasswd -a ${USER} docker
</code></pre>
<p>Q:为什么运行docker容器会失败呢?
A:一般有两个方面去判断:</p>
<p>1.docker容器没有实例进程启动。</p>
<p>2.docker容器内存溢出的情况</p>
<h2 id="8参考文章">8.参考文章</h2>
<p><a href="https://docs.docker.com/">docker官方</a></p>
<p><a href="https://hub.docker.com/">docker仓库官方</a></p>
<p><a href="https://yeasy.gitbooks.io/docker_practice/introduction/why.html">Docker技术入门与实战</a></p>
<p><a href="https://blog.csdn.net/gaoyingju/article/details/49616295">为什么容器技术将主宰世界</a></p></content><author><name></name></author><category term="["云计算"]" /><category term="docker" /><category term="docker-compose" /><summary type="html">0.docker</summary></entry></feed>