Practical Applications of Container Filtering
In this step, we will explore practical applications of container filtering to perform common Docker management tasks. These examples demonstrate how filtering can be used in real-world scenarios to make container management more efficient.
Finding and Restarting Specific Containers
Let's say you need to restart all web-related containers. You can use name filtering to identify them and then restart them:
## First, identify the web containers
docker ps --filter name=web
## Now, restart them one by one
docker restart web-frontend
docker restart web-backend
After running these commands, you should see confirmation that each container was restarted:
web-frontend
web-backend
Finding and Removing All Containers with a Specific Name Pattern
If you need to remove all containers with a certain name pattern, you can combine filtering with container removal:
## WARNING: This is for demonstration only. In a real environment,
## make sure you know what you're removing!
## List containers with "utility" in their names
docker ps -a --filter name=utility
You should see your utility container:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
26f8c661f7a5 alpine "sleep 1000" 45 minutes ago Up 45 minutes utility-alpine
To remove it, you would typically use:
## We won't actually run this command to preserve our lab environment
## docker rm -f $(docker ps -aq --filter name=utility)
Instead, let's see how this command works:
## Just get the IDs that would be affected
docker ps -aq --filter name=utility
This will output the container ID:
26f8c661f7a5
Using Filtering to Monitor Specific Container Groups
Container filtering is useful for targeted monitoring. Let's create a simple monitoring script that checks the status of our web containers:
nano web-monitor.sh
Add the following script:
#!/bin/bash
## This script checks the status of web containers and reports if any are not running
echo "=== Web Container Status Check ==="
docker ps -a --filter name=web --format "{{.Names}}: {{.Status}}"
## Count stopped web containers
STOPPED=$(docker ps -a --filter name=web --filter status=exited --format "{{.Names}}" | wc -l)
if [ $STOPPED -gt 0 ]; then
echo -e "\nWARNING: $STOPPED web containers are not running!"
else
echo -e "\nAll web containers are running normally."
fi
Save the file (Ctrl+O, Enter, then Ctrl+X) and make it executable:
chmod +x web-monitor.sh
Now, run the script to see the status of your web containers:
./web-monitor.sh
You should see output similar to:
=== Web Container Status Check ===
web-backend: Up 50 minutes
web-frontend: Up 50 minutes
All web containers are running normally.
Filtering in CI/CD Pipelines
In CI/CD pipelines, filtering containers can be essential for automated testing and deployment. Let's create a script that simulates a deployment process that targets only containers with a specific name pattern:
nano deploy-update.sh
Add the following script:
#!/bin/bash
## This script simulates updating all containers of a specific type
TARGET_CONTAINERS=$1
if [ -z "$TARGET_CONTAINERS" ]; then
echo "Usage: $0 <container-name-pattern>"
exit 1
fi
echo "Preparing to update containers matching pattern: $TARGET_CONTAINERS"
echo "Containers affected:"
docker ps --filter name=$TARGET_CONTAINERS --format "{{.Names}}"
echo -e "\nIn a real deployment, this would:"
echo "1. Pull the latest images"
echo "2. Stop each container"
echo "3. Start new containers with the updated images"
echo "4. Verify the containers are running correctly"
echo -e "\nSimulation completed successfully!"
Save the file (Ctrl+O, Enter, then Ctrl+X) and make it executable:
chmod +x deploy-update.sh
Now, let's simulate updating our web containers:
./deploy-update.sh web
You should see output similar to:
Preparing to update containers matching pattern: web
Containers affected:
web-backend
web-frontend
In a real deployment, this would:
1. Pull the latest images
2. Stop each container
3. Start new containers with the updated images
4. Verify the containers are running correctly
Simulation completed successfully!
These practical examples demonstrate how container filtering can be integrated into your Docker workflow to make container management more efficient and automated.