Advanced Docker – Building Custom Images
& Multi-Container Applications
1. Recap of Last Lecture:
• Differences between Containers and Virtual Machines.
• How to run and manage Docker containers.
• Basics of Kubernetes and its components.
Discussion:
• What challenges did you face while running Docker containers?
• When should you use Docker vs. Kubernetes?
2. Building Custom Docker Images:
Why Build Custom Docker Images?
Pre-configured environments for applications.
Ensures consistency across different systems.
Enables automation and scalability.
Understanding Dockerfile
A Dockerfile is a script with instructions to build a custom image.
Example: Creating a Custom Docker Image for a Web Server
Create a new directory and navigate to it:
mkdir my-web-server && cd my-web-server
Create a Dockerfile:
# Use an official base image
FROM nginx:latest
# Copy website files to the container
COPY index.html /usr/share/nginx/html/
# Expose port 80
EXPOSE 80
Build and run the custom image:
docker build -t my-nginx .
docker run -d -p 8080:80 my-nginx
Check the running container:
docker ps
Access the web server at http://localhost:8080.
3. Managing Multi-Container Applications with Docker
Compose:
What is Docker Compose?
Docker Compose allows you to define and run multiple containers using a single
configuration file (docker-compose.yml).
Example: Running a Web App with a Database
Create a docker-compose.yml file:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
db:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: mydb
Start the multi-container application:
docker-compose up -d
Verify running containers:
docker-compose ps
Stop the application:
docker-compose down
4. Container Networking & Volumes:
Container Networking
Containers in the same Docker network can communicate using service names.
Useful for microservices architecture.
Create a custom Docker network:
docker network create my-network
Run containers in the same network:
docker run -d --name web --network my-network nginx
docker run -d --name db --network my-network mysql
Docker Volumes for Persistent Storage
Containers are stateless by default – data is lost when a container stops.
Volumes allow data persistence.
Create a volume and mount it:
docker volume create my-data
docker run -d -v my-data:/var/lib/mysql mysql
5. Lab Activity:
Build a custom Docker image for a basic web app.
Use Docker Compose to set up a multi-container environment with:
• NGINX (as a web server).
• MySQL (as a database).
Test communication between containers.
Configure persistent storage using Docker volumes.
6. Recap & Discussion:
What did we learn today?
• Building custom Docker images using Dockerfile.
• Managing multi-container applications with Docker Compose.
• Container networking & persistent storage.
Next Session Preview:
• Introduction to Virtualization Security.
• Securing Virtual Machines & Containers.
7. Homework / Preparation for Next Class
Research Docker security best practices.
Try deploying a real-world web app using Docker Compose.
Read about securing virtualized environments.