-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-tutorial.sh
More file actions
290 lines (234 loc) · 6.63 KB
/
docker-tutorial.sh
File metadata and controls
290 lines (234 loc) · 6.63 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
##############
### Basics ###
##############
# runs nginx container
docker run nginx
# runs a specific image
docker run ubuntu:17.10
# shows containers running now
docker ps
# shows all the container launched
docker ps -a
# shows images
docker images
# stops container
docker stop nginx
# removes container
docker rm nginx
# removes image
docker rmi nginx
# runs container in detached mode -- means in background
docker -d nginx
# runs container in interactive mode
docker -it nginx
# attach container
docker attach nginx
# runs ubuntu and executes command
docker run ubuntu sleep 100
# example: shows current version
docker run ubuntu cat /etc/*release*
# runs ubuntu in interactive mode with terminal and runs bash, so we can interact with it
docker run -it ubuntu bash
#############################
### ADVANCED RUN COMMANDS ###
#############################
# show more info about the container
docker inspect nginx
# runs mysql container
docker run mysql
# run mysql container and maps docker host port (8080) to container port (5000)
docker run -p 8080:5000 mysql
# example with webapp: runs webapp in detached mode and maps container's port 5000 to 8080 (docker host port)
docker run -d -p 8080:5000 kodekloud/webapp
# mount external directory and data stays safe even if container is destroyed
docker run -v /opt/datadir/var/lib/mysql mysql
# provide info about container
docker inspect busybox
# logs from the container blissful_hopper
docker logs blissful_hopper
### Example ###
# We run Jenkins, set it up, etc.
# But when we stop this container and run a new one again... our data is gone (lost)
# So, Jenkins recommends to mount a volume for it
# Example: (from the docs)
docker run --name myjenkins -p 8080:8080 -p 50000:50000 -v /your_home:/var/jenkins_home jenkins
# or this way
mkdir my-jenkins-data
docker run -p 8080:8080 -v /root/my-jenkins-data:/var/jenkins_home -u root jenkins
# means:
# -p 8080:800 -- port mapping from 8080 from host to 8080 to container's port
# -v mount my volume to jenkins's volume to save and keep data
# -u root - username specified
### Enviroment Variables ###
# here we run simple-webapp-color and set up Envuroment Variable APP_COLOR to 'blue'
# to make it work we need to have it moved from the code to enviroment variables
docker run -e APP_COLOR=blue simple-webapp-color
# how to find?! RUN this and take a look at Config --> Env --> [here]
docker inspect [container_name]
#####################
### Dockerization ###
#####################
### Create my own image ###
# Create a Dockerfile:
FROM ubuntu
RUN apt-get update
RUN apt-get install python
RUN pip install flask
RUN pip install flask-mysql
COPY . /opt/source-code
ENTRYPOINT FLASK_APP=/opt/source_code/app.py flask run
# build the image from Dockerfile (dspv1 -- my login on Docker Hub)
docker build Dockerfile -t dspv1/my-custom-app
# the push it to Docker Hub (Docker Registry)
docker push dspv1/my-custom-app
# docker login to registry
docker login
######################
### Docker Compose ###
######################
docker run mmushad/simple-webapp
docker run mongodb
docker run redis:alpine
docker run ansible
# to save time we can combine everytihng at one file docker compose
# docker-compose.yml:
services:
web:
image: "mmumshad/simple-webapp"
database:
image: mongodb
messaging:
image: redis:alpine
orchestration:
images: ansible
# and then compile all together
doker-compose up
### Simple voting application ###
# voting-app (python)
# in-memory db (redis)
# worker (.net)
# db (postgre)
# result-app (node.js)
# deploy containers with docker and link them
docker run -d --name=redis redis
docker run -d --name=db postgres:9.4
docker run -d --name=vote -p 5000:80 --link redis:redis voting-app
docker run -d --name=result -p 5001:80 --link db:db result-app
docker run -d --name=worker --link db:db --link redis:redis worker
# generate docker-compose.yml:
redis:
image: reids
db:
image: postgres:9.4
vote:
image: voting-app
ports:
- 5000:80
links:
- redis
result:
image: result-app
ports:
- 5001:80
links:
- db
worker:
image: worker
links:
- db
- redis
#
# then
doker-compose up
# One more thing. If we don't have some images ready,
# We can specify how and where to build those images
# in the same docker-compose file
# generate docker-compose.yml with builds:
redis:
image: reids
db:
image: postgres:9.4
vote:
build: ./vote # build instead of image
ports:
- 5000:80
links:
- redis
result:
build: ./result # build instead of image
ports:
- 5001:80
links:
- db
worker:
build: ./worker # build instead of image
links:
- db
- redis
# New docker-compose.yaml with networks (v.2)
version: 2
services:
redis:
image: redis
networks:
- back-end
db:
image: postgres:9.4
networks:
- back-end
vote:
image: voting-app
networks:
- front-end
- back-end
result:
image: result
networks:
- front-end
- back-end
networks:
front-end:
back-end:
###############
### Volumes ###
###############
# docker files located in
# /var/lib/docker
# --volumes
# create volume
docker volume create data_volume
# mount volume with -v option (legacy) -- location within /var/lib/docker/
docker run -v data_volume:/var/lib/mysql mysql
# mount (bind) any location
docker run -v /data/mysql:/var/lib/mysql mysql
# mount volume in a modern way
docker run \
--mount type-bind,source=/data/mysql,target=/var/lib/mysql mysql
# note!, that mounting directory within /var/lib/docker/volumes -- means mounting volumes
# mounting any directory like /data/... -- means binding
##################
### Networking ###
##################
# there 3 networks:
# - bridge (default one) CIDR: 172.17.0.0/16
# - none (isolated) - not attached to any network, means fully isolated
# - host (means 'mapped to the host') - no isolation from host - no need to map any port
# create a networ
docker network create \
--driver bridge \
--subnet 182.18.0.0/16 \
custom-isolated-network
# check all the networks
docker network ls
# container association:
# runs ubuntu in [name] network
docker run ubuntu
docker run ubuntu --network=none
docker tun ubuntu --network=host
# example
# run mysql:5.6 images named 'mysql-db' in network 'wp-mysql-network'
# and set up enviroment variable (root password) MYSQL_ROOT_PASSWORD=db_pass123
docker run --name=mysql-db --network=wp-mysql-network -e MYSQL_ROOT_PASSWORD=db_pass123 mysql:5.6
###############
### The End ###
###############