Comprehensive Docker Guide: Concepts, Installation, Management, and Practical Use Cases
This article provides a detailed overview of Docker, covering its definition, core components, advantages over virtual machines, common use cases, installation steps, storage configuration, image management commands, network modes, data volumes, private registry setup, backup procedures, and MySQL deployment, all illustrated with practical command examples.
1. What is Docker?
Docker is an open‑source container engine written in Go, allowing lightweight, portable, and isolated application environments on a host system.
2. Docker Use Cases
Automated packaging and release of web applications.
Continuous integration and testing.
Deploying databases and backend services.
Building private PaaS platforms.
It is especially useful for internal development environments, reducing resource waste compared to virtual machines.
3. Advantages of Docker
Flexibility: complex applications can be containerized.
Lightweight: containers share the host kernel.
Portability: build locally, deploy to cloud, run anywhere.
Scalability: easily increase container replicas.
Stackability: vertical and immediate service stacking.
4. Docker vs. Virtual Machines
VMs add a hypervisor layer and emulate hardware, each with its own kernel, while Docker uses namespaces and cgroups to isolate resources, resulting in lower overhead but slightly weaker isolation.
5. Core Components
Image
Read‑only templates that contain everything needed to run a container.
Container
Runtime instances created from images, isolated from each other.
Registry
Servers that store multiple images; public registry is Docker Hub ( https://hub.docker.com ), with many private options.
6. Quick Installation
Run the following commands to install Docker on CentOS:
yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[root@centos7 ~] yum -y install docker-ce docker-ce-cli containerd.io
# docker ps -- view Docker [root@centos7 ~]# systemctl enable docker
[root@centos7 ~]# systemctl start docker
[root@centos7 ~]# systemctl status docker
[root@centos7 ~]# docker ps -- view containers
[root@centos7 ~]# docker version
[root@centos7 ~]# docker info7. Changing Docker Storage Location
Default location: /var/lib/docker . Check with docker info | grep "Docker Root Dir" .
To move:
systemctl stop docker
mkdir -p /root/data/docker
mv /var/lib/docker /root/data/docker
ln -s /root/data/docker /var/lib/docker8. Image Management
Search Images
Use docker search <keyword> .
Pull Images
Use docker pull repository[:tag] ; default tag is latest .
Inspect Images
Image details are stored under /var/lib/docker . Key fields: REPOSITORY, TAG, IMAGE ID, CREATED, SIZE.
Tag and Delete Images
Tag: docker tag name:tag . Delete: docker rmi repository:tag or docker rmi IMAGE_ID [-f] .
Save and Load Images
Save: docker save -o filename.tar image . Load: docker load -i filename.tar .
9. Creating Containers
#docker images -- list images
docker run -d --name centos7.8 -h centos7.8 \
-p 220:22 -p 3387:3389 \
--privileged=true \
centos:7.8.2003 /usr/sbin/init
# Create a Linux 8.2 environment
docker run -d --name centos8.2 -h centos8.2 \
-p 230:22 -p 3386:3389 \
--privileged=true \
daocloud.io/library/centos:8.2.2004 init
# Enter container
docker exec -it centos7.8 bash
docker exec -it centos8.2 bash
cat /etc/redhat-release -- view OS version10. Docker Run Process
Check for local image; pull if missing.
Create and start a container from the image.
Mount a writable layer over the read‑only image.
Bridge a virtual network interface to the container.
Assign an IP address from the pool.
Execute the specified application; container stops when it exits.
11. Docker Network Modes
host
Use --net=host ; container shares the host’s network namespace.
container
Use --net=container:NAME_or_ID ; shares another container’s network namespace.
none
Use --net=none ; container gets no network interfaces.
bridge
Default mode; creates an isolated network namespace, connects to docker0 bridge, and sets up NAT with iptables.
12. Docker Data Volumes
Special directories inside containers that can be mounted from the host, allowing persistent data without affecting the image.
13. Setting Up a Private Registry
# Pull registry image
docker pull registry
# Run registry container
docker run -di --name registry -p 5000:5000 registry
docker update --restart=always registry # enable auto‑restart
# Verify
docker ps -a --format "table {{.ID}} {{.Names}} {{.Status}}"
# Access URL: http://192.168.1.54:5000/v2/_catalog
# Configure trust
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
"insecure-registries": ["192.168.1.54:5000"]
}
systemctl restart docker
# Push an image
docker tag postgres:11 192.168.1.54:5000/postgres
docker push 192.168.1.54:5000/postgres
# Pull it back
docker rmi 192.168.1.54:5000/postgres
docker pull 192.168.1.54:5000/postgres14. Backup and Migration
Save a container as an image, then export the image:
# Commit container to image
docker commit redis myredis
# Save image to file
docker save -o myredis.tar myredis
# Restore on another host
docker load -i myredis.tar15. Deploy MySQL with Docker
Download Image
# Pull MySQL images
docker pull mysql:5.7.30
docker pull mysql:8.0.20Run Containers
mkdir -p /usr/local/mysql5730/
mkdir -p /usr/local/mysql8020/
docker run -d --name mysql5730 -h mysql5730 \
-p 3309:3306 \
-v /usr/local/mysql5730/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=root -e TZ=Asia/Shanghai \
mysql:5.7.30
docker run -d --name mysql8020 -h mysql8020 \
-p 3310:3306 \
-v /usr/local/mysql8020/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=root -e TZ=Asia/Shanghai \
mysql:8.0.20Access MySQL inside the container:
# Enter container
docker exec -it mysql5730 bash
mysql -uroot -proot
# Remote access
mysql -uroot -proot -h192.168.59.220 -P3309Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.