Skip to content

Latest commit

 

History

History
270 lines (213 loc) · 6.92 KB

docker.md

File metadata and controls

270 lines (213 loc) · 6.92 KB

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() {
   if hash docker-debug &>/dev/null; then
      sudo docker-debug "$@"
      return
   fi

   if [[ ! -e ~/.docker-debug/docker-debug.bin ]]; then
      echo "Installing the docker-debug tool (https://github.com/zeromake/docker-debug)..."
      mkdir -p ~/.docker-debug
      if [[ $OSTYPE == "darwin"* ]]; then
         curl -Lo ~/.docker-debug/docker-debug.bin https://github.com/zeromake/docker-debug/releases/download/0.6.3/docker-debug-darwin-amd64-upx
         chmod 700 ~/.docker-debug/docker-debug.bin
      elif [[ $OSTYPE == "linux"* ]]; then
         curl -Lo ~/.docker-debug/docker-debug.bin https://github.com/zeromake/docker-debug/releases/download/0.6.3/docker-debug-linux-amd64-upx
         chmod 700 ~/.docker-debug/docker-debug.bin
      else
         echo "$OSTYPE is not supported!"
         return 1
      fi
   fi

   sudo ~/.docker-debug/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:

  1. -docker-debug
  2. -docker-log
  3. -docker-netshoot
  4. -docker-sh
  5. -docker-top
  6. -swarm-cluster-id
  7. -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 {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.ID}}' | 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 {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.ID}}' | 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