-
Notifications
You must be signed in to change notification settings - Fork 21
/
session11.txt
256 lines (157 loc) · 7.26 KB
/
session11.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
Docker:
sudo apt-get update
Use the following command to install packages to allow the apt to use a repository over HTTPS
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
#Use the following curl command to add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
#Use the following command to set up a stable repository:
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
#
sudo apt update
sudo apt-get install docker-ce
docker --version
docker --version
++++++++++++++++++++++++++++++++++++++++++++++++++++++
Actual Lab:
docker run hello-world
#this going to show you All the container with running / exit state
docker ps -a
#this going to show you All the container with running state
docker ps
#Remove the exit container
docker rm <containerid>
#list of docker image
docker images
#remove docker image
docker image rm <imageid>
++++++++++++++++++++++
#to search the marketplace images
docker search nginx
#Two way you can run a conatainer
1. Interactive terminal
2. Detach terminal
#pull nginx docker image
docker pull nginx
#run the nginx container (bydefalt it is going to run in a interactive terminal)
docker run nginx
#run the nginx container with detach terminal (-d stands for detach -t stands for terminal)
docker run -dt nginx
#stop running container
docker stop <containername/containerid>
#start the container
docker start <containername/containerid>
#lets try to run something in a interactive, (-i stands for interactive -t stands for terminal)
docker run -it ubuntu
#we need to run to ubuntu container in a interactive terminal
#if you want to come out of the container without exit
use shortcart ctrl+p+q
#two way you can connect to the running container with a bash process id 1
docker attach <containerid>
#Preffered process to connect running container, create new process for your terminal instead of process id 1
docker exec -it <conatainerid/name> <newprocess>
docker exec -it 8d71a634622c /bin/bash
#delete image
docker rmi <imageid/name>
#Port Mapping
#Host Port Mapping
1. Custom port mapping 2. Random port maping
#How to map random port
docker run -d -P nginx
#custom port
docker run -d -p7686:80 nginx
#login to the nginx container and change the default page
docker exec -it cff034d88691 /bin/bash
#deafult page is /usr/share/nginx/html/index.html
#to change the default page I have run this command inside the interactive terminal of my nginx container
echo "Niladri Nginx for Random Port" > /usr/share/nginx/html/index.html
#Test the application default page
curl localhost:32768
#if you want to access the same nxinx server with your public ip then you need to enable the port in Network Security group which is attached with your vm
#Volume mount in you container from host filesystem
root@ip-172-31-12-78:~# mkdir dockermount
root@ip-172-31-12-78:~# pwd
/root
root@ip-172-31-12-78:~# cd dockermount/
root@ip-172-31-12-78:~/dockermount# pwd
/root/dockermount
root@ip-172-31-12-78:~/dockermount# echo "Niladri's Configuration form host file system" > index.html
root@ip-172-31-12-78:~/dockermount# cat index.html
Niladri's Configuration form host file system
root@ip-172-31-12-78:~/dockermount# echo "Niladri's Configuration form host file addition page" > niladri.html
root@ip-172-31-12-78:~/dockermount# ls
index.html niladri.html
root@ip-172-31-12-78:~/dockermount#
#create a conatainer with the mount volume
docker run -dP -v /root/dockermount:/usr/share/nginx/html nginx
#docker logs <containerid>
#docker logs -f <containerid>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Types of Image:
1. Base image : which start with From scratch
# syntax=docker/dockerfile:1
FROM scratch
ADD hello /
CMD ["/hello"]
2. Parent Image: which is going to refer another image
# syntax=docker/dockerfile:1
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
+++++++++++
# To create any custom image you need to maintain a Dockerfile
# You are going to define all the instruction what your image is going to do
Dockerfile Format
The Dockerfile is a raw text document. The following are some popular instructions in a Dockerfile.
FROM – This instruction defines the parent image for subsequent instructions. FROM clause must be the first entry in a Dockerfile. It can come after a comment or parse directive or ARG used in the FROM directive.
ARG – It defines variables used during the build once you run Docker build command on the file.
AD
CMD – This sets the command executed upon container creation. Docker only allows one CMD instruction per file. When you have more than one defined, it runs the last command.
LABEL – The label instruction defines metadata information for the image. You can add as many labels as you see fit in the form of key-value pairs. For example, image metadata could include the version number, author information, description, etc.
RUN – Sets the instructions to be executed during the image build.
USER – This instruction sets the username or UID of the user when running the image or instructions in a Dockerfile such as CMD, RUN, and ENTRYPOINT.
AD
ENTRYPOINT – It defines the commands Docker executes upon container creation. Options are overridable in the command line during container startup.
ADD – This instruction copies files and directories from the specified source to a specified destination. The source can be a local path or an external URL. If the files are archives, Docker automatically unpacks them into the image.
VOLUME – The volume instructions allow you to create mount points from host machine directories or other containers.
EXPOSE – This instruction tells Docker which port to listen on during runtime.
AD
ENV – It sets environment variables.
WORKDIR – sets the current working directory. If the directory specified does not exist, Docker will automatically create one.
The above are some standard instructions you can use in a Dockerfile.
#Lets create a Docker file for Custom Nginx Image:
#Dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get install -y nginx
COPY index-custom.html /var/www /html/index.html
EXPOSE 80
CMD nginx -g 'daemon off;'
+++++++++++++++
root@ip-172-31-12-78:~/customdockerimage# ls
Dockerfile index-custom.html
root@ip-172-31-12-78:~/customdockerimage# cat Dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get install -y nginx
COPY index-custom.html /var/www/html/index.html
EXPOSE 80
CMD nginx -g 'daemon off;'
root@ip-172-31-12-78:~/customdockerimage# cat index-custom.html
This My nginx page from my Custome image
root@ip-172-31-12-78:~/customdockerimage#
++++++++++++++++++++++++
#Build the docker image
docker build -t <imagename>:<tag-version> <pathof thedockerfile>
docker build -t niladringinx:1.0 .
#docker run
docker run -dP niladringinx:1.0