This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/Container.sh

77 lines
1.4 KiB
Bash
Raw Normal View History

2024-01-25 15:00:44 +03:00
# Attach/create container box in current directory with specified name.
# Uses `main` name by default.
# Usage: ca [NAME]
function ca() {
local name="${1}"
# Set default name.
[[ "${name}" = "" ]] && name="main"
# Append box prefix.
name="box-${name}"
# Create container.
docker run \
--privileged \
-d -it \
-h "${name}" --name "${name}" \
--workdir /data \
-v ${PWD}:/data \
2024-01-25 15:08:17 +03:00
-v ${HOME}:/root \
2024-01-25 15:00:44 +03:00
debian:latest bash -c bash &> /dev/null
# Attempt to start container.
docker start "${name}" &> /dev/null
# Attach to running container.
docker attach "${name}"
}
# Remove container box with specified name.
# By default uses `main` name.
# Usage: ck [NAME]
function ck() {
local name="${1}"
# Set default name.
[[ "${name}" = "" ]] && name="main"
# Append box prefix.
name="box-${name}"
# Kill container.
docker kill "${name}" &> /dev/null
docker rm "${name}" &> /dev/null
}
# Remove all container boxes.
function cka() {
local IFS=$'\n'
local boxes=$(_get_boxes)
for box in ${boxes[@]}; do
ck "${box}"
done
}
# List all container boxes.
function cl() {
_get_boxes
}
# Print all boxes.
function _get_boxes() {
local IFS=$'\n'
local boxes=$(docker ps -a | grep "box-" | sed -e "s/.*box-//")
[[ "${boxes[@]}" != "" ]] && echo "${boxes[@]}" || true
}
# Autocomplete with boxes.
function _comp_get_boxes() {
local IFS=$'\n'
_autocomplete_first $(_get_boxes)
}
complete -F _comp_get_boxes ca ck