-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Docker-basics.html
578 lines (500 loc) · 38.3 KB
/
Docker-basics.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>Docker</title>
<meta content="" name="description">
<meta content="" name="keywords">
<!-- Favicons -->
<link href="assets/img/Favicon-1.png" rel="icon">
<link href="assets/img/Favicon-1.png" rel="apple-touch-icon">
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,500,500i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">
<!-- Vendor CSS Files -->
<link href="assets/vendor/aos/aos.css" rel="stylesheet">
<link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
<link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
<link href="assets/vendor/glightbox/css/glightbox.min.css" rel="stylesheet">
<link href="assets/vendor/swiper/swiper-bundle.min.css" rel="stylesheet">
<!-- Creating a python code section-->
<link rel="stylesheet" href="assets/css/prism.css">
<script src="assets/js/prism.js"></script>
<!-- Template Main CSS File -->
<link href="assets/css/style.css" rel="stylesheet">
<!-- To set the icon, visit https://fontawesome.com/account-->
<script src="https://kit.fontawesome.com/5d25c1efd3.js" crossorigin="anonymous"></script>
<!-- end of icon-->
<!-- =======================================================
* Template Name: iPortfolio
* Updated: Sep 18 2023 with Bootstrap v5.3.2
* Template URL: https://bootstrapmade.com/iportfolio-bootstrap-portfolio-websites-template/
* Author: BootstrapMade.com
* License: https://bootstrapmade.com/license/
======================================================== -->
</head>
<body>
<!-- ======= Mobile nav toggle button ======= -->
<i class="bi bi-list mobile-nav-toggle d-xl-none"></i>
<!-- ======= Header ======= -->
<header id="header">
<div class="d-flex flex-column">
<div class="profile">
<img src="assets/img/myphoto.jpeg" alt="" class="img-fluid rounded-circle">
<h1 class="text-light"><a href="index.html">Arun</a></h1>
<div class="social-links mt-3 text-center">
<a href="https://www.linkedin.com/in/arunp77/" target="_blank" class="linkedin"><i class="bx bxl-linkedin"></i></a>
<a href="https://github.com/arunp77" target="_blank" class="github"><i class="bx bxl-github"></i></a>
<a href="https://twitter.com/arunp77_" target="_blank" class="twitter"><i class="bx bxl-twitter"></i></a>
<a href="https://www.instagram.com/arunp77/" target="_blank" class="instagram"><i class="bx bxl-instagram"></i></a>
<a href="https://arunp77.medium.com/" target="_blank" class="medium"><i class="bx bxl-medium"></i></a>
</div>
</div>
<nav id="navbar" class="nav-menu navbar">
<ul>
<li><a href="index.html#hero" class="nav-link scrollto active"><i class="bx bx-home"></i> <span>Home</span></a></li>
<li><a href="index.html#about" class="nav-link scrollto"><i class="bx bx-user"></i> <span>About</span></a></li>
<li><a href="index.html#resume" class="nav-link scrollto"><i class="bx bx-file-blank"></i> <span>Resume</span></a></li>
<li><a href="index.html#portfolio" class="nav-link scrollto"><i class="bx bx-book-content"></i> <span>Portfolio</span></a></li>
<li><a href="index.html#skills-and-tools" class="nav-link scrollto"><i class="bx bx-wrench"></i> <span>Skills and Tools</span></a></li>
<li><a href="index.html#language" class="nav-link scrollto"><i class="bi bi-menu-up"></i> <span>Languages</span></a></li>
<li><a href="index.html#awards" class="nav-link scrollto"><i class="bi bi-award-fill"></i> <span>Awards</span></a></li>
<li><a href="index.html#professionalcourses" class="nav-link scrollto"><i class="bx bx-book-alt"></i> <span>Professional Certification</span></a></li>
<li><a href="index.html#publications" class="nav-link scrollto"><i class="bx bx-news"></i> <span>Publications</span></a></li>
<li><a href="index.html#extra-curricular" class="nav-link scrollto"><i class="bx bx-rocket"></i> <span>Extra-Curricular Activities</span></a></li>
<!-- <li><a href="#contact" class="nav-link scrollto"><i class="bx bx-envelope"></i> <span>Contact</span></a></li> -->
</ul>
</nav><!-- .nav-menu -->
</div>
</header><!-- End Header -->
<main id="main">
<!-- ======= Breadcrumbs ======= -->
<section id="breadcrumbs" class="breadcrumbs">
<div class="container">
<div class="d-flex justify-content-between align-items-center">
<strong>Data Engineering</strong>
<ol>
<li><a href="Data-engineering.html" class="clickable-box"><i class="fas fa-arrow-left"></i> Go to content </a></li>
<li><a href="index.html#portfolio" class="clickable-box">Go to Portfolio <i class="fas fa-arrow-right"></i> </a></li>
</ol>
</div>
</div>
</section><!-- End Breadcrumbs -->
<!------ right dropdown menue ------->
<div class="right-side-list">
<div class="dropdown">
<button class="dropbtn"><strong>Shortcuts:</strong></button>
<div class="dropdown-content">
<ul>
<li><a href="cloud-compute.html"><i class="fas fa-cloud"></i> Cloud</a></li>
<li><a href="AWS-GCP.html"><i class="fas fa-cloud"></i> AWS-GCP</a></li>
<li><a href="amazon-s3.html"><i class="fas fa-cloud"></i> AWS S3</a></li>
<li><a href="ec2-confi.html"><i class="fas fa-server"></i> EC2</a></li>
<li><a href="Docker-Container.html"><i class="fab fa-docker" style="color: rgb(22, 22, 22);"></i> Docker</a></li>
<li><a href="Jupyter-nifi.html"><i class="fab fa-python" style="color: rgb(15, 15, 15);"></i> Jupyter-nifi</a></li>
<li><a href="snowflake-task-stream.html"><i class="fas fa-snowflake"></i> Snowflake</a></li>
<li><a href="data-model.html"><i class="fas fa-database"></i> Data modeling</a></li>
<li><a href="sql-basics.html"><i class="fas fa-table"></i> QL</a></li>
<li><a href="sql-basic-details.html"><i class="fas fa-database"></i> SQL</a></li>
<li><a href="Bigquerry-sql.html"><i class="fas fa-database"></i> Bigquerry</a></li>
<li><a href="scd.html"><i class="fas fa-archive"></i> SCD</a></li>
<li><a href="sql-project.html"><i class="fas fa-database"></i> SQL project</a></li>
<!-- Add more subsections as needed -->
</ul>
</div>
</div>
</div>
<!-- ======= Portfolio Details Section ======= -->
<section id="portfolio-details" class="portfolio-details">
<div class="container">
<div class="row gy-4">
<h1>Fundamentals of Docker</h1>
<div class="image">
<figure style="text-align: center;">
<img src="assets/img/portfolio/docker-logo.png" alt="" style="max-width: 40%; max-height: auto;">
<figcaption style="text-align: center;"><strong>Image credit:</strong><a href="https://www.docker.com/company/newsroom/media-resources/" target="_blank"> docker.com</a></figcaption>
</figure>
</div>
<h2>Introduction</h2>
<ul>
<p>Docker is a software platform that enables developers to build, package, and run applications in lightweight, portable containers. It provides
a standardized environment for developing, deploying, and managing applications across different computing environments.</p>
<h3>What is Docker?</h3>
<p>Before we dive into the installation and setup, let's briefly explore what Docker is and why it has become a cornerstone of modern software development and deployment.</p>
<p>Docker is an open-source platform designed to automate the deployment, scaling, and management of applications within lightweight, portable, and self-sufficient containers.
These containers encapsulate everything an application needs to run, including code, libraries, dependencies, and runtime environments. Unlike traditional
virtual machines, Docker containers share the host OS kernel, which makes them extremely efficient and fast to start.</p>
<p>Docker provides an environment that enables developers to build, test, deploy, and run applications by using containerization technology.</p>
<!----------------------------------------->
<p>The key benefits of Docker include:</p>
<ol style="list-style-type: disc; margin-left: 30px;">
<li><b>Isolation:</b> Containers run in isolated environments, ensuring that applications don't interfere with each other.</li>
<li><b>Consistency:</b> Docker ensures that an application behaves the same way in development, testing, and production environments.</li>
<li><b>Portability:</b> Containers can run on any system that supports Docker, making it easy to move applications between different environments.</li>
<li><b>Scalability:</b> Dock
</ol><br>
<h3>Docker images and containers</h3>
Docker images are lightweight, stand-alone, executable software packages that include everything needed to run a piece of software: the code, runtime, libraries, environment variables, and configuration files.
<ul>
<li><b>Docker Image: </b>A read-only template with instructions for creating a Docker container. Images can be shared via Docker Hub or other registries.</li>
<li><b>Docker Container: </b>A runnable instance of a Docker image. Containers are isolated environments that run applications with their dependencies. Containers can be started, stopped, and removed without affecting the image.</li>
</ul>
<div class="box">
We can think of Docekr image as the blueprint of a Docker container.
</div>
<p>When we create a Docker container, we’re adding a writable layer on top of the Docker image. We can run many Docker containers from the same Docker image. We can see a Docker container as a runtime instance of a Docker image.</p>
<figure style="text-align: center;">
<img src="assets/img/data-engineering/container-image.png" alt="" style="max-width: 40%; max-height: auto;">
<figcaption style="text-align: center;"><strong>Image credit:</strong><a href="https://stackify.com/docker-build-a-beginners-guide-to-building-docker-images/?s=03" target="_blank"> stackify</a></figcaption>
</figure>
<!------------------------>
<h4>Dockerfile</h4>
A <code>Dockerfile</code> is a text file that contains a series of instructions used to build a Docker image. Each instruction in the Dockerfile defines a specific step that Docker follows to construct the image, layer by layer. When the Dockerfile is processed, Docker creates an image based on the commands written in it.
<p><b>Structure of a Dockerfile:</b>A typical Docekrfile includes:</p>
<ul>
<li><b>Base Image: </b>The starting point for your image.</li>
<li><b>Instructions: </b>Commands that copy files, install dependencies, and set up the environment.</li>
<li><b>Final Command: </b>The default command or entry point for the container when it's run.</li>
</ul>
<p><b>Key Instructions in a Dockerfile: </b>Let's consider a simple python Flask application and the try to understand how the DOcker images are created.</p>
<pre class="language-docker"><code>
# Step 1: Use an official Python runtime as a base image
FROM python:3.9-slim
# Step 2: Set the working directory inside the container
WORKDIR /app
# Step 3: Copy the requirements file from the host to the container
COPY requirements.txt .
# Step 4: Install dependencies inside the container
RUN pip install --no-cache-dir -r requirements.txt
# Step 5: Copy the current directory contents to /app in the container
COPY . .
# Step 6: Expose port 5000 (used by Flask)
EXPOSE 5000
# Step 7: Define the command to run the Flask app
CMD ["python", "app.py"]
</code></pre>
<p>Here’s a breakdown of common instructions in a Dockerfile:</p>
<ul>
<li><b>FROM: </b>This specifies the base image that your image will be built upon. The base image can be anything, from a bare OS (like ubuntu) to a specific language runtime (like python:3.9). For example in the present example: <code>FROM python:3.9-slim</code>, uses a minimal Python 3.9 environment as the base image.</li>
<li><b>WORKDIR: </b>Sets the working directory inside the Docker container. All subsequent instructions and paths in the Dockerfile will be relative to this directory. <code>WORKDIR /app</code> will creates and sets the <code>/app</code> directory as the working directory inside the container.</li>
<li><b>COPY: </b>This instruction copies files from your host machine into the Docker container. The source file or directory on your machine is specified first, followed by the destination path inside the container. For example, in our case, <code>COPY requirements.txt .</code> command copies the <code>requirements.txt</code> from the local machine into the current working directory <code>/app</code> in the container. The second <code>COPY . .</code> copies all the files from the current directory on the machine into <code>/app</code>.</li>
<li><b>RUN: </b>This executes a command in the shell during the build process. Commonly used to install dependencies or set up the environment. The <code>RUN pip install --no-cache-dir -r requirements.txt</code> installs the Python packages listed in <code>requirements.txt</code> using <code>pip</code>.</li>
<li><b>EXPOSE: </b>This informs Docker that the container listens on a specific network port at runtime. It doesn’t actually publish the port, but it serves as documentation for which ports should be exposed. The <code>EXPOSE 5000</code> declares that the container will listen on port 5000 (used by Flask in the present example).</li>
<li><b>CMD: </b>Specifies the command to run when the container starts. Unlike <code>RUN</code>, which is executed during the build, <code>CMD</code> is executed when the container is started. If multiple <code>CMD</code> instructions are provided, only the last one is used. The <code>CMD ["python", "app.py"]</code> runs the <code>python app.py</code> command when the container starts, which launches the Flask application.</li>
<li><b>ENTRYPOINT (Optional): </b>Similar to <code>CMD</code>, but it defines the main command to be executed when the container starts, and it can't be overridden. It is often combined with <code>CMD</code> for arguments.</li>
</ul>
So once the Dockerfile is ready, we can create the Docekr image using the following command:
<pre><code>
docker build -t my-python-app .
</code></pre>
This command reads the instructions in the Dockerfile and creates an image named <code>my-python-app</code>. So, a Dockerfile is essentially a recipe that tells Docker how to assemble an image. It’s written in a simple format that lets you define the base environment, install dependencies, copy files, and define what happens when the container is run.
<!----------------------->
<p><strong><code>docker-compose.yml</code>: </strong>The <code>docker-compose.yml</code> file is used with Docker Compose, a tool that helps define and manage multi-container Docker applications. Instead of managing each container separately, <code>docker-compose.yml</code> allows us to describe how our entire application—comprising multiple containers.</p>
<div style="background-color: #f0f0f0; padding: 15px; border-radius: 5px; border: 1px solid #000;">
<h5><strong>Virtual Machines Overview</strong></h5>
<p>
Virtual machines (VMs) are software-based emulations of physical computers. They allow the simultaneous
operation of multiple operating systems on a single physical machine, optimizing hardware resources.
</p>
<p>
<strong>Key Points:</strong>
<ul>
<li>VMs are managed by a hypervisor, facilitating the creation and control of virtualized environments.</li>
<li>Isolation is ensured, as each VM operates independently with its own virtualized hardware resources.</li>
<li>VMs support various guest operating systems, enabling diverse software environments on the same hardware.</li>
<li>Resource allocation by the hypervisor optimizes hardware use, allowing multiple VMs on a single server.</li>
</ul>
</p>
<p>
Virtual machines find applications in data centers for server consolidation, testing, development, and more.
They offer flexibility, allowing easy migration between physical servers and efficient resource management.
</p>
<p>
<em>Popular virtualization platforms include VMware, Microsoft Hyper-V, and open-source solutions like KVM.</em>
</p>
</div><br>
<div style="background-color: #66a8a56b; padding: 15px; border-radius: 5px; border: 1px solid #000;">
<h4><strong>Containerization:</strong></h4>
<ul>
<li><strong>Concept:</strong> Containerization is a lightweight form of virtualization that allows you to package and run applications and their dependencies in isolated units called containers.</li>
<li><strong>Key Points: </strong>
<ul>
<li>Containers encapsulate an application and its dependencies, ensuring consistency across different environments</li>
<li>They provide a standardized and efficient way to deploy, distribute, and manage applications.</li>
</ul>
</li>
</ul>
</div><br>
<div style="background-color: #f1f5f56b; padding: 15px; border-radius: 5px; border: 1px solid #111111;">
<strong>Hypervisor: </strong> A hypervisor, also known as a Virtual Machine Monitor (VMM), is a software or firmware that enables the creation and management of virtual machines (VMs) on a
physical host machine. It allows multiple operating systems to run concurrently on a single hardware platform. Hypervisors play a crucial role in virtualization technology, facilitating the
isolation and efficient allocation of resources for different virtualized environments. They are commonly used in cloud computing, server consolidation, and other scenarios where maximizing
hardware utilization and flexibility are key objectives. <br><br>
<p>In summary, the hypervisor is the layer that enables the creation and management of virtual machines on a physical host. VMs are the instances of virtualized operating systems and applications
that run on top of the hypervisor. The hypervisor provides the abstraction and control necessary to efficiently share and allocate resources among multiple VMs.</p>
</div><br>
<h4>Difference between the Virtual machines and Dockerr</h4>
<table>
<tr>
<th>Aspect</th>
<th>Docker Containers</th>
<th>Virtual Machines</th>
</tr>
<tr>
<td>Resource Utilization</td>
<td>Containers have faster startup times and consume fewer resources.</td>
<td>VMs require more resources due to the overhead of running multiple OS instances</td>
</tr>
<tr>
<td>Isolation</td>
<td>Containers share the OS kernel but are isolated from each other.</td>
<td>VMs provide stronger isolation as they run separate OS instances.</td>
</tr>
<tr>
<td>OS Dependency</td>
<td>Containers share the host OS kernel, making them more lightweight than VMs.</td>
<td>VMs include a full OS for each instance, leading to higher resource overhead.</td>
</tr>
<tr>
<td>Portability</td>
<td>Containers ensure consistent behavior across different environments, promoting portability.</td>
<td>VMs may face compatibility issues when moved between different hypervisors.</td>
</tr>
</table><br>
<h3>Common Use Cases of Docker:</h3>
<ol>
<li><strong>Microservices Architectures:</strong> Docker is widely used for implementing microservices architectures, where applications are broken down into smaller, independent services.
It is mostly used in Deploying Microservices Architecture in Cloud Environments (AWS, Azure).</li>
<li><strong>Continuous Integration and Continuous Delivery (CI/CD):</strong> Docker is integrated into CI/CD pipelines to automate application builds, deployments, and testing.</li>
<li><strong>Web Application Development:</strong> Docker is a popular choice for developing and deploying web applications, providing a consistent and portable environment.</li>
<li><strong>Legacy Application Modernization:</strong> Docker can be used to modernize legacy applications by packaging them into containers and running them in a modern environment.</li>
<li><strong>DevOps Practices:</strong> Docker plays a crucial role in DevOps practices, enabling rapid application development, testing, and deployment cycles.</li>
</ol>
<!-------------------Step by step guide to learn Docker conterization ------------------------->
<h4>Learn Docker: A Step-by-Step Guide</h4>
<ol>
<li>Start by visiting the <a href="https://www.docker.com/products/docker-desktop/" target="_blank">Docker desktop</a> download page to acquire the latest version compatible with your operating system, whether it's Windows, Linux, or macOS.</li>
<li>Download the installer provided on the Docker Desktop download page and execute it. Follow the on-screen instructions to complete the installation process. It's a straightforward process, and the installer will guide you through the necessary steps.</li>
<li>After the installation is complete, you can find Docker Desktop by searching for "docker" in your system's search bar. Launch Docker Desktop, and it will run on your local machine.</li>
<li>To fully utilize Docker's capabilities, consider creating an account on <a href="https://hub.docker.com/" target="_blank">docker hub</a>. This step is optional but recommended, as it allows you to manage and view the repositories associated with your Docker images on Docker Hub.</li>
<li>Now create a project and hence image and push it to docker repository.</li>
</ol>
<!------------------------------>
<section id ="project">
<h3>Simple Flask application</h3>
You can find the Docker hub repository at: <a href="https://hub.docker.com/r/arunp77/welcome-app" target="_blank">https://hub.docker.com/r/arunp77/welcome-app</a>.
<ul>
<li><strong>Step-1:</strong>Create a simple Flask application. For example, create a file named app.py with the following content:
<pre class="language-python"><code>
## flask app for hello world
from flask import Flask
import os
app = Flask(__name__)
@app.route('/', methods=['GET'])
def home():
return "Hello world! This is arun."
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port="5000")
</code></pre>
Here decorator <code>@app.route</code> is used to decorate the <code>home()</code> function, associated it with the '<code>/</code>' route and specifying that it responds to HTTP Ger requests. Howver, the <code>if __name__ == __main__</code>
ensures that the app is only run when the script is executed directly, not when it's imported as a module. The <code>app.run(debug=True, host="0.0.0.0", port="5000")</code>
section of the code runs the Flask app. <code>debug=True</code> enables debugging mode, <code>host="0.0.0.0"</code> makes the app accessible from external devices, and <code>port="5000"</code>
sets the port to 5000.
</li>
<li><strong>Step-2:</strong>Create a file named <code>requirements.txt</code> with the following content:
<pre class="language-docker"><code> Flask == 2.0.1</code></pre>
This file specifies the dependencies for your Flask app.
</li>
<li><strong>Step-3:</strong>Create a file named Dockerfile with the following content:
<pre class="language-text"><code>
FROM python:3.8-alpine
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD python app.py
</code></pre>
This Dockerfile specifies how to build your Docker image.
</li>
<li><strong>Step-4:</strong>Open a terminal, navigate to the directory containing your <code>Dockerfile</code>, <code>app.py</code>, and <code>requirements.txt</code>, and run the following command:
<pre class="language-bash"><code>$ docker build -t welcome-app .</code></pre>
<figure style="text-align: center;">
<img src="assets/img/data-engineering/docker-images.png" alt="" style="max-width: 90%; max-height: auto;">
<figcaption style="text-align: center;"><strong></strong><a href="" target="_blank"></a></figcaption>
</figure>
Here you will find all available images that you have created along with the image just you created.
</li>
<li><strong>Step-5:</strong>
After building the Docker image, you can run a container using the following command:
<pre class="language-bash"><code>$ docker run -p 5000:5000 welcome-app</code></pre>
This command runs the Docker container and maps port 5000 from the container to port 5000 on your host machine.
</li>
<li><strong>Step-6:</strong>Open a web browser and go to <a href="http://localhost:5000" target="_blank">http://localhost:5000</a>. You should see the message "Hello, world!".</li>
<li>Once you have already created the docker image, you should push it to docker hub.
<pre><code class="language-bash">$ docker push dockerusername/welcome-app:latest</code></pre>
replace 'dockerusername' with your actual docker username. The pushed repostory can be seen in your dockerhub as follows:
</li>
<figure style="text-align: center;">
<img src="assets/img/data-engineering/docker-welcom-app.png" alt="" style="max-width: 100%; max-height: auto;">
<figcaption style="text-align: center;"><strong></strong><a href="" target="_blank"></a></figcaption>
</figure>
</ul>
To stop a container just use, find the container id using <code>docker ps</code> (it will list all the images running). Then use following command to stop it:
<pre class="language-bash"><code>$ docker stop container_id</code></pre>
</section>
<!---------------------->
<section id="multi-container">
<h3>Multi-container docker image</h3>
<figure style="text-align: center;">
<img src="assets/img/data-engineering/docker-webapp.png" alt="" style="max-width: 100%; max-height: auto;">
<figcaption style="text-align: center;"><strong>Docker hub home </strong><a href="" target="_blank"></a></figcaption>
</figure>
<ul>
<li>A multi-container Docker image is an image that consists of multiple software components or services, each running in its own container within the same image. This allows you to package and distribute applications with their dependencies and services, ensuring consistency and ease of deployment.</li>
<li>In a multi-container Docker image, the individual containers within the image work together to provide a complete application or service. Each container has its own environment and runtime, but they can communicate with each other through defined channels like network connections or shared volumes.</li>
<li>This approach is particularly useful for applications that require multiple interconnected services to function. Instead of deploying each service separately, a multi-container Docker image encapsulates all the necessary components, making it easier to manage and deploy the entire application as a single unit.</li>
<li>Docker Compose is often used in conjunction with multi-container Docker images to define and manage the configuration of multiple services within an application. It simplifies the orchestration of these services, ensuring they start, stop, and interact with each other seamlessly.</li>
</ul>
<h5>Docker Compose:</h5>
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define a multi-container application in a single file, then spin up and manage all the containers defined in that file with a single command. This is particularly useful for complex applications where multiple services need to work together.
<h3>Flask-Redis Hello World App</h3>
<h5>Project Overview:</h5>
he Flask-Redis Hello World App is a simple web application built using Flask, a micro web framework for Python, and Redis, an in-memory data structure store. The project demonstrates how to create a basic web application that displays a "Hello World!" message and keeps track of the number of times the page has been accessed using a Redis database.
More details on the projects can be found at the <a href="https://hub.docker.com/r/arunp77/web-application" target="_blank">Docker hub repository</a>.
<h5>Components and files:</h5>
<ol>
<li><strong>app.py</strong>
<pre class="language-python"><code>
import time
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
# Corrected the typo here: 'cache.incr' should return the result
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5000)
</code></pre>
<ul>
<li>This is the main file of the project, containing the Flask application setup and routes.</li>
<li>It uses the <code>redis</code> library to interact with a Redis database.</li>
<li>The <code>get_hit_count</code> function increments a counter in the Redis database each time the page is accessed.</li>
<li>The '<code>/</code>' route displays a message along with the count of page visits.</li>
</ul>
</li>
<li><strong>requirements.txt: </strong>
<pre class="language-text"><code>
Flask
redis==3.5.3
</code></pre>
<ul>
<li>Lists the Python dependencies for the project.</li>
<li>Includes Flask for web development and Redis version 3.5.3 for Redis interactions.</li>
</ul>
</li>
<li><strong>Dockerfile: </strong>
<pre class="language-docker">
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
COPY . .
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["flask", "run"]
</pre>
<ul>
<li>Configures the Docker image for the project.</li>
<li>Uses the lightweight Python 3.7 Alpine image as the base.</li>
<li>Sets the working directory to '<code>/code</code>'.</li>
<li>Defines environment variables for Flask application configuration.</li>
<li>Copies the project files into the image and installs dependencies using <code>pip</code>.</li>
<li>Exposes port 5000 and sets the command to run the Flask application.</li>
</ul>
</li>
<li><strong>docker-compose.yml: </strong>
<pre>
version: "3.0"
services:
web:
build: .
image: web-app
ports:
- "5000:5000"
redis:
image: "redis:alpine"
</pre>
<ul>
<li>Defines services for the project using Docker Compose.</li>
<li>The '<code>web</code>' service builds the Docker image from the current directory, names it "web-app," and maps port 5000 to the host.</li>
<li>The '<code>redis</code>' service uses the official Redis image from Docker Hub.</li>
</ul>
</li>
</ol>
<h5>How to run the project:</h5>
<ul>
<li><strong>Build the Docker images:</strong> <pre><code>docker-compose build</code></pre></li>
<li><strong>Start the Docker Containers:</strong> <pre><code>docker-compose up</code></pre> Access the web application by navigating to http://localhost:5000 in a web browser.</li>
<li><strong>Stop the Docker Containers:</strong> <pre><code>docker-compose down</code></pre></li>
</ul>
<h5>Conclusion</h5>
The Flask-Redis Hello World App serves as a foundational project for those looking to understand the integration of Flask with Redis and containerization using Docker. It can be extended and modified based on specific requirements and serves as a starting point for more complex web applications.
</section>
<section id="reference">
<h3>Reference </h3>
<ul>
<li>For a best description of docker basics, you can look at youtube video: <a href="https://www.youtube.com/watch?v=8vmKtS8W7IQ&t=1861s" target="_blank">Youtube video</a>.</li>
</ul>
</section>
<div class="navigation">
<a href="Data-engineering.html" class="clickable-box">
<span class="arrow-right">Content section</span>
</a>
<a href="index.html#portfolio" class="clickable-box">
<span class="arrow-right">Portfolio</span>
</a>
</div>
</div>
</section><!-- End Portfolio Details Section -->
</main><!-- End #main -->
<!-- ======= Footer ======= -->
<footer id="footer">
<div class="container">
<div class="copyright">
© Copyright <strong><span>Arun</span></strong>
</div>
</div>
</footer><!-- End Footer -->
<a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>
<!-- Vendor JS Files -->
<script src="assets/vendor/purecounter/purecounter_vanilla.js"></script>
<script src="assets/vendor/aos/aos.js"></script>
<script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="assets/vendor/glightbox/js/glightbox.min.js"></script>
<script src="assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
<script src="assets/vendor/swiper/swiper-bundle.min.js"></script>
<script src="assets/vendor/typed.js/typed.umd.js"></script>
<script src="assets/vendor/waypoints/noframework.waypoints.js"></script>
<script src="assets/vendor/php-email-form/validate.js"></script>
<!-- Template Main JS File -->
<script src="assets/js/main.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
hljs.initHighlightingOnLoad();
});
</script>
</body>
</html>