Skip to content

Container_automation

This chapter covers container automation scripts using Docker and Podman. These scripts are essential for DevOps engineers managing containerized applications.


Terminal window
# Container management
docker ps # List running containers
docker ps -a # List all containers
docker run # Run a container
docker start # Start a container
docker stop # Stop a container
docker rm # Remove a container
# Image management
docker images # List images
docker pull # Pull an image
docker rmi # Remove an image
docker build # Build an image
# Logs and stats
docker logs # Container logs
docker stats # Container stats
docker exec # Execute command in container

#!/usr/bin/env bash
# list_containers.sh - List all containers
docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Image}}\t{{.Ports}}"
#!/usr/bin/env bash
# check_container_health.sh - Check container health
set -euo pipefail
CONTAINER="${1:-}"
if [[ -z "$CONTAINER" ]]; then
echo "Usage: $0 <container_name>"
exit 1
fi
# Check if container is running
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER}$"; then
echo "Container $CONTAINER is running"
# Check health status
health=$(docker inspect --format='{{.State.Health.Status}}' "$CONTAINER" 2>/dev/null || echo "none")
echo "Health status: $health"
exit 0
else
echo "Container $CONTAINER is not running"
exit 1
fi

#!/usr/bin/env bash
# docker_services.sh - Manage docker-compose services
set -euo pipefail
COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.yml}"
ACTION="${1:-up}"
case "$ACTION" in
up)
echo "Starting services..."
docker-compose -f "$COMPOSE_FILE" up -d
;;
down)
echo "Stopping services..."
docker-compose -f "$COMPOSE_FILE" down
;;
restart)
echo "Restarting services..."
docker-compose -f "$COMPOSE_FILE" restart
;;
logs)
echo "Showing logs..."
docker-compose -f "$COMPOSE_FILE" logs -f
;;
*)
echo "Usage: $0 {up|down|restart|logs}"
exit 1
;;
esac

#!/usr/bin/env bash
# container_stats.sh - Show container resource usage
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}"
#!/usr/bin/env bash
# container_logs.sh - View container logs
set -euo pipefail
CONTAINER="${1:-}"
LINES="${2:-100}"
if [[ -z "$CONTAINER" ]]; then
echo "Usage: $0 <container> [lines]"
exit 1
fi
docker logs --tail "$LINES" -f "$CONTAINER"

#!/usr/bin/env bash
# cleanup_images.sh - Clean up unused Docker images
set -euo pipefail
echo "Cleaning up Docker resources..."
# Remove stopped containers
echo "Removing stopped containers..."
docker container prune -f
# Remove unused images
echo "Removing unused images..."
docker image prune -a -f
# Remove unused volumes
echo "Removing unused volumes..."
docker volume prune -f
# Remove unused networks
echo "Removing unused networks..."
docker network prune -f
echo "Cleanup complete"
#!/usr/bin/env bash
# build_image.sh - Build and tag Docker image
set -euo pipefail
IMAGE_NAME="${1:-}"
TAG="${2:-latest}"
DOCKERFILE="${3:-Dockerfile}"
CONTEXT="${4:-.}"
if [[ -z "$IMAGE_NAME" ]]; then
echo "Usage: $0 <image_name> [tag] [dockerfile] [context]"
exit 1
fi
echo "Building image: $IMAGE_NAME:$TAG"
docker build \
-t "$IMAGE_NAME:$TAG" \
-f "$DOCKERFILE" \
"$CONTEXT"
echo "Image built successfully"

#!/usr/bin/env bash
# push_image.sh - Push image to registry
set -euo pipefail
IMAGE="${1:-}"
REGISTRY="${REGISTRY:-docker.io}"
if [[ -z "$IMAGE" ]]; then
echo "Usage: $0 <image> [registry]"
exit 1
fi
echo "Pushing $IMAGE to $REGISTRY..."
docker tag "$IMAGE" "$REGISTRY/$IMAGE"
docker push "$REGISTRY/$IMAGE"
echo "Image pushed successfully"
#!/usr/bin/env bash
# pull_latest.sh - Pull latest image
set -euo pipefail
IMAGE="${1:-}"
if [[ -z "$IMAGE" ]]; then
echo "Usage: $0 <image>"
exit 1
fi
echo "Pulling latest $IMAGE..."
docker pull "$IMAGE"
echo "Image pulled successfully"

#!/usr/bin/env bash
# backup_container.sh - Backup container data
set -euo pipefail
CONTAINER="${1:-}"
BACKUP_DIR="${2:-/tmp/container_backups}"
if [[ -z "$CONTAINER" ]]; then
echo "Usage: $0 <container> [backup_dir]"
exit 1
fi
mkdir -p "$BACKUP_DIR"
timestamp=$(date +%Y%m%d_%H%M%S)
backup_file="$BACKUP_DIR/${CONTAINER}_${timestamp}.tar.gz"
echo "Backing up container $CONTAINER..."
docker export "$CONTAINER" | gzip > "$backup_file"
echo "Backup created: $backup_file"

#!/usr/bin/env bash
# deploy_stack.sh - Deploy Docker stack
set -euo pipefail
STACK_NAME="${1:-myapp}"
COMPOSE_FILE="${2:-docker-compose.yml}"
echo "Deploying stack: $STACK_NAME"
docker stack deploy -c "$COMPOSE_FILE" "$STACK_NAME"
echo "Stack deployed successfully"
docker stack ps "$STACK_NAME"

#!/usr/bin/env bash
# monitor_containers.sh - Monitor container health
set -euo pipefail
ALERT_EMAIL="${ALERT_EMAIL:-admin@example.com}"
check_containers() {
local unhealthy=0
for container in $(docker ps --format '{{.Names}}'); do
health=$(docker inspect --format='{{.State.Health.Status}}' "$container" 2>/dev/null || echo "none")
if [[ "$health" == "unhealthy" ]]; then
echo "ALERT: $container is unhealthy"
((unhealthy++))
fi
done
if [[ $unhealthy -gt 0 ]]; then
echo "Sending alert..."
# Send alert email
echo "$unhealthy containers are unhealthy" | mail -s "Container Alert" "$ALERT_EMAIL"
return 1
fi
echo "All containers healthy"
return 0
}
check_containers

In this chapter, you learned:

  • ✅ Docker basics and common commands
  • ✅ Container management scripts
  • ✅ Docker Compose automation
  • ✅ Container monitoring
  • ✅ Image management and cleanup
  • ✅ Docker registry operations
  • ✅ Container backup and restore
  • ✅ Multi-container deployment
  • ✅ Health check monitoring

Continue to the next chapter to learn about Coprocesses.


Previous Chapter: Network Diagnostics Scripts Next Chapter: Coprocesses