-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathTech-COE-Docker-Basics.txt
918 lines (555 loc) · 23.9 KB
/
Tech-COE-Docker-Basics.txt
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
Day1
====
1.Why Docker?
2.What is a docker ?
=====================
Docker is a container management service.
The keywords of Docker are develop, ship and run anywhere.
The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere.
3.Features of Docker
====================
=>Docker has the ability to reduce the size of development by providing a smaller footprint of the operating system via containers.
=>With containers, it becomes easier for teams across different units, such as development, QA and Operations to work seamlessly across applications.
=>You can deploy Docker containers anywhere, on any physical and virtual machines and even on the cloud.
=>Since Docker containers are pretty lightweight, they are very easily scalable.
Docker Basic Commands
=====================
Basic
: docker version
: docker -v
: docker --version
: docker info
: docker --help
: docker login
————————————
Images
: docker images To list the images
: docker images -q To list the images
: docker pull To pull tha image
: docker rmi To remove one or more iamges
: docker rmi $(docker images -q) To remove all images
————————————
Containers
: docker ps shows only running cotainers(use -a for show all containers))
: docker ps -a shows all containers
: docker rm <container-name> remove one or more containers
: docker rm $(docker ps -a -q) remove all containers
: docker run <image-name> create a container from image (first time used to container)
: docker run -d <image-name> create a container from image in detached mode
: docker run -it <image-name> bash create a container from image in interactive mode
: docker start -i <container-name> To start the stoped container
: docker stop <container-name> To stop running container
: docker logs <container-name> To sess logs of contianer
: docker exec -it <container-name> bash To interact with container
: docker run --name a-centos -it centos bash container will not be removed on exit
: docker run --name b-centos --rm -it centos bash container will be removed on exit
————————————
System
: docker stats
: docker system df
: docker system prune (to stop all stoped containers)
Online practice for docker:
https://labs.play-with-docker.com/
====================================================================
7-April-2020
============================
To create a container from image only first time
===================================================
docker run --name a-centos -it centos
docker start a-centos
docker stop a-centos
docker rm a-centos
docker logs a-centos
docker ps
docker ps -a
docker exec -it a-centos bash
docker rm $(docker ps -a -q)
docker run --name a-centos -it centos bash
docker run --name b-centos -it centos bash
docker network inspect bridge
docker exec -it a-centos bash
# ping 172.17.0.3
docker internall uses networking concepts
==========================================
bridge
host
none
mysql
============
docker run --name p-mysql -e MYSQL_ROOT_PASSWORD=pradeep -d mysql
docker run --name p-mysql -e MYSQL_ROOT_PASSWORD=pradeep -p 3232:3306 -d mysql:5.6
docker logs p-mysql
docker exec -it p-mysql bash
#mysql -uroot -ppradeep
mysql> show databases
======================================
web servers :httpd default port :80
:nginx default port :80
:mysql default port :3306
:mongodb default port :27017
docker run --name a-nginx -p 1111:80 -d nginx
docker run --name b-nginx -p 2222:80 -d nginx
docker run --name c-nginx -p 3333:80 -d nginx
docker exec -it a-nginx bash
echo "<h1>Hello A-NgInx Server </h1>" > /usr/share/nginx/html/index.html
docker exec -it b-nginx bash
echo "<h1>Hello B-NgInx Server </h1>" > /usr/share/nginx/html/index.html
docker exec -it c-nginx bash
echo "<h1>Hello C-NgInx Server </h1>" > /usr/share/nginx/html/index.html
8-Apr-2020
===========
To create image from running container
========================================
docker run --name p-apache -d -p 9999:80 httpd
echo "<h1>Hello Welcome To Custom Apache Image</h1>" > /usr/local/apache2/htdocs/index.html
docker commit -m 'Index File Content Changed' -a 'Pradeep Chinchole <[email protected]>' p-apache pradeepch82/apache:1.1
docker login
username :[email protected]
password :password
docker push pradeepch82/apache:1.1
docker run --name b-apache -p 4545:80 -d pradeepch82/apache:1.1
Dockerfile :
A text file with instructions to build image
Automation of Docker Image Creation
FROM :Base Image
MAINTAINER :Author Name
ENV :Environment variable
ADD :ADD the files while buiding the image
RUN :To install libraries/packages (executed while building the image)
ENTRYPOINT :Executed at run time and we can not override the commands
CMD :Executed at run time and we can override by passing command line argument
Step 1 : Create a file named Dockerfile
Step 2 : Add instructions in Dockerfile
Step 3 : Build dockerfile to create image
Step 4 : Run image to create container
Example :Dockerfile
FROM ubuntu
MAINTAINER Pradeep Chinchole <[email protected]>
RUN apt-get update
CMD ["echo", "Hello World..! from my first docker iamge1"]
CMD ["echo", "Hello World..! from my first docker iamge2"]
CMD ["echo", "Hello World..! from my first docker iamge3"]
CMD ["echo", "Hello World..! from my first docker iamge4"]
COMMANDS
: docker build directoryOfDocekrfile
: docker build -t ImageName:Tag directoryOfDocekrfile
: docker run imageid
Docker File Spring Boot Java Application
================================================
FROM openjdk:8
ADD target/docker-spring-boot.jar docker-spring-boot.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","docker-spring-boot.jar"]
======>
docker build -t pradeepch82/spring-dokcer-demo:1.1 .
docker login
docker push pradeepch82/spring-docker-demo:1.1
Docker file for MYSQL
=======================
# Derived from official mysql image (our base image)
FROM mysql
# Add a database
ENV MYSQL_DATABASE company
ENV MYSQL_ROOT_PASSWORD admin
ENV MYSQL_USER pradeep
ENV MYSQL_PASSWORD pradeep
# Add the content of the sql-scripts/ directory to your image
# All scripts in docker-entrypoint-initdb.d/ are automatically
# executed during container startup
ADD script.sql /docker-entrypoint-initdb.d/
script.sql
===========
use company;
create table employee(id int primary key,name text,salary double);
insert into employee values(101,'RAM',10000.00);
insert into employee values(102,'RAHIM',20000.00);
insert into employee values(103,'DAVID',30000.00);
insert into employee values(104,'PRADEEP',40000.00);
Linking Docker containers with Links
===========================================
Links allow the containers to discover each other and securely transfer the information about one container to another container.
docker network create p-network
docker run --name mysql02 --network p-network -e MYSQL_ROOT_PASSWORD=Password1234 -d mysql:5.6
docker run --name wordpress02 --network p-network --link mysql02 -p 1111:80 -e WORDPRESS_DB_HOST=mysql02:3306 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=Password1234 -e WORDPRESS_DB_NAME=wordpress -e WORDPRESS_TABLE_PREFIX=wp_ -d wordpress
Docker Volumes
===============
Docker Volume
===============
1. What are Volumes
2. How to create / list / delete volumes
3. How to attach volume to a container
4. How to share volume among containers
5. What are bind mounts
Volumes are the preferred mechanism for persisting data generated by and used by Docker containers
docker volume //get information
docker volume create
docker volume ls
docker volume inspect
docker volume rm
docker volume prune
Use of Volumes
===================
Decoupling container from storage
Share volume (storage/data) among different containers
Attach volume to container
On deleting container volume does not delete
Commands
docker run --name MyJenkins1 -v myvol1:/var/jenkins_home -p 8080:8080 -p 50000:50000 jenkins
docker run --name MyJenkins2 -v myvol1:/var/jenkins_home -p 9090:8080 -p 60000:50000 jenkins
docker run --name MyJenkins3 -v /Users/raghav/Desktop/Jenkins_Home:/var/jenkins_home -p 9191:8080 -p 40000:50000 jenkins
References
https://hub.docker.com/_/jenkins/
https://docs.docker.com/storage/volumes/
NOTES
By default all files created inside a container are stored on a writable container layer
The data doesn稚 persist when that container is no longer running
A container痴 writable layer is tightly coupled to the host machine where the container is running. You can稚 easily move the data somewhere else.
Docker has two options for containers to store files in the host machine
so that the files are persisted even after the container stops
VOLUMES and BIND MOUNTS
Volumes are stored in a part of the host filesystem which is managed by Docker
Non-Docker processes should not modify this part of the filesystem
Bind mounts may be stored anywhere on the host system
Non-Docker processes on the Docker host or a Docker container can modify them at any time
In Bind Mounts, the file or directory is referenced by its full path on the host machine.
Volumes are the best way to persist data in Docker
volumes are managed by Docker and are isolated from the core functionality of the host machine
A given volume can be mounted into multiple containers simultaneously.
When no running container is using a volume, the volume is still available to Docker and is not removed automatically. You can remove unused volumes using docker volume prune.
When you mount a volume, it may be named or anonymous.
Anonymous volumes are not given an explicit name when they are first mounted into a container
Volumes also support the use of volume drivers, which allow you to store your data on remote hosts or cloud providers, among other possibilities.
4.Components of Docker
======================
=>Docker for Mac - It allows one to run Docker containers on the Mac OS.
=>Docker for Linux - It allows one to run Docker containers on the Linux OS.
=>Docker for Windows - It allows one to run Docker containers on the Windows OS.
=>Docker Engine - It is used for building Docker images and creating Docker containers.
=>Docker Hub - This is the registry which is used to host various Docker images.
=>Docker Compose - This is used to define applications using multiple Docker containers.
5.Installation of Docker on windows?
Refer this site :
https://docs.docker.com/toolbox/toolbox_install_windows/
For windows we need to download Docker toolbox.
To Check Installation
=======================
1.docker version :shows the version of docker
2.docker info :gives the indormation of docker
Docker Basic Commands
=====================
Basic
: docker version
: docker --version
: docker -v
: docker info
: docker --help
: docker login
覧覧覧覧覧覧
Images
: docker images
: docker pull
: docker push
: docker rmi
覧覧覧覧覧覧
Containers
: docker ps
: docker run
: docker start
: docker stop
: docker pause
覧覧覧覧覧覧
System
: docker stats
: docker system df
: docker system prune
To remove all multiple containers
===================================
docker rm -f c2d79dbd5581 39bff36fec4f 5cddf9344a62 152e56f4f4d1 558197ba2d6e e1672924ad4e
Downloading the Latest MongoDB Docker Image
===========================================
Syntax : docker pull <image-name> or docker pull <image-name>:<tag>
Example : docker pull mongo or docker pull mongo:4.0.4
Deploying an Instance of MongoDB as a Container
====================================================
Syntax : docker run --name <alias-name> <image-name>:tag
Example : docker run --name mongodb mongo:4.0.4
Interacting with the MongoDB Docker Container with Basic Shell Operations
==========================================================================
Syntax :docker exec -it <container-name> bash
Example : docker exec -it mongodb bash
do some modification
and commit the changes.
To commit the changes in container
====================================
docker commit -m 'Pradeep has created mongodb' -a "Pradep Chinchole<[email protected]>" 055e9cfa39fc pradeep-mongodb:latest
To Tag the Image
==================
docker tag pradeep-mongodb:latest pradeepch82/pradeep-mongodb
To Push The Images
====================
docker push pradeepch82/pradeep-mongodb
short cut commands
=====================
docker run --name pradeep-mysql -it mysql:latest bash
docker commit -m "added mysql server" -a "Pradeep Chinchole" pradeep-mysql pradeepch82/pradeep-mysql:latest
docker login
docker push pradeepch82/pradeep-mysql
https://cloud.docker.com/repository/list
1. What are images
2. How to pull image
3. How to run a container using an image
4. Basic Commands
TIPS & TRICKS
What are Images ?
=================
Docker Images are templates used to create Docker containers
Container is a running instance of image
Where are Images Stored
Registries (e.g. docker hub)
Can be stored locally or remote
: docker images --help
: docker pull image
: docker images
: docker images -q
: docker images -f 電angling=false・
: docker images -f 電angling=false・-q
: docker run image
: docker rmi image
: docker rmi -f image
: docker inspect
: docker history imageName
1. What are Containers
2. How to create Containers
3. How to start / stop Containers
4. Basic Commands
TIPS & TRICKS
What are Containers:
Containers are running instances of Docker Images
COMMANDS
: docker ps
: docker run ImageName
: docker start ContainerName/ID
: docker stop ContainerName/ID
: docker pause ContainerName/ID
: docker unpause ContainerName/ID
: docker top ContainerName/ID
: docker stats ContainerName/ID
: docker attach ContainerName/ID
: docker kill ContainerName/ID
: docker rm ContainerName/ID
: docker history ImageName/ID
What is a Container ?
=====================
A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings.
Features of Containers:
======================
=>Are lightweight
=>Fewer resources are used
=>Booting of containers is very fast
=>Can start, stop, kill, remove containers easily and quickly
=>Operating System resources can be shared within Docker
=>Containers run on the same machine sharing the same Operating system Kernel, this makes it faster
=>You can use the command docker container create to create a container in stopped state
Day2
=====
Docker File
=============
1. What is Dockerfile
2. How to create Dockerfile
3. How to build image from Dockerfile
4. Basic Commands
TIPS & TRICKS
Dockerfile :
A text file with instructions to build image
Automation of Docker Image Creation
FROM
MAINTAINER
RUN
CMD
Step 1 : Create a file named Dockerfile
Step 2 : Add instructions in Dockerfile
Step 3 : Build dockerfile to create image
Step 4 : Run image to create container
Example :Dockerfile
FROM ubuntu
MAINTAINER Pradeep Chinchole <[email protected]>
RUN apt-get update
CMD ["echo", "Hello World..! from my first docker iamge"]
COMMANDS
docker build directoryOfDocekrfile
docker build -t ImageName:Tag directoryOfDocekrfile
docker run image
References:
https://www.youtube.com/redirect?event=video_description&v=LQjaJINkQXY&redir_token=E3PErBNVcg9u6KYx6LdmsGYdf4x8MTU2Mjc3ODI4OUAxNTYyNjkxODg5&q=https%3A%2F%2Fgithub.com%2Fwsargent%2Fdocker-cheat-sheet%23dockerfile
https://www.youtube.com/redirect?event=video_description&v=LQjaJINkQXY&redir_token=E3PErBNVcg9u6KYx6LdmsGYdf4x8MTU2Mjc3ODI4OUAxNTYyNjkxODg5&q=https%3A%2F%2Fdocs.docker.com%2Fengine%2Freference%2Fbuilder%2F%23environment-replacement
Docker Compose
==============
1. What | Why - Docker Compose
2. How to install
3. How to create docker compose file
4. How to use docker compose file to create services
5. Basic Commands
TIPS
Docker compose
tool for defining & running multi-container docker applications
use yaml files to configure application services (docker-compose.yml)
can start all services with a single command : docker compose up
can stop all services with a single command : docker compose down
can scale up selected services when required
Step 1 : install docker compose
(already installed on windows and mac with docker)
docker-compose -v
2 Ways
1. https://github.com/docker/compose/rel...
2. Using PIP
pip install -U docker-compose
Step 2 : Create docker compose file at any location on your system
docker-compose.yml
version: '3'
services:
web:
image: nginx
database:
image: redis
Step 3 : Check the validity of file by command
docker-compose config
Step 4 : Run docker-compose.yml file by command
docker-compose up -d
Steps 5 : Bring down application by command
docker-compose down
TIPS
How to scale services
耀cale
docker-compose up -d --scale database=4
Day3
====
day2
=======
docker pull centos
docker run -it centos bash
docker run -it ubuntu bash
//create folders and files
//exit
To Login
==========
docker login
To commit the changes in container
======================================
docker commit -m "Added new folders" -a "Pradeep Chinchole" 2886b219bb66 pradeepch82/pradeep-ubuntu:latest
To push the images
==================
docker push pradeepch82/pradeep-ubuntu:latest
Docker is a software Contairnerization platform
Difference Virtualization vs Contairnerization
==================================================
Virtulization
==============
Host os=> Hypervisor=>(Guest os1,Guest os2....)=>(Bin& Lib, Bin & Lib 2.....)=>(App1,App2,.....)
1.Virtualization is a technique of importing a guest os on the top of host os.
2.This technique is revolution in the beginning because Developers run the different application in different vms all running on same host.
3.This eliminate the need of extra h/w resource and enable the backup allowing the the recovery in failure conditions
4.lowering the total cost of foundation.
Advantages
==========
1.Multiple OS in the same machine
2.Easy Maintainance and Recovery
3.Lower total cost of Ownership
Disadvantages
=============
1.Multiple VMs Lead to unstable performance
2.Hypervisors are not as efficient as a host os
3.Long Boot up process (Approximate 1 minute)
4.This is using the host system resources
5.This is very critical in case of real time applications
where fast processing is required.
6.scaling the no of vms is tedious and costly affiar
Contairnerization
==================
Host os=>Container Engine=>(Bin& Lib, Bin & Lib 2.....) =>(App1,App2)
1.Contairnerization is Just Virtualization at OS Level
2.Contairnerization is also a type of Virtualization.
3.It is more efficient as there is no guest os.
4.Binaries and Libraries of the containers are on the host kernel which makes processing and execution very fast
5.Even Booting takes fraction of second.
Virtualization brings the abstraction at h/w where as Contairnerization brings abstraction to software
Advantages
==========
1.Containers on same os kernel are lighter and smaller
2.Better Resource utilization Compared to VMS
3.Short Boot up process (1/20th of second )
Which of the below option is incorrect ?
1. Contairnerization is replacing Virtualization
2. Contairnerization brings process level isolation
3. Hypervisors Manage the VMS.
What is Docker ?
==============
Docker is a Contairnerization platform which packages your
application and all its dependencies together in the form of containers
so as to ensure that your application works seamlessly in any environment
be it Dev/Test/Prod
Host Os=>Docker Engine=> (App1[bin & lib],App2[bin & lib])=>(container1 ,container2)
The no of system scaled up easily.
=============================================================
VMS Dockers
=============================================================
Elephants Size Rabbit
Tortoise Speed Chita
No integration Integration comfortably integrated with Devops tools fo automating the manual process.
Resource and Memory Utilization
======================================
Host os 16 GB Ram
==================
In Case of virtual Macine
=============================
Allocation => VMs => Memory Used => Memory Un used
6 GB => VM 1 => 4 GB => 2 GB
4 GB => VM 2 => 1 GB => 3 GB
6 GB => VM 3 => 2 GB => 2 GB
==========================================================
16 GB 9 GB 7 GB
7 GB RAM is blocked
7 GB RAM cannot be alloted to new vm.
Host os 16 GB Ram
==================
In Case of Docker
=============================
Allocation => Docker => Memory Used
=> Container1 => 4 GB
=> Container2 => 1 GB
=> Container3 => 2 GB
==========================================================
16 GB 9 GB 7 GB
No Ram is allocated for unused
7 GB RAM is unused
7 GB RAM can be alloted to new containerization.
Building and deployment
===============================
In case of VMs
New Builds=>Multiple Guest OS =>Separate Libraries =>Heavy=>More Time
In case of Docker
New Builds=>Same Host OS =>Separate Libraries =>Light Weight => Less Time
Integration
=============
In case of Vms Integration is possible but costly due to infrastructur requirement
and not easily scalable.
In case of Docker Integration is Faster,Cheap and scalable.
Q.Which of the follwing option is correct w.r.t creating Docker containers. ?
================================================================================
1.I can build my own image and use it for creating a Docker container. T
2.I can pull the image from Docker hub and then run the Docker image. T
3.I can create a Docker container without Docker image. F
Docker containers can be created by executing the docker image hence it is not
possible to run a container without building the image.
Q.Where does the Docker daemon stores the Docker images?
1.Docker Client
2.Docker Host
3.Docker Registry
All the docker images are stored in the Docker registry which can be either
local repository or a remote Docker hub.
Question
=========
Suppose I have a System with 4 GB RAM and I want to run 4 instances of particular Devops tool
then which of the below is the best choice?
1. 4 VMS with 4 different instances of that tool
2. 1 VM with 4 different instances of that tool
3. 4 Docker containers with different instances of that tool