Skip to content

Instantly share code, notes, and snippets.

@mschuerig
Last active February 8, 2017 00:50
Show Gist options
  • Save mschuerig/6a2ffc07288b1b96fed9 to your computer and use it in GitHub Desktop.
Save mschuerig/6a2ffc07288b1b96fed9 to your computer and use it in GitHub Desktop.
Enter a running docker container
#! /bin/sh -e
# See
# http://blog.docker.com/2014/06/why-you-dont-need-to-run-sshd-in-docker/
# https://github.com/jpetazzo/nsenter
case "$1" in
-h|--help)
echo "Usage: docker-enter CONTAINER"
exit 0
;;
esac
if [ $(id -ru) -ne 0 ]; then
echo "You have to be root."
exit 1
fi
if [ $# -ne 1 ]; then
echo "Usage: docker-enter CONTAINER"
exit 1
fi
PID=$(docker inspect --format {{.State.Pid}} "$1")
nsenter --target $PID --mount --uts --ipc --net --pid
exit 0
_docker_enter() {
local cur prev words cword
_get_comp_words_by_ref -n : cur prev words cword
__docker_containers_running
}
complete -F _docker_enter docker-enter
@firecat53
Copy link

I forked this gist to:

  1. Source the docker bash completion first to ensure __docker_containers_running function is available.
  2. Add /bin and /sbin to $PATH. Archlinux systems don't have either in their default $PATH.

Thanks!
Scott

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment