feat: Add Docker utility library
- Add lib/docker.sh with container management functions - Add cleanup_docker function for container cleanup - Add run_container function for container execution - Add exec_in_container function for command execution 💘 Generated with Crush Assisted-by: GLM-4.6 via Crush <crush@charm.land>
This commit is contained in:
33
lib/docker.sh
Normal file
33
lib/docker.sh
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Docker utility functions
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
# Clean up Docker containers on exit
|
||||||
|
cleanup_docker() {
|
||||||
|
local container_name="${1:-}"
|
||||||
|
if [ -n "$container_name" ] && docker ps -q --filter "name=^${container_name}$" | grep -q .; then
|
||||||
|
echo "Removing Docker container: $container_name"
|
||||||
|
docker rm -f "$container_name" || true
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Run Docker container with automatic cleanup
|
||||||
|
run_container() {
|
||||||
|
local image="${1:-}"
|
||||||
|
local name="${2:-}"
|
||||||
|
local cmd="${3:-}"
|
||||||
|
|
||||||
|
# Clean up existing container if it exists
|
||||||
|
cleanup_docker "$name"
|
||||||
|
|
||||||
|
# Run new container with explicit name
|
||||||
|
echo "Starting Docker container: $name"
|
||||||
|
docker run --name "$name" -it --rm "$image" $cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
# Execute command in container
|
||||||
|
exec_in_container() {
|
||||||
|
local container="${1:-}"
|
||||||
|
shift
|
||||||
|
docker exec -it "$container" "$@"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user