Master Docker: From Fundamentals to Advanced Container Management
This comprehensive guide walks you through Docker's core concepts, installation on multiple operating systems, image handling, container lifecycle commands, building web services with Apache, Nginx, Python and MySQL, and advanced monitoring techniques using cAdvisor, Prometheus, Grafana, and Kubernetes, providing practical examples and command‑line snippets for each step.
Part 1: Docker Fundamentals and Application Scenarios
Docker containers are the basic unit of resource isolation and scheduling, encapsulating the entire runtime environment of a service. They enable fast, automated deployment, continuous integration, and high resource utilization across platforms.
Key benefits include continuous deployment, cross‑platform portability, efficient resource usage, environment standardization, version‑controlled images, and a public image repository that acts like an application store.
Part 2: Core Concepts and Installation
The three core concepts are Image , Container , and Repository . Understanding these is essential for mastering Docker's lifecycle.
2.1 Core Concepts
Images are read‑only templates (e.g., an Apache image). Containers are writable instances created from images. Repositories store and distribute images, similar to Git repositories.
2.2 Installing Docker
Docker runs on major Linux distributions, macOS, Windows, and cloud platforms.
RHEL 7 (64‑bit) – enable the extras repository and install:
sudo subscription-manager repos --enable=rhel-7-server-extras-rpms
sudo yum install dockerRHEL 6.5 – enable EPEL and install:
sudo yum -y install docker-io
sudo service docker start2.2.2 Installing on Windows
Download the Docker for Windows installer, run it to install VirtualBox, MSYS‑git, and Boot2Docker, then start the Docker daemon.
docker run hello-world2.2.3 Installing on CentOS
Supported versions: CentOS 7 (64‑bit) and CentOS 6.5+. Install via yum:
yum -y install docker
service docker start
docker run hello-worldPart 3: Working with Docker Images
Images are the most important Docker concept. You can pull, list, tag, inspect, save, load, and push images.
3.1 Pulling Images
docker pull ubuntu:latest3.2 Inspecting Images
docker images
docker inspect ubuntu:14.043.3 Searching Images
docker search --automated -s 3 nginx3.4 Deleting Images
docker rmi myubuntu:latest
# Force delete when no tags remain
docker rmi -f ubuntu:14.043.5 Creating Images
Three methods: commit from a container, import from a template, or build with a Dockerfile.
# Commit example
docker run -it ubuntu /bin/bash
# make changes inside container
exit
docker commit -m "Added file" -a "Docker Newbee" <container_id> myimage:0.13.6 Saving and Loading Images
# Save
docker save -o ubuntu_14.04.tar ubuntu:14.04
# Load
docker load -i ubuntu_14.04.tar3.7 Pushing Images
docker tag test:latest user/test:latest
docker push user/test:latestPart 4: Managing Docker Containers
Containers are runtime instances of images, providing isolated execution environments.
4.1 Creating Containers
docker create --name mycontainer ubuntu4.2 Starting Containers
docker start mycontainer4.3 Running Containers Directly
docker run -it ubuntu /bin/bash4.4 Daemonized Execution
docker run -d -p 80:80 httpd4.5 Stopping Containers
docker stop <container_id>4.6 Executing Commands Inside Containers
docker exec -it <container_id> /bin/bash4.7 Removing Containers
# Remove stopped container
docker rm <container_id>
# Force remove running container
docker rm -f <container_id>4.8 Exporting and Importing Containers
# Export
docker export -o mycontainer.tar <container_id>
# Import as image
docker import mycontainer.tar myimage:latest4.9 Port Mapping
# Random host port
docker run -d -P myapp
# Specific host port
docker run -d -p 5001:5002 myappPart 5: Docker for Web Services and Applications
5.1 Apache Service
Build a custom Apache image with a Dockerfile or pull the official
httpdimage.
# Build example
docker build -t myapache .
# Run with volume mounts
docker run -d -p 80:80 -v $(pwd)/www:/usr/local/apache2/htdocs -v $(pwd)/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf httpd5.2 Nginx Service
# Pull and run
docker pull nginx
docker run -d -p 80:80 -v $(pwd)/www:/www -v $(pwd)/conf/nginx.conf:/etc/nginx/nginx.conf nginx5.3 Python Application
# Build custom image
docker build -t pythonapp .
# Run with source mount
docker run -v $(pwd)/myapp:/usr/src/myapp -w /usr/src/myapp python:3.5 python helloworld.py5.4 MySQL Service
# Pull official image
docker pull mysql:5.6
# Run with data, config, and logs mounted
docker run -d -p 3306:3306 -v $(pwd)/data:/mysql_data -v $(pwd)/conf/my.cnf:/etc/mysql/my.cnf -v $(pwd)/logs:/logs -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6Part 6: Docker Runtime Monitoring
6.1 Monitoring Strategies
Traditional VM‑centric monitoring does not fit the dynamic nature of containers. Monitoring should be performed on the host, collecting per‑container metrics.
6.2 Single‑Host Monitoring
Use
docker statsfor real‑time metrics, or deploy cAdvisor for historical graphs.
6.3 Multi‑Host Monitoring
Combine cAdvisor, InfluxDB, and Grafana: each host runs cAdvisor, stores metrics in InfluxDB, and Grafana visualizes them.
6.4 Kubernetes Monitoring
Kubernetes includes cAdvisor; Heapster aggregates node metrics, and Kubedash (or the newer metrics‑server) provides dashboards.
6.5 Mesos Monitoring
Use
mesos‑exporterto expose metrics to Prometheus, which can then be visualized in Grafana.
6.6 Comparison of Tools
cAdvisor : per‑host, in‑memory storage, can export to InfluxDB.
Heapster : aggregates cAdvisor data across a Kubernetes cluster.
mesos‑exporter : focuses on Mesos task‑level metrics.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.