Managing Docker Images
The following commands allow you to work with Docker images. Replace image_name with the actual image name and container_name for container references.
# Pull an image from a registry
docker pull ubuntu:20.04
# List all locally available images
docker images
# Remove a specific image by name or ID
docker rmi ubuntu:20.04
# Tag an existing image with a new name and version
docker tag e45a5af57b00 myapp:v2.1
# Export an image to a tar archive
docker save -o myapp_backup.tar myapp:v2.1
Saving Container Changes as New Image
After modifying a running container, commit those changes into a new reusable image.
docker commit -a "dev-team" -m "Updated homepage content" c9b133f82aa5 custom-nginx:v1.0
# Run the customized image with port binding
docker run -d -p 8880:80 custom-nginx:v1.0
Container Operations
Use these commands to manage containers during development and deployment.
# Start a new container from an image
docker run nginx:latest
# Show only currently running containers
docker ps
# Display all containers, including stopped ones
docker ps -a
# Stop, start, or restart a container by name or ID
docker stop web-container
docker start web-container
docker restart web-container
# Rename a container for better identification
docker rename old-name new-web-server
# Check exposed ports and their host mappings
docker port web-container
# View real-time logs from a container
docker logs -f web-container
# Tail the last 100 log lines and continue streaming
docker logs -f --tail 100 web-container
# Inspect processes running inside the container
docker top web-container
# Retrieve the container's IP address
docker inspect web-container | grep IPAddress
# Access the shell inside a running container
docker exec -it web-container /bin/bash
Clean Up Containers
To remove a container, first stop it and then delete it:
docker stop web-container
docker rm web-container
Port Mapping and Volume Mounting Example: MySQL Setup
Pull and run a MySQL 5.7 container with persistent storage and configuration maping.
docker pull mysql:5.7
# Run MySQL with volume mounts and environment settings
docker run -p 3306:3306 --name mysql-db \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=securepass \
-d mysql:5.7
Now, any change made in /mydata/mysql/conf on the host will reflect inside the container. For example, create a custom config file:
mkdir -p /mydata/mysql/conf
echo "
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET NAMES utf8'
init_connect='SET collation_connection = utf8_unicode_ci'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
" > /mydata/mysql/conf/my.cnf
Restart the container to apply changes:
docker restart mysql-db
Docker Networking
Isolate services using custom networks.
# List all Docker networks
docker network ls
# Create a bridge network
docker network create -d bridge app-network
# Launch a container attached to a specific network
docker run -d --network=app-network --name api-server alpine sleep 3600
# Connect an existing container to a network
docker network connect app-network db-container
File Transfer Between Host and Container
Copy files between the local filesystem and containers.
# Copy from host to container
docker cp ./local-file.txt mysql-db:/tmp/
# Copy from container to host
docker cp mysql-db:/var/log/mysql/error.log ./error.log
Note: This behavior is similar to Kubernetes'
kubectl cp, enabling seamless file transfer across environments.
Log Management
Inspect container output for debugging purposes.
# Start a stopped container and attach to its I/O
docker start failed-container -ia
# Follow logs in real time
docker logs -f failed-container
# Stream the latest 100 log entries
docker logs -f --tail=100 failed-container
Inspecting Configuration
Retreive detailed information about a container’s setup.
docker inspect mysql-db
This outputs JSON-formatted metadata including mounted volumes, network settings, and runtime configurations.
Deploying Common Middleware with Docker
Redis with Persistence
Set up Redis with AOF persistence enabled via external configuration.
# Prepare directory and empty config file
mkdir -p /mydata/redis/conf
touch /mydata/redis/conf/redis.conf
# Pull and run Redis with mounted config
docker pull redis
docker run -p 6379:6379 --name redis-store \
-v /mydata/redis/data:/data \
-v /mydata/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-d redis redis-server /usr/local/etc/redis/redis.conf
Enable append-only file (AOF) mode:
echo "appendonly yes" >> /mydata/redis/conf/redis.conf
docker restart redis-store
Ensure Redis starts automatically on system boot:
docker update redis-store --restart=always
Nginx with Custom Configuration
Run Nginx with shared HTML, logs, and configuration directories.
# Step 1: Extract default config from a temporary container
docker run -d -p 8080:80 --name temp-nginx nginx:1.10
docker cp temp-nginx:/etc/nginx /mydata/nginx/conf
docker stop temp-nginx && docker rm temp-nginx
# Step 2: Launch production-ready Nginx with volume bindings
docker run -p 80:80 --name web-server \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/conf:/etc/nginx \
-d nginx:1.10
Add auto-start on boot:
docker update web-server --restart=always
Create a test page:
echo "<h1>Hello from Dockerized Nginx</h1>" > /mydata/nginx/html/index.html
Uninstalling Docker Completely
If you need to fully remove Docker from a Linux system (e.g., CentOS/RHEL), follow these steps:
- Stop the Docker service ```
sudo systemctl stop docker
- List installed Docker packages ```
yum list installed | grep docker
- Remove Docker packages ```
yum -y remove docker-ce docker-ce-cli containerd.io
- Delete all Docker data (images, containers, volumes) ```
rm -rf /var/lib/docker
After completing these steps, Docker will be completely removed from the system.