This exercise will help you explore containerization, Docker, and Kubernetes basics. You'll containerize a simple web application that displays an environment variable.
- Docker installed
- Minikube installed
- kubectl installed
- Docker Hub account
index.html
: Basic web pageserver.js
: Simple Node.js server
Create a Dockerfile that:
- Uses node:alpine as base image
- Sets up a working directory
- Copies both HTML and JS files
- Exposes port 3000
- Runs the Node.js server
# Build image (replace USERNAME with your Docker Hub username and provide a version number of your choice)
docker build -t USERNAME/capstone-web:<version> .
# Test locally
docker run -d -p 8080:3000 -e APPNAME="Local Test App" USERNAME/capstone-web:latest
# Verify in browser
open http://localhost:8080
# Login to Docker Hub
docker login
# Push image
docker push USERNAME/capstone-web:latest
Create a deployment.yaml that includes:
- Deployment resource
- 2 replicas
- Container image from your Docker Hub
- Environment variable APPNAME
- Container port 3000
- NodePort service
- Maps to port 3000
- NodePort type
# Start Minikube if not running
minikube start
# Apply deployment
kubectl apply -f deployment.yaml
# Get NodePort
kubectl get svc
# Get Minikube IP
minikube ip
Access your application at:
http://MINIKUBE_IP:NODEPORT
- Docker image builds successfully
- Application runs locally with Docker
- Application shows environment variable value
- Kubernetes deployment works
- Application accessible via NodePort
- Environment variable displays correctly in K8s deployment
- Use
docker ps
to check running containers - Use
docker logs CONTAINER_ID
for container logs - Use
kubectl get pods
to check pod status - Use
kubectl describe pod POD_NAME
for detailed pod info - Use
kubectl logs POD_NAME
for pod logs
# Stop local Docker container
docker ps
docker stop CONTAINER_ID
# Delete K8s deployment
kubectl delete -f deployment.yaml
# Stop Minikube (optional)
minikube stop