-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.html
More file actions
1826 lines (1588 loc) · 65.2 KB
/
Copy pathindex.html
File metadata and controls
1826 lines (1588 loc) · 65.2 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
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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Flask Dockerfile Guide - From venv to Docker Deployment</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
color: #333;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.header {
text-align: center;
color: white;
margin-bottom: 40px;
padding: 40px 0;
}
.header h1 {
font-size: 3rem;
margin-bottom: 20px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.header p {
font-size: 1.2rem;
opacity: 0.9;
}
.nav {
background: rgba(255,255,255,0.95);
border-radius: 15px;
padding: 20px;
margin-bottom: 30px;
box-shadow: 0 8px 32px rgba(0,0,0,0.1);
backdrop-filter: blur(10px);
}
.nav ul {
list-style: none;
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 20px;
}
.nav a {
text-decoration: none;
color: #667eea;
font-weight: 600;
padding: 10px 20px;
border-radius: 25px;
transition: all 0.3s ease;
border: 2px solid transparent;
}
.nav a:hover {
background: #667eea;
color: white;
transform: translateY(-2px);
}
.section {
background: rgba(255,255,255,0.95);
border-radius: 15px;
padding: 30px;
margin-bottom: 30px;
box-shadow: 0 8px 32px rgba(0,0,0,0.1);
backdrop-filter: blur(10px);
}
.section h2 {
color: #667eea;
font-size: 2.2rem;
margin-bottom: 20px;
border-bottom: 3px solid #667eea;
padding-bottom: 10px;
}
.section h3 {
color: #764ba2;
font-size: 1.5rem;
margin: 25px 0 15px 0;
}
.code-block {
background: #2d3748;
color: #e2e8f0;
padding: 20px;
border-radius: 10px;
margin: 20px 0;
font-family: 'Courier New', monospace;
overflow-x: auto;
border-left: 5px solid #667eea;
}
.code-block pre {
margin: 0;
white-space: pre-wrap;
}
.highlight {
background: linear-gradient(120deg, #a8edea 0%, #fed6e3 100%);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
border-left: 5px solid #667eea;
}
.analogy {
background: linear-gradient(120deg, #ffecd2 0%, #fcb69f 100%);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
border-left: 5px solid #ff6b6b;
}
.analogy h4 {
color: #d63031;
margin-bottom: 10px;
}
.interactive-demo {
background: linear-gradient(120deg, #a8edea 0%, #fed6e3 100%);
padding: 25px;
border-radius: 15px;
margin: 20px 0;
border: 2px dashed #667eea;
}
.demo-button {
background: #667eea;
color: white;
border: none;
padding: 12px 24px;
border-radius: 25px;
cursor: pointer;
font-size: 1rem;
margin: 10px 5px;
transition: all 0.3s ease;
}
.demo-button:hover {
background: #5a67d8;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}
.comparison-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: white;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
.comparison-table th {
background: #667eea;
color: white;
padding: 15px;
text-align: left;
}
.comparison-table td {
padding: 15px;
border-bottom: 1px solid #e2e8f0;
}
.comparison-table tr:hover {
background: #f7fafc;
}
.dockerfile-args {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
margin: 20px 0;
}
.arg-card {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
border-left: 5px solid #667eea;
}
.arg-card h4 {
color: #667eea;
margin-bottom: 10px;
}
.visual-diagram {
background: #f8f9fa;
padding: 30px;
border-radius: 15px;
margin: 20px 0;
text-align: center;
border: 2px solid #e9ecef;
}
.step-box {
background: #667eea;
color: white;
padding: 15px;
margin: 10px;
border-radius: 10px;
display: inline-block;
min-width: 150px;
text-align: center;
}
.arrow {
font-size: 2rem;
color: #667eea;
margin: 0 10px;
}
.toc {
background: rgba(255,255,255,0.9);
padding: 20px;
border-radius: 10px;
margin-bottom: 30px;
}
.toc h3 {
color: #667eea;
margin-bottom: 15px;
}
.toc ul {
list-style: none;
}
.toc li {
margin: 8px 0;
}
.toc a {
color: #667eea;
text-decoration: none;
padding: 5px 10px;
border-radius: 5px;
transition: all 0.3s ease;
}
.toc a:hover {
background: #667eea;
color: white;
}
.warning {
background: linear-gradient(120deg, #ff9a9e 0%, #fecfef 100%);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
border-left: 5px solid #ff6b6b;
}
.warning h4 {
color: #d63031;
margin-bottom: 10px;
}
.success {
background: linear-gradient(120deg, #a8edea 0%, #fed6e3 100%);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
border-left: 5px solid #00b894;
}
.success h4 {
color: #00b894;
margin-bottom: 10px;
}
@media (max-width: 768px) {
.header h1 {
font-size: 2rem;
}
.nav ul {
flex-direction: column;
align-items: center;
}
.dockerfile-args {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🐍 Python Flask Dockerfile Guide</h1>
<p>From Virtual Environments to Docker Deployment - Complete Tutorial</p>
</div>
<nav class="nav">
<ul>
<li><a href="#pip-issues">Pip Issues & venv</a></li>
<li><a href="#venv-deployment">venv Deployment</a></li>
<li><a href="#docker-deployment">Docker Deployment</a></li>
<li><a href="#dockerfile-explained">Dockerfile Explained</a></li>
<li><a href="#comparison">venv vs Docker</a></li>
<li><a href="#best-practices">Best Practices</a></li>
</ul>
</nav>
<div class="toc">
<h3>📋 Table of Contents</h3>
<ul>
<li><a href="#dockerfile-basics">What is Dockerfile & Docker Build</a></li>
<li><a href="#dockerfile-commands">Dockerfile Commands Explained</a></li>
<li><a href="#docker-args">Dockerfile Arguments Reference</a></li>
<li><a href="#when-to-use-commands">When to Use Which Commands</a></li>
<li><a href="#pip-problem">The Pip Problem You Encountered</a></li>
<li><a href="#venv-solution">Virtual Environment Solution</a></li>
<li><a href="#docker-deployment-steps">Your Docker Deployment</a></li>
<li><a href="#your-dockerfile">Your Dockerfile Explained</a></li>
<li><a href="#venv-vs-docker">venv vs Docker Comparison</a></li>
</ul>
</div>
<section id="dockerfile-basics" class="section">
<h2>🐳 What is Dockerfile & Docker Build?</h2>
<div class="analogy">
<h4>🏗️ Dockerfile = Blueprint for Your Application House</h4>
<p>Think of a Dockerfile like an architect's blueprint:</p>
<ul>
<li><strong>Blueprint (Dockerfile):</strong> Step-by-step instructions to build your house</li>
<li><strong>Construction (docker build):</strong> The actual building process</li>
<li><strong>House (Docker Image):</strong> The finished, moveable house</li>
<li><strong>Living in House (docker run):</strong> Actually using your house</li>
</ul>
</div>
<h3>What is a Dockerfile?</h3>
<div class="highlight">
<h4>📝 Dockerfile Definition</h4>
<p>A <strong>Dockerfile</strong> is a text file containing a series of instructions that Docker uses to automatically build a Docker image. It's like a recipe that tells Docker exactly how to create your application environment.</p>
</div>
<h3>What is Docker Build?</h3>
<div class="code-block">
<pre># The command you use to build your image
docker build -t my-app .
# What this does:
# - Reads Dockerfile in current directory (.)
# - Follows each instruction step by step
# - Creates layers for each instruction
# - Produces final image named "my-app"</pre>
</div>
<div class="visual-diagram">
<h3>Docker Build Process</h3>
<div class="step-box">📝 Dockerfile</div>
<span class="arrow">→</span>
<div class="step-box">🔨 docker build</div>
<span class="arrow">→</span>
<div class="step-box">📦 Docker Image</div>
<span class="arrow">→</span>
<div class="step-box">🚀 docker run</div>
<span class="arrow">→</span>
<div class="step-box">🏠 Running Container</div>
</div>
<h3>Real-World Example</h3>
<div class="code-block">
<pre># Simple Dockerfile example
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y python3
COPY app.py /app/
WORKDIR /app
CMD ["python3", "app.py"]
# Build command
docker build -t my-python-app .
# Run command
docker run my-python-app</pre>
</div>
</section>
<section id="dockerfile-commands" class="section">
<h2>📋 Dockerfile Commands Explained</h2>
<div class="analogy">
<h4>🏠 Building a House Analogy</h4>
<p>Each Dockerfile command is like a step in building a house:</p>
</div>
<h3>Essential Dockerfile Commands</h3>
<div class="dockerfile-args">
<div class="arg-card">
<h4>FROM</h4>
<p><strong>What it does:</strong> Sets the base image (foundation)</p>
<p><strong>Analogy:</strong> Choosing the plot of land for your house</p>
<p><strong>Examples:</strong></p>
<div class="code-block">
<pre>FROM ubuntu:20.04
FROM python:3.11-slim
FROM node:18-alpine
FROM nginx:latest</pre>
</div>
</div>
<div class="arg-card">
<h4>WORKDIR</h4>
<p><strong>What it does:</strong> Sets working directory</p>
<p><strong>Analogy:</strong> Deciding which room to work in</p>
<p><strong>Examples:</strong></p>
<div class="code-block">
<pre>WORKDIR /app
WORKDIR /usr/src/app
WORKDIR /home/user</pre>
</div>
</div>
<div class="arg-card">
<h4>COPY</h4>
<p><strong>What it does:</strong> Copies files from host to container</p>
<p><strong>Analogy:</strong> Moving furniture into your house</p>
<p><strong>Examples:</strong></p>
<div class="code-block">
<pre>COPY app.py .
COPY package.json ./
COPY . /app/</pre>
</div>
</div>
<div class="arg-card">
<h4>RUN</h4>
<p><strong>What it does:</strong> Executes commands during build</p>
<p><strong>Analogy:</strong> Installing utilities in your house</p>
<p><strong>Examples:</strong></p>
<div class="code-block">
<pre>RUN apt-get update
RUN pip install -r requirements.txt
RUN npm install</pre>
</div>
</div>
<div class="arg-card">
<h4>CMD</h4>
<p><strong>What it does:</strong> Default command when container starts</p>
<p><strong>Analogy:</strong> Your daily routine when you wake up</p>
<p><strong>Examples:</strong></p>
<div class="code-block">
<pre>CMD ["python", "app.py"]
CMD ["npm", "start"]
CMD ["nginx", "-g", "daemon off;"]</pre>
</div>
</div>
<div class="arg-card">
<h4>EXPOSE</h4>
<p><strong>What it does:</strong> Documents which ports the app uses</p>
<p><strong>Analogy:</strong> Putting a sign on your door with your address</p>
<p><strong>Examples:</strong></p>
<div class="code-block">
<pre>EXPOSE 3000
EXPOSE 5000
EXPOSE 80 443</pre>
</div>
</div>
</div>
<h3>Advanced Dockerfile Commands</h3>
<div class="dockerfile-args">
<div class="arg-card">
<h4>ENV</h4>
<p><strong>What it does:</strong> Sets environment variables</p>
<p><strong>Analogy:</strong> Setting up your house's climate control</p>
<div class="code-block">
<pre>ENV NODE_ENV=production
ENV PYTHONPATH=/app
ENV PORT=3000</pre>
</div>
</div>
<div class="arg-card">
<h4>ADD</h4>
<p><strong>What it does:</strong> Copies files with extra features</p>
<p><strong>Analogy:</strong> Moving furniture AND unpacking it</p>
<div class="code-block">
<pre>ADD https://example.com/file.tar.gz /tmp/
ADD app.tar.gz /app/</pre>
</div>
</div>
<div class="arg-card">
<h4>USER</h4>
<p><strong>What it does:</strong> Sets user for subsequent commands</p>
<p><strong>Analogy:</strong> Deciding who owns the house</p>
<div class="code-block">
<pre>USER node
USER appuser
USER 1001</pre>
</div>
</div>
<div class="arg-card">
<h4>VOLUME</h4>
<p><strong>What it does:</strong> Creates mount point for data</p>
<p><strong>Analogy:</strong> Designating a storage room</p>
<div class="code-block">
<pre>VOLUME ["/data"]
VOLUME ["/app/logs"]</pre>
</div>
</div>
<div class="arg-card">
<h4>LABEL</h4>
<p><strong>What it does:</strong> Adds metadata to image</p>
<p><strong>Analogy:</strong> Putting a nameplate on your house</p>
<div class="code-block">
<pre>LABEL version="1.0"
LABEL maintainer="your-email@example.com"</pre>
</div>
</div>
<div class="arg-card">
<h4>ARG</h4>
<p><strong>What it does:</strong> Defines build-time variables</p>
<p><strong>Analogy:</strong> Customizing your house during construction</p>
<div class="code-block">
<pre>ARG NODE_VERSION=18
ARG PYTHON_VERSION=3.11</pre>
</div>
</div>
</div>
</section>
<section id="docker-args" class="section">
<h2>📚 Complete Dockerfile Arguments Reference</h2>
<h3>Build-Time vs Runtime Commands</h3>
<div class="analogy">
<h4>🏗️ Construction vs Living Analogy</h4>
<p><strong>Build-time commands</strong> = What you do while building the house (FROM, RUN, COPY, etc.)</p>
<p><strong>Runtime commands</strong> = What happens when someone lives in the house (CMD, ENTRYPOINT)</p>
</div>
<table class="comparison-table">
<thead>
<tr>
<th>Command</th>
<th>When Used</th>
<th>Purpose</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>FROM</strong></td>
<td>Build-time</td>
<td>Base image</td>
<td><code>FROM python:3.11</code></td>
</tr>
<tr>
<td><strong>WORKDIR</strong></td>
<td>Build-time</td>
<td>Set directory</td>
<td><code>WORKDIR /app</code></td>
</tr>
<tr>
<td><strong>COPY</strong></td>
<td>Build-time</td>
<td>Copy files</td>
<td><code>COPY . .</code></td>
</tr>
<tr>
<td><strong>RUN</strong></td>
<td>Build-time</td>
<td>Execute commands</td>
<td><code>RUN pip install</code></td>
</tr>
<tr>
<td><strong>CMD</strong></td>
<td>Runtime</td>
<td>Default command</td>
<td><code>CMD ["python", "app.py"]</code></td>
</tr>
<tr>
<td><strong>ENTRYPOINT</strong></td>
<td>Runtime</td>
<td>Fixed entry point</td>
<td><code>ENTRYPOINT ["python"]</code></td>
</tr>
<tr>
<td><strong>EXPOSE</strong></td>
<td>Documentation</td>
<td>Document ports</td>
<td><code>EXPOSE 3000</code></td>
</tr>
<tr>
<td><strong>ENV</strong></td>
<td>Both</td>
<td>Environment variables</td>
<td><code>ENV NODE_ENV=prod</code></td>
</tr>
</tbody>
</table>
<h3>CMD vs ENTRYPOINT - The Key Difference</h3>
<div class="analogy">
<h4>🍽️ Restaurant Menu Analogy</h4>
<p><strong>CMD</strong> = Default dish that can be completely replaced</p>
<p><strong>ENTRYPOINT</strong> = Restaurant's signature style that always remains</p>
</div>
<div class="code-block">
<pre># CMD Example - Can be overridden
FROM ubuntu:20.04
CMD ["echo", "Hello World"]
# Build and run:
docker build -t cmd-demo .
docker run cmd-demo
# Output: Hello World
# Override CMD:
docker run cmd-demo echo "Custom Message"
# Output: Custom Message
# ENTRYPOINT Example - Cannot be overridden
FROM ubuntu:20.04
ENTRYPOINT ["echo", "Restaurant says:"]
# Build and run:
docker build -t entrypoint-demo .
docker run entrypoint-demo
# Output: Restaurant says:
# Add arguments:
docker run entrypoint-demo "Welcome!"
# Output: Restaurant says: Welcome!</pre>
</div>
</section>
<section id="dockerfile-confusions" class="section">
<h2>🤔 Common Dockerfile Confusions Explained</h2>
<div class="analogy">
<h4>🏠 Building a House - Common Mistakes</h4>
<p>Just like building a house, there are common mistakes people make with Dockerfile commands. Let's clear up the confusion!</p>
</div>
<h3>1. COPY vs ADD - The Great Confusion</h3>
<div class="warning">
<h4>❌ Common Mistake: Using ADD when COPY is better</h4>
<p>Many people use ADD for everything, but COPY is usually the better choice!</p>
</div>
<div class="dockerfile-args">
<div class="arg-card">
<h4>COPY - Simple File Moving</h4>
<p><strong>Analogy:</strong> Moving furniture from your old house to new house</p>
<p><strong>What it does:</strong> Simply copies files from host to container</p>
<div class="code-block">
<pre># ✅ Good - Simple file copying
COPY app.py .
COPY package.json ./
COPY . /app/
# What happens:
# - Copies files exactly as they are
# - No automatic extraction
# - No URL downloading
# - Fast and predictable</pre>
</div>
</div>
<div class="arg-card">
<h4>ADD - Advanced File Moving</h4>
<p><strong>Analogy:</strong> Moving furniture AND unpacking boxes automatically</p>
<p><strong>What it does:</strong> Copies files with extra features</p>
<div class="code-block">
<pre># ⚠️ Use only when needed
ADD app.tar.gz /app/ # Auto-extracts tar files
ADD https://example.com/file /tmp/ # Downloads from URL
ADD config.json /app/ # Works like COPY
# What happens:
# - Can download from URLs
# - Auto-extracts compressed files
# - Can be unpredictable
# - Slower than COPY</pre>
</div>
</div>
</div>
<div class="highlight">
<h4>💡 Rule of Thumb: Use COPY, Use ADD only when you need its special features</h4>
<div class="code-block">
<pre># ✅ Good examples
COPY . . # Simple file copy
COPY package.json . # Copy specific file
ADD app.tar.gz /app/ # Extract compressed file
ADD https://file.com /tmp/ # Download from URL
# ❌ Bad examples
ADD . . # Using ADD for simple copy
ADD package.json . # Using ADD for single file</pre>
</div>
<h3>2. CMD vs ENTRYPOINT - The Ultimate Confusion</h3>
<div class="analogy">
<h4>🍽️ Restaurant Analogy</h4>
<p><strong>CMD</strong> = Default dish that can be completely replaced</p>
<p><strong>ENTRYPOINT</strong> = Restaurant's signature style that always remains</p>
</div>
<div class="dockerfile-args">
<div class="arg-card">
<h4>CMD - Default Command (Can be Overridden)</h4>
<p><strong>Analogy:</strong> Default meal that can be completely changed</p>
<div class="code-block">
<pre># Dockerfile
FROM ubuntu:20.04
CMD ["echo", "Hello World"]
# Build and run
docker build -t cmd-demo .
docker run cmd-demo
# Output: Hello World
# Override the command completely
docker run cmd-demo echo "Custom Message"
# Output: Custom Message (CMD is completely replaced)</pre>
</div>
</div>
<div class="arg-card">
<h4>ENTRYPOINT - Fixed Entry Point (Cannot be Overridden)</h4>
<p><strong>Analogy:</strong> Restaurant's signature style that always remains</p>
<div class="code-block">
<pre># Dockerfile
FROM ubuntu:20.04
ENTRYPOINT ["echo", "Restaurant says:"]
# Build and run
docker build -t entrypoint-demo .
docker run entrypoint-demo
# Output: Restaurant says:
# Add arguments (ENTRYPOINT stays, arguments are added)
docker run entrypoint-demo "Welcome!"
# Output: Restaurant says: Welcome!</pre>
</div>
</div>
</div>
<h3>3. CMD + ENTRYPOINT Combination</h3>
<div class="analogy">
<h4>🍽️ Restaurant with Default Menu</h4>
<p><strong>ENTRYPOINT</strong> = Restaurant's signature style</p>
<p><strong>CMD</strong> = Default menu item</p>
<p><strong>Result</strong> = You can order the default or something custom</p>
</div>
<div class="code-block">
<pre># Dockerfile with both
FROM ubuntu:20.04
ENTRYPOINT ["echo", "Restaurant says:"]
CMD ["Default dish"]
# Build and run
docker build -t combo-demo .
# Run with default
docker run combo-demo
# Output: Restaurant says: Default dish
# Run with custom order
docker run combo-demo "Special pasta"
# Output: Restaurant says: Special pasta</pre>
</div>
<h3>4. RUN vs CMD - Build Time vs Runtime</h3>
<div class="analogy">
<h4>🏗️ Construction vs Living</h4>
<p><strong>RUN</strong> = What you do while building the house (install utilities, setup rooms)</p>
<p><strong>CMD</strong> = What happens when someone lives in the house (daily routine)</p>
</div>
<div class="dockerfile-args">
<div class="arg-card">
<h4>RUN - Build Time (During Construction)</h4>
<p><strong>When:</strong> While building the image</p>
<p><strong>Purpose:</strong> Install packages, setup environment</p>
<div class="code-block">
<pre># These run during docker build
RUN apt-get update
RUN pip install -r requirements.txt
RUN npm install
RUN mkdir /app</pre>
</div>
</div>
<div class="arg-card">
<h4>CMD - Runtime (When Living in House)</h4>
<p><strong>When:</strong> When container starts</p>
<p><strong>Purpose:</strong> Start your application</p>
<div class="code-block">
<pre># This runs when container starts
CMD ["python", "app.py"]
CMD ["npm", "start"]
CMD ["nginx", "-g", "daemon off;"]</pre>
</div>
</div>
</div>
<h3>5. EXPOSE vs Port Mapping</h3>
<div class="analogy">
<h4>🏠 House Address vs Mail Delivery</h4>
<p><strong>EXPOSE</strong> = Putting your house number on the door (documentation)</p>
<p><strong>Port Mapping</strong> = Actually connecting your house to the street (docker run -p)</p>
</div>
<div class="code-block">
<pre># Dockerfile
FROM python:3.11-slim
EXPOSE 5000
CMD ["python", "app.py"]
# EXPOSE just documents the port
# It doesn't actually make it accessible!
# To make it accessible, use port mapping:
docker run -p 5000:5000 my-app
# -p 5000:5000 means: host port 5000 → container port 5000
# Without -p flag:
docker run my-app
# App runs but not accessible from outside!</pre>
</div>
<h3>6. ENV vs ARG - Environment vs Build Variables</h3>
<div class="analogy">
<h4>🏠 House Settings vs Construction Options</h4>
<p><strong>ENV</strong> = House settings that stay after construction (thermostat, lighting)</p>
<p><strong>ARG</strong> = Construction options used only during building (paint color, room size)</p>
</div>
<div class="dockerfile-args">
<div class="arg-card">
<h4>ENV - Runtime Environment Variables</h4>
<p><strong>When:</strong> Available during build AND runtime</p>
<p><strong>Purpose:</strong> Configure your application</p>
<div class="code-block">
<pre># Available in container when running
ENV NODE_ENV=production
ENV PORT=3000
ENV DATABASE_URL=postgres://...
# Your app can access these:
# process.env.NODE_ENV
# process.env.PORT</pre>
</div>
</div>
<div class="arg-card">
<h4>ARG - Build-time Variables</h4>
<p><strong>When:</strong> Only available during build</p>
<p><strong>Purpose:</strong> Customize the build process</p>
<div class="code-block">
<pre># Only available during docker build
ARG NODE_VERSION=18
ARG PYTHON_VERSION=3.11
FROM node:${NODE_VERSION}-alpine
FROM python:${PYTHON_VERSION}-slim
# Build with custom values:
docker build --build-arg NODE_VERSION=20 -t my-app .</pre>
</div>
</div>
</div>
<h3>7. WORKDIR vs RUN cd</h3>
<div class="analogy">
<h4>🏠 Room Selection vs Walking to Room</h4>
<p><strong>WORKDIR</strong> = Setting your home office as the default room</p>
<p><strong>RUN cd</strong> = Walking to a room for one task, then leaving</p>
</div>
<div class="code-block">
<pre># ✅ Good - WORKDIR sets default directory
WORKDIR /app
COPY . .
RUN npm install
# ❌ Bad - RUN cd doesn't persist
RUN cd /app
COPY . . # This copies to root, not /app!
RUN npm install # This runs in root, not /app!
# ✅ Good - WORKDIR persists for all commands
WORKDIR /app
COPY . . # Copies to /app
RUN npm install # Runs in /app</pre>
</div>
<h3>8. Multi-line RUN vs Single RUN</h3>
<div class="analogy">
<h4>🏗️ Construction Efficiency</h4>
<p><strong>Single RUN</strong> = Doing all construction work in one visit</p>
<p><strong>Multiple RUN</strong> = Making multiple construction visits</p>
</div>
<div class="code-block">
<pre># ❌ Bad - Creates multiple layers
RUN apt-get update
RUN apt-get install -y python3
RUN apt-get install -y pip
RUN apt-get clean
# ✅ Good - Single layer
RUN apt-get update && \
apt-get install -y python3 pip && \
apt-get clean
# Why? Each RUN creates a new layer
# More layers = larger image size
# Single RUN = smaller image</pre>
</div>
<h3>9. COPY Order for Better Caching</h3>
<div class="analogy">
<h4>🏠 Smart Construction Order</h4>
<p>Copy dependency files first (like installing utilities), then copy your code (like moving furniture)</p>
</div>
<div class="code-block">
<pre># ❌ Bad order - changes to code invalidate npm install
COPY . .
RUN npm install
# ✅ Good order - dependencies cached separately
COPY package*.json ./
RUN npm install
COPY . .
# Why? Docker caches each layer
# If package.json doesn't change, npm install is cached
# Only rebuilds when package.json changes</pre>
</div>
<h3>10. USER for Security</h3>
<div class="analogy">
<h4>🏠 House Ownership</h4>
<p>Don't run as root (like giving everyone master keys to your house)</p>
</div>
<div class="code-block">
<pre># ❌ Bad - Running as root
FROM python:3.11-slim
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
# ✅ Good - Create and use non-root user
FROM python:3.11-slim
RUN adduser --disabled-password --gecos '' appuser