Ubuntu Docker - Enter Running Container With Nsenter

Docker 0.11 has a new feature that allows direct host networking. Unfortunately this feature doesn't work when you set docker to use the LXC execution driver. Thus, I have found an alternative way to attach to a running container on Ubuntu, other than using lxc-attach.

This tutorial teaches you to use nsenter and not nsinit. I did initially try to use nsinit, but configuring it was not as simple (for me).

Steps

Compile the latest nsenter

#!/bin/bash
cd /tmp
curl http://bit.ly/1iLVbQU | tar -zxf-
cd util-linux-2.24

# Install the necessary tools to for make
sudo apt-get install build-essential -y

# Now compile!
./configure --without-ncurses
make nsenter

# Move nsenter into your execution PATH
sudo mv nsenter /usr/local/bin

Great, now you have nsenter. Here is a script I use to enter the first (and only) docker container I am running. I save it to "enter-first-container.sh" because it's a bit much to keep copy and pasting.

#!/bin/bash
CONTAINER_ID=`docker ps --no-trunc | sed -n 2p | tr -s ' ' | cut -d' ' -f1`
PID=`docker inspect --format '{{ .State.Pid }}' $CONTAINER_ID`
nsenter -m -u -n -i -p -t $PID /bin/bash

References

1 comment: