Docker Fundamentals: Imperative and Declarative Deployment
Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).
Objective
In this lab, students will:
Run Docker containers
Compare imperative and declarative deployment methods
Create a deployment template using Docker Compose
Explore Docker volumes
Demonstrate persistent storage
Relate Docker concepts to cloud computing concepts
By the end of this lab, students will understand how modern cloud applications are deployed and how persistent storage works.
Background
Docker allows applications to run inside lightweight containers.
Containers are commonly used in:
AWS ECS
AWS EKS
Azure Container Apps
Azure Kubernetes Service (AKS)
Google Cloud Run
Google Kubernetes Engine (GKE)
There are two common approaches to deploying containers:
Imperative Approach
The administrator issues commands directly.
Example:
docker run ...
docker stop ...
docker rm ...
This is similar to manually launching a cloud virtual machine.
Declarative Approach
The administrator describes the desired deployment in a configuration file.
Example:
compose.yaml
Docker automatically creates the required resources.
This is similar to:
AWS CloudFormation
Azure ARM Templates
Terraform
Part 1 – Verify Docker Desktop
Open Docker Desktop.
Verify:
Engine running
Part 2 – Verify Docker Installation
Open PowerShell.
Execute:
docker version
Verify both:
Client
Server
appear.
Part 3 – Explore Docker Desktop
Observe the following sections:
Containers
Images
Volumes
Discussion:
Docker Desktop provides a graphical interface for managing Docker resources.
Part 4 – Imperative Deployment
Step 1 – Run Python Web Server
Execute:
docker run -d `
--name python-web `
-p 8000:8000 `
python:3.9-slim `
python3 -m http.server 8000
Explanation:
| Option | Description |
|---|---|
| -d | Run in background |
| --name | Container name |
| -p | Port mapping |
| python:3.9-slim | Image |
| python3 -m http.server | Application |
Step 2 – Verify Container
Execute:
docker ps
Expected:
python-web
Step 3 – Open Browser
Browse to:
http://localhost:8000
A directory listing page should appear.
Step 4 – View Logs
Execute:
docker logs python-web
Observe web access activity.
Step 5 – Stop Container
docker stop python-web
Step 6 – Remove Container
docker rm python-web
Discussion
This deployment was performed manually.
To recreate it later, the administrator must remember the entire command.
This is known as an imperative deployment.
Part 5 – Declarative Deployment
Step 1 – Create Project Folder
mkdir docker-template-demo
cd docker-template-demo
Step 2 – Create compose.yaml
Create:
compose.yaml
Insert:
services:
python-web:
image: python:3.9-slim
container_name: python-web
command: python3 -m http.server 8000
ports:
- "8000:8000"
Save the file.
Step 3 – Deploy Template
Execute:
docker compose up -d
Docker reads the template and creates the container automatically.
Step 4 – Verify Deployment
docker compose ps
Expected:
python-web
Step 5 – Open Browser
Browse to:
http://localhost:8000
The web server should appear again.
Step 6 – Remove Deployment
docker compose down
Discussion
The deployment definition is stored inside:
compose.yaml
This is similar to:
AWS CloudFormation Templates
Azure ARM Templates
Terraform Files
Part 6 – Create Persistent Storage
Step 1 – Update compose.yaml
Replace the file contents with:
services:
python-web:
image: python:3.9-slim
container_name: python-web
command: python3 -m http.server 8000
ports:
- "8000:8000"
volumes:
- website-data:/data
volumes:
website-data:
Step 2 – Deploy Again
docker compose up -d
Step 3 – Verify Volume Creation
docker volume ls
Expected:
website-data
Part 7 – Explore Volume
Open container shell:
docker exec -it python-web sh
Verify volume contents:
ls /data
Observe:
(empty)
The volume is newly created.
Step 2 – Store Data
echo "Cloud Plus Storage Lab" > /data/storage.txt
Verify:
cat /data/storage.txt
Expected:
Cloud Plus Storage Lab
Exit:
exit
Part 8 – Demonstrate Persistence
Remove deployment:
docker compose down
Deploy again:
docker compose up -d
Open shell:
docker exec -it python-web sh
Verify:
cat /data/storage.txt
Expected:
Cloud Plus Storage Lab
The file still exists.
Exit:
exit
Discussion
The container was recreated.
The volume remained.
This is persistent storage.
Cloud Storage Comparison
| Docker Concept | Cloud Equivalent |
|---|---|
| Container | EC2 Instance |
| Docker Image | AMI |
| Compose Template | CloudFormation Template |
| Volume | EBS Volume |
| docker run | Manual Deployment |
| docker compose up | Template Deployment |
Reflection Questions
What is the difference between imperative and declarative deployment?
What advantages does a deployment template provide?
Why is Compose similar to CloudFormation?
Why did the volume survive when the container was recreated?
How is a Docker volume similar to AWS EBS?
Why is persistent storage important for applications?
Learning Outcomes
After completing this lab, students will be able to:
Deploy containers imperatively
Deploy containers declaratively
Create deployment templates using Docker Compose
Create and use Docker volumes
Demonstrate persistent storage
Relate Docker concepts to cloud infrastructure concepts
Understand template-based deployment strategies used in cloud environments