Host Environment Setup
Verify the Linux kernel version (requires 3.10 or higher):
uname -rRefresh the system package cache:
yum clean all && yum makecacheSet up the Aliyun Docker CE repository:
curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repoQuery available Docker CE versions:
yum list docker-ce showduplicates | sort -rConfigure the Docker daemon to use a domestic registry mirror for faster pulls:
mkdir -p /etc/docker
touch /etc/docker/daemon.
vim /etc/docker/daemon.Insert the mirror configuration:
{
"registry-mirrors": [
"https://8xpk5wnt.mirror.aliyuncs.com"
]
}Reload the daemon and restart Docker:
systemctl daemon-reload
systemctl enable docker
systemctl restart dockerVerify Docker process details:
ps -ef | grep dockerRetrieve comprehensive Docker system information, including storage paths:
docker infoExplore image storage on the host:
ls /var/lib/docker/image/overlay2/imagedb/content/sha256/ -lImage Management
Search for images in the repository:
docker search httpdDownload an image from the registry:
docker pull httpd:2.4List all locally downloaded images:
docker image ls
# or
docker imagesDisplay only image IDs:
docker images -qFormat the output to show specific fields:
docker images --format "{{.Repository}}--{{.ID}}"Remove a specific image:
docker rmi <image_id>Batch delete all images:
docker rmi $(docker images -aq)Export an image to a compressed tar archive:
docker image save httpd:2.4 > /opt/httpd_backup.tarImport an image from a local tar file:
docker image load -i /opt/httpd_backup.tarContainer Lifecycle
Create a container without starting it:
docker create -it --name dev_env ubuntu:22.04List all containers, including stopped ones:
docker ps -aList only running containers:
docker psStart a stopped container using its ID or name:
docker start <container_id_or_name>Execute an interactive shell inside a running container:
docker exec -it <container_id_or_name> bashRun a container in the background (daemon mode):
docker run -it -d --name dev_env ubuntu:22.04Run a container with custom port mapping:
docker run -it -p 9090:8080 --name dev_env ubuntu:22.04 /bin/bashRun a temporary container that is automatically removed upon exiting:
docker run -it --rm ubuntu:22.04 bashA container requires at least one foreground process to remain alive. Without it, the container will exit immediately. Keeping a process running interactively prevents this:
docker run -it ubuntu:22.04 ping localhostStop a running container:
docker stop <container_id>Exit an interactive container session:
exitBatch delete all stopped containers:
docker rm $(docker ps -aq)File Transfer
Copy files or directories from the host to a container:
docker cp /host/path/to/file <container_id>:/target/path/Networking and Inspection
Run a container with random port mapping:
docker run -d --name web_proxy_random -P httpd:2.4Run a container with specific port mapping (host 85 to container 80):
docker run -d --name web_proxy -p 85:80 httpd:2.4Check the port mappings for a running container:
docker port <container_id>Retrieve the host machine's IP address (look for the inet address under the primary interface):
ip aStream container logs in real-time:
docker logs -f <container_id>Display detailed configuration and parameters of a container:
docker container inspect <container_id>Building Custom Images
Launch a base container and install required software:
docker run -it ubuntu:22.04 bash
apt-get update && apt-get install -y vimAfter exiting the container, commit the changes to create a new custom image:
docker commit <container_id> custom/ubuntu-vim:latest