Skip to content

Instantly share code, notes, and snippets.

@jimmidyson
Created July 7, 2014 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmidyson/b0fa988b42c352707d46 to your computer and use it in GitHub Desktop.
Save jimmidyson/b0fa988b42c352707d46 to your computer and use it in GitHub Desktop.
SSH into a Docker container
#!/bin/bash
export DOCKER_HOST=tcp://$1:4243
PID=$(docker inspect --format {{.State.Pid}} $2)
if [ -n "$PID" ]; then
ssh -t docker-ssh@$1 sudo nsenter --target $PID --mount --uts --ipc --net --pid
fi

Install nsenter on the host, either by installing linux-utils package or by following https://github.com/jpetazzo/nsenter#how-do-i-install-nsenter-with-this

Create a new user on the host box:

useradd -g docker docker-ssh
passwd docker-ssh

Set up sudoers for the docker-ssh user by appending the following to /etc/sudoers:

Cmnd_Alias NSENTER = /bin/nsenter --target * --mount --uts --ipc --net --pid

docker-ssh      ALL=(root)      NOPASSWD: NSENTER

Set up docker on the host to listen on tcp socket by adding -H tcp://0.0.0.0:4243 to either /etc/sysconfig/docker. This will probably be different depending on your Linux flavour/version.

Add the file below, docker-ssh to your client's PATH.

Run docker-ssh DOCKER_HOST CONTAINER_ID

e.g.

docker-ssh localhost 2765ea52e7c5

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