Essential Docker Operations and Configuration Guide

Host Environment Setup

Verify the Linux kernel version (requires 3.10 or higher):

uname -r

Refresh the system package cache:

yum clean all && yum makecache

Set 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.repo

Query available Docker CE versions:

yum list docker-ce showduplicates | sort -r

Configure 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 docker

Verify Docker process details:

ps -ef | grep docker

Retrieve comprehensive Docker system information, including storage paths:

docker info

Explore image storage on the host:

ls /var/lib/docker/image/overlay2/imagedb/content/sha256/ -l

Image Management

Search for images in the repository:

docker search httpd

Download an image from the registry:

docker pull httpd:2.4

List all locally downloaded images:

docker image ls
# or
docker images

Display only image IDs:

docker images -q

Format 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.tar

Import an image from a local tar file:

docker image load -i /opt/httpd_backup.tar

Container Lifecycle

Create a container without starting it:

docker create -it --name dev_env ubuntu:22.04

List all containers, including stopped ones:

docker ps -a

List only running containers:

docker ps

Start 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> bash

Run a container in the background (daemon mode):

docker run -it -d --name dev_env ubuntu:22.04

Run a container with custom port mapping:

docker run -it -p 9090:8080 --name dev_env ubuntu:22.04 /bin/bash

Run a temporary container that is automatically removed upon exiting:

docker run -it --rm ubuntu:22.04 bash

A 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 localhost

Stop a running container:

docker stop <container_id>

Exit an interactive container session:

exit

Batch 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.4

Run a container with specific port mapping (host 85 to container 80):

docker run -d --name web_proxy -p 85:80 httpd:2.4

Check 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 a

Stream 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 vim

After exiting the container, commit the changes to create a new custom image:

docker commit <container_id> custom/ubuntu-vim:latest

Tags: docker containerization devops Linux

Posted on Wed, 30 Sep 2026 16:41:20 +0000 by zhopa