dc
#!/usr/bin/env bash
# FUNCTION: __docker_compose_check
__docker_compose_check() {
# Check if Docker Compose file is in the current directory
if [[ ! -f "compose.yml" ]]; then
echo "No compose.yml file found"
return 1
fi
return 0
}
# FUNCTION: __docker_check
__docker_check() {
docker_error=0
# Check if Docker is installed
if ! command -v docker &>/dev/null; then
echo "Docker is not installed"
docker_error=1
fi
# Check if the Docker daemon is running
if ! sudo systemctl is-active --quiet docker; then
echo "Docker is not running"
docker_error=1
fi
}
# FUNCTION: __docker_install
__docker_install() {
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" |
sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
}
# FUNCTION: __docker_attach
__docker_select_container() {
# List all running docker containers
containers=$(docker ps --format "{{.Names}}")
container_list=($containers)
# Display the list of containers
echo "Select a container to execute commands in:"
for i in "${!container_list[@]}"; do
echo "[$i] ${container_list[$i]}"
done
# Check if there are any running containers
if [[ ${#container_list[@]} -eq 0 ]]; then
echo "No running containers or insufficient permissions"
return 1
fi
# Read the user's choice
read -p "Enter the number of the container: " container_index
# Validate the user's choice
if [[ ! $container_index =~ ^[0-9]+$ ]] || [[ $container_index -ge ${#container_list[@]} ]]; then
echo "Invalid selection"
return 1
fi
# Set the selected container
container=${container_list[$container_index]}
echo "$container"
# Start an interactive shell in the selected container
# docker exec -it $container /usr/bin/bash
}
# FUNCTION: __docker_run
__docker_run() {
if ! __docker_compose_check; then
return 1
fi
docker compose up -d
}
# FUNCTION: __docker_stop
__docker_update() {
if ! __docker_compose_check; then
return 1
fi
docker compose down
docker compose pull
__docker_run
}
# FUNCTION: __docker_list
__docker_list() {
# List all running docker containers by name
docker ps --format "{{.Names}}"
}
# FUNCTION: __docker_attach
__docker_attach() {
local container="$1"
local exec_command="$2"
local user="$3"
if [[ -z "$container" ]]; then
echo "No container specified"
return 1
fi
if [[ -z "$exec_command" ]]; then
exec_command="/bin/bash"
fi
if [[ -z "$user" ]]; then
user="root"
fi
user_option="--user=$user"
docker exec -it $user_option $container $exec_command
}
# FUNCTION: __docker_help
__docker_help() {
echo
echo "Usage: dc [command]"
echo
echo "Commands:"
echo " install - Install Docker"
echo " check - Check if Docker is installed and running"
echo " list - List all running containers"
echo " run - Start the container belonging to the compose.yml file in $(pwd)"
echo " update - Update the container belonging to the compose.yml file in $(pwd)"
echo " attach [container] [command] [user] - Attach to a running container"
echo
}
# MAIN
if [[ -z "$1" ]]; then
__docker_help
exit 1
fi
__docker_check
if [[ "$docker_error" != "0" ]]; then
echo "You need to install and start Docker first"
exit 1
fi
case "$1" in
run)
__docker_run
;;
check)
__docker_check
;;
install)
__docker_install
;;
list)
__docker_list
;;
attach)
__docker_attach "$2" "$3" "$4"
;;
update)
__docker_update
;;
*)
__docker_help
exit 1
;;
esac