forked from devops-learner01012022/DevOps_ClassNotes
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDOCKER_NOTES_LMS.txt
More file actions
513 lines (236 loc) · 8.18 KB
/
DOCKER_NOTES_LMS.txt
File metadata and controls
513 lines (236 loc) · 8.18 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
Installation of Docker:
# yum install docker -y
# systemctl start docker
************************************
Image Commands
**************************************
Scenario 1:
Search images on Docker Hub
***************************
Signinto docker hub : https://hub.docker.com/
Go to search tab and enter Hello-world ==> images will be displayed, select the offical image
When we run the image it will display welcome message to user.
Scenarios2:
Search for an Image locally on docker host
**************************************
Go to docker host
# docker search hello // all images related to hello will be displayed.
2. Pull an image from docker hub
# docker pull hello-world. //Image will be pulled from docker Hub
3. Check if Image is present in hub or not
# docker images //image will be present, that it is pulled into the host
Naming Convention for an Image:
Imagename:tagname
Userdefined image naming convention
username/imagename:tagname
4. Check specific details of image will be displayed.
# docker image hello-world
5. Run an Image
# docker run hello-world // container will be created
6. Check container is created
# docker ps -a
7. Create a new container and give name to the container
# docker run --name c1 hello-world
Understanding Image more:
Go to docker hub where all Images are present
Here you can search any image that we want, check for official tag
Naming convention of unofficial (user defined) Images
Registory/username/repoName:tagname
Naming convention of official Images
Registry/repoName:tagname
If no registryname is present by default it will be docker hub
If no tagname then by default it will be latest
6. Pull an Image and run the container together
*****************************************
# docker run --name u1 ubuntu
# docker images
# docker ps -a
==> this will pull the ubuntu image first from hub
--> then create a ubuntu conatiner with name as u1 in exited state
==> So run command == PULL IMAGE and CREATE CONTAINER
===> pull command ==> only pulls image
7. START CONTAINER IN DETACH MODE (pull and run together again)
************************************************************
# docker run --name n1 nginx // image nginx will be pulled and container with name n1 will be running
start the container in detach mode where in container will be running in background.
# docker run --name n1 -d nginx // starts container in detach mode
# docker ps -a. // gives all containers, nginx container running
*********************************
Delete all container
docker rm -f $(docker ps -aq)
docker system prune --all
***************************************************
Command line containers
Creating container in interactive mode
# docker run --name u1 -it ubuntu
We will be in container now
#ls
Out of container
# ctlpctlq
# docker attach u1
Again inside the container
Now use exit to come out
Both ways we can come out of container in exit case the container will go to exited state.
*************************************
Creating container in interactive mode
# docker run --name c1 -it centos
We will be in container now
#ls
# yum update -y
# yum install git -y
Out of container
exit
Container is exited now
Start the conatiner again
# docker start c1
# docker attach c1
# Ctldp +Ctlq
# docker stop c1
# docker rm c1
**********************************************
Creating a DB container
REMOVE all images and container before this
# docker run --name mydb -d -e MY_ROOT_PASSWORD=edureka mysql:5
PORT FORWARDING/PORT MAPPING
****************************************
# docker run --name c1 -d -p 8888:80 nginx // creating container to be accessed from browser
Go to browser
Give public ip:8888
*********************************************
Automatic port mapping
# docker run --name j1 -d -P jenkins
# docker container ls
OR
# docker port containername
On browser
hostip:new port
******************************************************
Docker Volumes
**********************************
Docker Volume Containers
**************************************
# docker volume create myvol // create a volume on host
# docker volume ls // list the volume on the host, myvol will be there
# docker volume inspect myvol // gives the path where volume is created on the host
/var/lib/docker/volumes/myvol/_data // copy this path on notepad
# cd /var/lib/docker/volumes/myvol/_data
# ls // no files
# cd
MOUNT THE VOLUME ON A CONTAINER
# docker run --name u1 -it -v myvol:/tmp ubuntu
# cd /tmp
# ls
# touch file1 file2
#ls
# Ctlp+Ctlq
Use the same path copied above
# cd /var/lib/docker/volumes/myvol/_data
# ls // file1 and file2 files will be present
Create 1 more new file here --> file should be available in container also
# touch file3
# ls
#cd
# docker ps -a
# docker attach u2
# ls
# cd /tmp
# ls
# file1, file2 , file3 will be there
REMOVE THE CONTAINER AND CHECK IF FILES ARE AVIALBLE
# docker rm -f u2
# cd /var/lib/docker/volumes/myvol/_data
# ls ==> still files will be available.
Delete the volume :
*********************
Delete containers before.
# docker volume rm myvol // volume will be removed.
**************************************************
DOCKER FILE
*****************************************
Creating our own Images:
2 ways:
1. Creating an image from a running container
2. Using a Docker file
Method 1:Creating an image from a running container
**********************************************
# docker run --name c1 -it centos
#ls
# yum update -y
# yum install git -y
# git --version
# ctlp+ctlQ
# docker commit c1 mycentosImage
# docker images // our image will be available with tag as latest
//Create a container from our own Image
# docker run --name c2 -it mycentosImage
push our image into Docker Hub:
# docker tag mycentosImage:latest sonal04/mycentosImage:latest
# docker images // new image with new name will be created
# docker login //loginto docker hub
Username:
Password:
# docker push dockerusername/mycentosImage:latest //image will be pushed to docker hub
Method2 : Creating a docker file and building an image from it
*******************************************************
it is a simple txt file.
# vim dockerfile
i
FROM centos // base image information
MAINTAINER edureka
RUN yum -y update // these are commands that we give for building the image
RUN yum install git -y // information about packages that shoudl be there on the image
VOLUME /data
:wq!
# docker build -t mycentos:git .
When we run the image -->container will be created.
# docker images
# docker run --name c1 -it mycentos1:dockerfile
==> data folder which is mounted for volume wll be there
OTher instructions that we can give in an Image :
FROM:
MAINTAINER:
RUN:
CMD:
VOLUME:
EXPOSE:
ADD:
**************************************************
Deploy Job in Jenkins:
**************************************
Steps to create a Jenkins job and create docker file and create container for addressbook.war
1. Start the jenkins on AWS instance
# systemctl start jenkins
2. Start docker
# systemctl start docker
3. Log into jenkins --> go to package job --go to console and copy the path of .war file
4. create a new job -->Deploy
5. Go to build -->shell command
rm -rf mydockerfile
mkdir mydockerfile
cd mydockerfile
cp /var/lib/jenkins/workspace/Package/target/addressbook.war .
touch dockerfile
cat <<EOT>> dockerfile
From tomcat
ADD addressbook.war /usr/local/tomcat/webapps
EXPOSE 8080
CMD ["catalina.sh", "run"]
EOT
sudo docker build -t myimage:$BUILD_NUMBER .
sudo docker run -itd -P myimage:$BUILD_NUMBER
Save the job
Go to instance to give jenkins permission to execute docker commands. As of now we are logged in as admin in jenkins and it doesnt have permission to run docker commands.
So go to
vim /etc/sudoers
i
add under root
Jenkins ALL=NOPASSWD: ALL
:wq!
Go back to jenkins and build now.
==> New image and container will be created
# docker images
# docker ps -a
Access from browser
http://18.221.206.57:32768/addressbook/
******************************************************************************
**************************************************