Skip to content

Instantly share code, notes, and snippets.

@firecat53
Forked from mschuerig/docker-enter
Last active August 29, 2015 14:03
Show Gist options
  • Save firecat53/e4e62553772da7010e95 to your computer and use it in GitHub Desktop.
Save firecat53/e4e62553772da7010e95 to your computer and use it in GitHub Desktop.
Wrapper for nsinit and nsenter for easily accessing running Docker containers
#! /bin/bash
# Enter a running Docker container using either nsinit(preferred) or nsenter
# You can specify one or the other if desired.
# See
# http://blog.docker.com/2014/06/why-you-dont-need-to-run-sshd-in-docker/
# https://github.com/jpetazzo/nsenter
# https://gist.github.com/ubergarm/ed42ebbea293350c30a6 for compiling nsinit
[[ -n "$1" ]] || { echo "Usage: `basename $0` <container ID or name> [nsinit|nsenter]. Defaults to nsinit if available."; exit 0; }
[[ -n "$2" ]] && cmd=$2 || cmd=nsinit
command -v $cmd >/dev/null 2>&1 || cmd=nsenter
command -v $cmd >/dev/null 2>&1 || { echo "$cmd must be installed"; exit 1; }
if [ $(id -ru) -ne 0 ]; then
echo "You have to be root."
exit 1
fi
if [[ $cmd == "nsinit" ]]; then
# libcontainer driver method:
echo "Using nsinit..."
FULL_ID=$(docker inspect --format='{{.Id}}' $1)
bash -c "cd /var/lib/docker/execdriver/native/$FULL_ID && nsinit exec bash"
exit 0
elif [[ $cmd == "nsenter" ]]; then
echo "Using nsenter..."
export PATH=$PATH:/bin:/sbin
PID=$(docker inspect --format {{.State.Pid}} "$1")
nsenter --target $PID --mount --uts --ipc --net --pid
exit 0
fi
_docker_enter() {
local cur prev words cword
_get_comp_words_by_ref -n : cur prev words cword
__docker_containers_running
}
. /usr/share/bash-completion/completions/docker
complete -F _docker_enter docker-enter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment