Docker Fundamentals: Installation, Image Management, and Container Operations
Docker packages applications along with their runtime dependencies into portbale, isolated units—enabling consistant execution across environments. Its architecture draws inspiration from physical containerization: each container opeartes independently, with strict process and filesystem boundaries enforced by Linux kernel features like namespaces and cgroups.
The core components are:
- Image: A read-only blueprint used to instantiate containers—similar to a class definition in object-oriented programming.
- Container: A runnable instance of an image—like an object created from a class.
- Registry: A centralized service for storing and distributing images; Docker Hub is the default public registry.
Prerequisites
Docker requires a 64-bit Linux kernel (version ≥3.8). Verify compatibility:
# Check OS version
cat /etc/os-release | grep "PRETTY_NAME"
# Confirm kernel version
uname -r
Installation on CentOS 8
- Remove legacy installations (if present):
sudo yum remove docker* -y
- Install build essentials:
sudo yum install -y gcc gcc-c++
- Configure Docker CE repository using Alibaba Cloud mirror:
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum makecache fast
- Install and activate Docker:
sudo yum install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
# Validate installation
docker version
docker run hello-world
Cleanup and Uninstallation
sudo systemctl stop docker
sudo yum remove -y docker-ce docker-ce-cli containerd.io
sudo rm -rf /var/lib/docker /var/lib/containerd
Configure Registry Mirroring
To accelerate image pulls, configure the Alibaba Cloud mirror by editing /etc/docker/daemon.json:
{
"registry-mirrors": ["https://<your-mirror-id>.mirror.aliyuncs.com"]
}
Then reload the daemon:
sudo systemctl daemon-reload
sudo systemctl restart docker
Essential Commands
Service Control
systemctl start docker # Start daemon
systemctl stop docker # Stop daemon
systemctl restart docker # Restart daemon
systemctl status docker # Check status
systemctl enable docker # Enable auto-start
System Information
docker info # Runtime metadata
docker --help # Global help
docker <command> --help # Command-specific help
Image Lifecycle
# List local images
docker images
# Search remote registry
docker search --limit 5 nginx
# Pull image (default: latest)
docker pull nginx:alpine
# Inspect disk usage
docker system df
# Remove single or multiple images
docker rmi nginx:alpine
docker rmi -f $(docker images -q)
Container Lifecycle
# Launch interactive Ubuntu shell
docker run -it --rm ubuntu:22.04 /bin/bash
# Run detached Nginx with port binding
docker run -d --name web -p 8080:80 nginx:alpine
# Manage running instances
docker ps -a # All containers (including stopped)
docker start web # Start stopped container
docker stop web # Graceful shutdown
docker kill web # Force termination
# Execute commands inside a running container
docker exec -it web sh
# Copy files between host and container
docker cp web:/usr/share/nginx/html/index.html ./
# Export container state (not image)
docker export web > backup.tar
# Import exported archive as new image
cat backup.tar | docker import - my-backup:latest
Practical Deployments
Tomcat 8 with JDK 8:
docker run -d --name tomcat8 -p 8080:8080 billygoo/tomcat8-jdk8
MySQL 5.7 with persistent volumes and UTF-8 configuration:
mkdir -p /xc/mysql/{log,data,conf}
docker run -d \
--name mysql01 \
-p 3306:3306 \
--privileged \
-v /xc/mysql/log:/var/log/mysql \
-v /xc/mysql/data:/var/lib/mysql \
-v /xc/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=hd123456 \
mysql:5.7
Create /xc/mysql/conf/my.cnf:
[client]
default_character_set = utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
Then restart:
docker restart mysql01