Bash-Funk “docker” module
This module contains functions related to docker. It only loads if docker is installed.
The following statements are automatically executed when this module loads:
function -docker-debug() {
local docker_debug_bin=~/.docker-debug/docker-debug.bin
local -a docker_debug_cmd_prefix=(sudo)
if [[ $OSTYPE == "msys"* || $OSTYPE == "cygwin"* || $OSTYPE == "mingw"* || $OSTYPE == "win32"* ]]; then
docker_debug_bin=~/.docker-debug/docker-debug.exe
docker_debug_cmd_prefix=()
fi
if hash docker-debug &>/dev/null; then
"${docker_debug_cmd_prefix[@]}" docker-debug "$@"
return
fi
if [[ ! -e $docker_debug_bin ]]; then
echo "Installing the docker-debug tool (https://github.com/zeromake/docker-debug)..."
mkdir -p ~/.docker-debug
local docker_debug_os docker_debug_arch docker_debug_ext
if [[ $OSTYPE == "darwin"* ]]; then
docker_debug_os=darwin
elif [[ $OSTYPE == "linux"* ]]; then
docker_debug_os=linux
elif [[ $OSTYPE == "msys"* || $OSTYPE == "cygwin"* || $OSTYPE == "mingw"* || $OSTYPE == "win32"* ]]; then
docker_debug_os=windows
docker_debug_ext=.exe
else
echo "$OSTYPE is not supported!"
return 1
fi
docker_debug_arch=$( uname -m )
if [[ $docker_debug_arch == "x86_64" || $docker_debug_arch == "amd64" ]]; then
docker_debug_arch=amd64
elif [[ $docker_debug_arch == "arm64" || $docker_debug_arch == "aarch64" ]]; then
docker_debug_arch=arm64
else
echo "Architecture [$docker_debug_arch] is not supported!"
return 1
fi
if [[ $docker_debug_os == "windows" && $docker_debug_arch != "amd64" ]]; then
echo "Architecture [$docker_debug_arch] is not supported on Windows!"
return 1
fi
curl -fLo "$docker_debug_bin" "https://github.com/zeromake/docker-debug/releases/download/v0.7.11/docker-debug-${docker_debug_os}-${docker_debug_arch}${docker_debug_ext}" || return 1
chmod 700 "$docker_debug_bin" || return 1
fi
"${docker_debug_cmd_prefix[@]}" "$docker_debug_bin" "$@"
}
function -docker-slim() {
# https://github.com/docker-slim/docker-slim
if [ $# -eq 0 ]; then
sudo docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock dslim/docker-slim help
else
sudo docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock dslim/docker-slim "$@"
fi
}
The following commands are available when this module is loaded:
- -docker-debug
- -docker-log
- -docker-netshoot
- -docker-sh
- -docker-top
- -swarm-cluster-id
- -test-all-docker
License
SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com)
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-docker-debug
Usage: -docker-debug [OPTION]...
Installs and executes the docker-debug tool (https://github.com/zeromake/docker-debug).
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
sudo journalctl -u docker.service -f -n20
-docker-log
Usage: -docker-log [OPTION]...
Displays dockerd's log messages in realtime.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
sudo journalctl -u docker.service -f -n20
-docker-netshoot
Usage: -docker-netshoot [OPTION]...
Starts Netshoot (https://github.com/nicolaka/netshoot) - a network trouble-shooting container.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
echo "Select a network to troubleshoot:"
local containers=$(sudo docker container ls --format 'table \t\t\t' | sort)
echo " $containers" | head -1
local selection
eval -- "-choose --assign selection $(echo -n "'Docker Host Network' " && echo "$containers" | tail +2 | while read line; do printf "%s" "'$line' "; done)" || return 1
if [[ $selection == "Docker Host Network" ]] ; then
echo "Attaching to Docker host network..."
sudo docker run -it --rm --net host nicolaka/netshoot
else
local containerId=$(-substr-after-last "$selection" " ")
echo "Attaching to network of container [$containerId]..."
sudo docker run -it --rm --net container:$containerId nicolaka/netshoot
fi
-docker-sh
Usage: -docker-sh [OPTION]...
Displays a list of all running containers and starts an interactive shell (/bin/sh) for the selected one.
Options:
-u, --user USER
Login user.
-----------------------------
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
echo "Select a container to enter:"
local containers=$(sudo docker container ls --format 'table \t\t\t' | sort)
echo " $containers" | head -1
local selection
eval -- "-choose --assign selection $(echo "$containers" | tail +2 | while read line; do printf "%s" "'$line' "; done)" || return 1
echo "Entering [$(-substr-before "$selection" " ")]..."
if [[ $_user ]]; then
sudo docker exec -u $_user -it $(-substr-after-last "$selection" " ") /bin/sh
else
sudo docker exec -it $(-substr-after-last "$selection" " ") /bin/sh
fi
-docker-top
Usage: -docker-top [OPTION]...
Starts Dockly (https://lirantal.github.io/dockly/).
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
sudo docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock lirantal/dockly
-swarm-cluster-id
Usage: -swarm-cluster-id [OPTION]...
Prints the Swarm cluster ID.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
sudo docker info 2>/dev/null | grep --color=never -oP '(?<=ClusterID: ).*'
-test-all-docker
Usage: -test-all-docker [OPTION]...
Performs a selftest of all functions of this module by executing each function with option '--selftest'.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
-docker-debug --selftest && echo || return 1
-docker-log --selftest && echo || return 1
-docker-netshoot --selftest && echo || return 1
-docker-sh --selftest && echo || return 1
-docker-top --selftest && echo || return 1
-swarm-cluster-id --selftest && echo || return 1