[Tutorial] Attaching to a running docker container

When you get your container up and running, sometimes you’ll need to perform some actions inside your container. This post will explain how to enter a running container without ssh.

Insure you have LXC installed

You can run the following command to make sure you have lxc installed.

sudo apt-get install lxc -y

Edit /etc/default/docker file

New we will need to instruct docker to use lxc when it starts, we can do so by editing /etc/default/docker file

Add the following line to the file

DOCKER_OPTS="-e lxc"

Reboot the server

From my experiment, restarting docker was not enough to get the new option picked by docker, it may work for you, but you should reboot the server to avoid any headaches.

Grab the full container ID

In order to attach to the running container, we need the full container ID. Use this command:

docker ps --no-trunc

Run lxc-attach with full container ID

Now that we have the full container ID, attaching to it is simple as:

lxc-attach -n MYCONTAINERID

Yey, I’m inside

That’s it, you should be inside your container, and do whatever you want inside.

I hoped docker would avoid us interacting directly with lxc, but for now there no better alternative.

But wait, I think having a way to attach ssh deployment keys to every container would grant us permission to ssh in, anyhow this will a good subject to discuss in another post.

Thank you for reading.

Leave a Reply