Skip to content

Commit 9d90de6

Browse files
authored
Create lab2-build-concepts.md
1 parent 4819c9c commit 9d90de6

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

lab2-build-concepts.md

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
2+
### **Lab Title**
3+
**Exploring Docker Build: Environment Variables, Build Arguments, Tags, Layering, and Multi-Stage Builds**
4+
5+
---
6+
7+
### **Lab Objectives**
8+
1. Understand the use of build arguments (`ARG`) and environment variables (`ENV`) in Dockerfiles.
9+
2. Learn about tagging images and versioning.
10+
3. Explore Docker image layering and caching for efficient builds.
11+
4. Implement multi-stage builds to optimize image size.
12+
5. Apply best practices for Dockerfile creation.
13+
14+
---
15+
16+
### **Pre-requisites**
17+
1. Docker installed on your system.
18+
2. Basic knowledge of Docker commands and image creation.
19+
3. A code editor (e.g., VSCode).
20+
4. Git installed.
21+
22+
---
23+
24+
### **Lab Setup**
25+
Create a project folder structure:
26+
```
27+
docker-build-lab/
28+
├── stage-1-simple-build/
29+
│ └── Dockerfile
30+
├── stage-2-env-vars/
31+
│ ├── Dockerfile
32+
│ ├── app/
33+
│ │ └── app.py
34+
├── stage-3-build-args/
35+
│ ├── Dockerfile
36+
│ ├── config/
37+
│ │ └── config.txt
38+
├── stage-4-layering/
39+
│ ├── Dockerfile
40+
│ ├── app/
41+
│ │ └── main.py
42+
├── stage-5-multi-stage/
43+
│ ├── Dockerfile
44+
│ └── app/
45+
│ ├── requirements.txt
46+
│ └── app.py
47+
```
48+
49+
---
50+
51+
### **Stage 1: Simple Build**
52+
**Objective**: Build a basic image with a single layer.
53+
1. Create a `Dockerfile`:
54+
```dockerfile
55+
# Base image
56+
FROM alpine:3.17
57+
58+
# Add a command
59+
CMD ["echo", "Hello, Docker!"]
60+
```
61+
2. Build and run:
62+
```bash
63+
docker build -t simple-build .
64+
docker run simple-build
65+
```
66+
67+
---
68+
69+
### **Stage 2: Using Environment Variables**
70+
**Objective**: Illustrate the use of environment variables.
71+
1. Add the following `Dockerfile`:
72+
```dockerfile
73+
FROM python:3.9-slim
74+
75+
# Set environment variable
76+
ENV APP_ENV=development
77+
78+
# Copy and run the app
79+
COPY app/ /app
80+
WORKDIR /app
81+
CMD ["python", "app.py"]
82+
```
83+
2. Create a simple Python app:
84+
```python
85+
# app/app.py
86+
import os
87+
print(f"Running in {os.getenv('APP_ENV')} environment")
88+
```
89+
3. Build and run:
90+
```bash
91+
docker build -t env-example .
92+
docker run env-example
93+
```
94+
95+
---
96+
97+
### **Stage 3: Build Arguments**
98+
**Objective**: Use `ARG` to customize builds.
99+
1. Create a `Dockerfile`:
100+
```dockerfile
101+
FROM ubuntu:20.04
102+
103+
# Define build argument
104+
ARG VERSION="1.0"
105+
106+
# Use the argument
107+
RUN echo "Building version $VERSION"
108+
109+
CMD ["echo", "Build complete!"]
110+
```
111+
2. Build with different arguments:
112+
```bash
113+
docker build -t build-arg-example --build-arg VERSION=2.0 .
114+
```
115+
116+
---
117+
118+
### **Stage 4: Layering and Caching**
119+
**Objective**: Illustrate how layering works and optimize builds.
120+
1. Add the following `Dockerfile`:
121+
```dockerfile
122+
FROM python:3.9-slim
123+
124+
# Install dependencies
125+
RUN pip install flask
126+
127+
# Copy app code
128+
COPY app/ /app
129+
130+
# Set working directory and run the app
131+
WORKDIR /app
132+
CMD ["python", "main.py"]
133+
```
134+
2. Create a simple Flask app:
135+
```python
136+
# app/main.py
137+
from flask import Flask
138+
app = Flask(__name__)
139+
140+
@app.route("/")
141+
def hello():
142+
return "Hello from Flask!"
143+
144+
if __name__ == "__main__":
145+
app.run(host="0.0.0.0", port=5000)
146+
```
147+
3. Build and run:
148+
```bash
149+
docker build -t flask-app .
150+
docker run -p 5000:5000 flask-app
151+
```
152+
153+
---
154+
155+
### **Stage 5: Multi-Stage Build**
156+
**Objective**: Reduce image size using a multi-stage build.
157+
1. Create a `Dockerfile`:
158+
```dockerfile
159+
# Stage 1: Build dependencies
160+
FROM python:3.9-slim as builder
161+
162+
WORKDIR /app
163+
COPY app/requirements.txt .
164+
RUN pip install -r requirements.txt --target /app/dependencies
165+
166+
# Stage 2: Create final image
167+
FROM python:3.9-slim
168+
WORKDIR /app
169+
COPY --from=builder /app/dependencies /app/dependencies
170+
COPY app/ /app
171+
172+
ENV PYTHONPATH=/app/dependencies
173+
CMD ["python", "app.py"]
174+
```
175+
2. Add `requirements.txt`:
176+
```
177+
flask
178+
```
179+
3. Build and run:
180+
```bash
181+
docker build -t multi-stage-example .
182+
docker run -p 5000:5000 multi-stage-example
183+
```
184+
185+
---
186+
187+
### **Key Concepts to Explore in Each Stage**
188+
1. **Environment Variables**: Show how to pass runtime variables.
189+
2. **Build Arguments**: Customize image creation during the build process.
190+
3. **Image Tagging**: Use `-t` to version images.
191+
4. **Layering**: Modify the `Dockerfile` to see caching in action.
192+
5. **Multi-Stage Builds**: Reduce image size by separating build dependencies.
193+

0 commit comments

Comments
 (0)