Week 8 - Exercises
π Introduction
These exercises are designed to strengthen your understanding of:
- Docker fundamentals
- Docker commands
- Dockerfiles
- Containerization concepts
- Spring Boot deployment
- Docker Compose
- Debugging containerized applications
Complete the exercises in sequence. The difficulty level increases gradually.
π§ͺ Exercise 1 - Verify Docker Installation
Objective
Verify that Docker is installed correctly.
Tasks
- Install Docker Desktop or Docker Engine.
- Verify installation using command:
```bash id=βf5p1qgβ docker βversion
3. Run:
```bash id="0e1u3y"
docker run hello-world
Expected Outcome
You should see a successful hello-world message.
π§ͺ Exercise 2 - Explore Docker Commands
Objective
Learn commonly used Docker commands.
Tasks
Execute the following commands:
```bash id=βf1s4h6β docker ps
```bash id="r5s7m2"
docker ps -a
```bash id=βj7l8k2β docker images
```bash id="d8k9v1"
docker system info
Questions
- What is the difference between
docker psanddocker ps -a? - What information does
docker imagesprovide? - Why is
docker system infouseful?
π§ͺ Exercise 3 - Run an Ubuntu Container
Objective
Understand interactive containers.
Tasks
Run Ubuntu container interactively:
```bash id=βk3s9d2β docker run -it ubuntu bash
Inside container:
```bash id="x9d2f4"
ls
```bash id=βt2k5n8β pwd
```bash id="w4l7c3"
cat /etc/os-release
Exit container:
```bash id=βm1q7r9β exit
---
# Questions
1. What does `-it` mean?
2. Why do containers stop after exiting terminal?
3. What operating system was running inside the container?
---
# π§ͺ Exercise 4 - Run Nginx Container
# Objective
Understand port mapping.
---
# Tasks
Run Nginx:
```bash id="u6v3x1"
docker run -p 8080:80 nginx
Open browser:
```text id=βj2r4v7β http://localhost:8080
---
# Questions
1. Why does port mapping matter?
2. What happens if port 8080 is already in use?
3. What is the purpose of the Nginx image?
---
# π§ͺ Exercise 5 - Create Your First Dockerfile
# Objective
Create a Docker image using Dockerfile.
---
# Tasks
Create file:
```text id="f7u3d2"
Dockerfile
Add:
```dockerfile id=βe4m2n7β FROM openjdk:17
WORKDIR /app
COPY app.jar app.jar
ENTRYPOINT [βjavaβ, β-jarβ, βapp.jarβ]
---
# Questions
1. What is the purpose of `FROM`?
2. Why do we use `WORKDIR`?
3. What does `COPY` do?
4. What is the purpose of `ENTRYPOINT`?
---
# π§ͺ Exercise 6 - Build Docker Image
# Objective
Build image from Dockerfile.
---
# Tasks
Execute:
```bash id="n8f6v1"
docker build -t my-java-app .
Verify image:
```bash id=βq4k7m1β docker images
---
# Questions
1. What does `-t` represent?
2. Why is `.` used at the end of build command?
3. What happens during image build?
---
# π§ͺ Exercise 7 - Run Java Application Container
# Objective
Run Java application inside container.
---
# Tasks
Run image:
```bash id="r9x2z4"
docker run -p 8080:8080 my-java-app
Verify application accessibility.
Questions
- What does
8080:8080represent? - Why should application expose correct port?
- How can you stop running container?
π§ͺ Exercise 8 - View Logs
Objective
Learn container debugging basics.
Tasks
List running containers:
```bash id=βy2k5n7β docker ps
View logs:
```bash id="g6h8j1"
docker logs <container-id>
Questions
- Why are logs important?
- What types of issues can logs help identify?
- What happens if application crashes?
π§ͺ Exercise 9 - Execute Commands Inside Container
Objective
Interact with running container.
Tasks
Execute shell inside container:
```bash id=βm3v8r2β docker exec -it
Run commands:
```bash id="v8d4s2"
ls
```bash id=βh2m7x1β pwd
Exit shell:
```bash id="s5j9n2"
exit
Questions
- Why is container access useful?
- What debugging tasks can be done inside containers?
- Why should production containers remain lightweight?
π§ͺ Exercise 10 - Run MySQL Container
Objective
Run database using Docker.
Tasks
Run MySQL container:
```bash id=βw6x2c8β docker run -d
βname mysql-db
-p 3306:3306
-e MYSQL_ROOT_PASSWORD=password
mysql:8
Verify container:
```bash id="d8m1s5"
docker ps
Questions
- What does
-dmean? - Why are environment variables important?
- What is the purpose of
MYSQL_ROOT_PASSWORD?
π§ͺ Exercise 11 - Create docker-compose.yml
Objective
Learn multi-container setup.
Tasks
Create:
```text id=βr4v8m3β docker-compose.yml
Add:
```yaml id="p6q2x9"
version: '3'
services:
app:
image: my-java-app
ports:
- "8080:8080"
mysql:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: password
Run Compose
bash id="u5j3n7" docker compose up
Questions
- Why is Docker Compose useful?
- What problem does Compose solve?
- Why do microservices commonly use Docker Compose during development?
π§ͺ Exercise 12 - Debug Broken Configuration
Objective
Practice troubleshooting.
Tasks
Intentionally create issues:
- Wrong port mapping
- Incorrect database password
- Missing image
- Invalid Dockerfile instruction
Observe errors and fix them.
Questions
- What was the issue?
- How did you identify the issue?
- Which logs or commands helped?
π§ Reflection Questions
- Why is Docker important in backend engineering?
- What advantages do containers provide?
- How does Docker simplify deployment?
- Why is containerization important in cloud-native systems?
- What skills need more practice?
β Optional Advanced Exercises
Advanced Exercise 1
Use PostgreSQL instead of MySQL.
Advanced Exercise 2
Push image to Docker Hub.
Advanced Exercise 3
Add Spring Boot Actuator health endpoint.
Advanced Exercise 4
Create separate development and production Docker configurations.
π― Learning Outcome
After completing these exercises, you should be able to:
- Work confidently with Docker
- Build containerized applications
- Run backend services in containers
- Understand deployment workflows
- Troubleshoot common container problems
π License
This project is licensed under the GNU GPL-3.0 License.
Maintained by Aditya Pratap Bhuyan LinkedIn: https://linkedin.com/in/adityabhuyan