Skip to content

Commit a57bc26

Browse files
authored
Create lab6-docker-build.md
1 parent fe07ce8 commit a57bc26

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

lab6-docker-build.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
### **Assignment: Understanding `CMD`, `RUN`, and `ENTRYPOINT`**
2+
3+
**Objective**: Build a Docker image that demonstrates the use of `CMD`, `RUN`, and `ENTRYPOINT` directives effectively and understand their differences.
4+
5+
---
6+
7+
#### **Scenario**
8+
You are tasked with creating a Docker container for a simple Python application that performs mathematical calculations. The application should allow users to provide arguments at runtime to customize the calculations.
9+
10+
---
11+
12+
#### **Steps**
13+
14+
1. **Create a Python Script**
15+
Write a Python script (`calculator.py`) that can perform basic arithmetic operations (addition, subtraction, multiplication, and division).
16+
The script should accept arguments from the command line.
17+
18+
```python
19+
# calculator.py
20+
import sys
21+
22+
def main():
23+
if len(sys.argv) != 4:
24+
print("Usage: python calculator.py <operation> <num1> <num2>")
25+
sys.exit(1)
26+
27+
operation = sys.argv[1]
28+
num1 = float(sys.argv[2])
29+
num2 = float(sys.argv[3])
30+
31+
if operation == "add":
32+
print(f"The result is: {num1 + num2}")
33+
elif operation == "subtract":
34+
print(f"The result is: {num1 - num2}")
35+
elif operation == "multiply":
36+
print(f"The result is: {num1 * num2}")
37+
elif operation == "divide":
38+
if num2 != 0:
39+
print(f"The result is: {num1 / num2}")
40+
else:
41+
print("Error: Division by zero.")
42+
else:
43+
print("Error: Unsupported operation.")
44+
45+
if __name__ == "__main__":
46+
main()
47+
```
48+
49+
2. **Write the Dockerfile**
50+
Create a Dockerfile that demonstrates the use of `RUN`, `CMD`, and `ENTRYPOINT`.
51+
52+
```dockerfile
53+
# Use Python as the base image
54+
FROM python:3.10-slim
55+
56+
# Set the working directory inside the container
57+
WORKDIR /app
58+
59+
# Copy the Python script into the container
60+
COPY calculator.py /app/
61+
62+
# Install any dependencies (if needed)
63+
# Using RUN to demonstrate execution at build time
64+
RUN echo "Building the Docker image and setting up the environment..."
65+
66+
# Use ENTRYPOINT to ensure the script is always run with Python
67+
ENTRYPOINT ["python", "calculator.py"]
68+
69+
# Set a default CMD for the container
70+
CMD ["add", "10", "20"]
71+
```
72+
73+
3. **Build the Docker Image**
74+
Build the Docker image with a suitable tag.
75+
76+
```bash
77+
docker build -t calculator .
78+
```
79+
80+
4. **Run the Container**
81+
Run the container with and without overriding the default `CMD`.
82+
83+
- **Default CMD**:
84+
```bash
85+
docker run calculator
86+
```
87+
This should perform the default addition operation (10 + 20).
88+
89+
- **Override CMD**:
90+
```bash
91+
docker run calculator subtract 30 10
92+
```
93+
This should perform the subtraction operation (30 - 10).
94+
95+
5. **Experiment with ENTRYPOINT**
96+
Try overriding the `ENTRYPOINT` and see what happens:
97+
98+
```bash
99+
docker run --entrypoint echo calculator "Overriding entrypoint"
100+
```
101+
This should override the `ENTRYPOINT` and print "Overriding entrypoint".
102+
103+
6. **Document the Observations**
104+
Answer the following questions:
105+
- What happens when `CMD` is overridden?
106+
- What happens when `ENTRYPOINT` is overridden?
107+
- Why use `CMD` and `ENTRYPOINT` together?
108+
109+
---
110+
111+
#### **Deliverables**
112+
1. Dockerfile with properly configured `CMD`, `RUN`, and `ENTRYPOINT`.
113+
2. Screenshots or logs of running the container with various configurations.
114+
3. Written observations and answers to the provided questions.
115+

0 commit comments

Comments
 (0)