Debugging Docker Containers

Dhruva Sagar
Aug 20, 2020 · 3 min read

As software craftsmen, one of the most common things we do on a daily basis is debug our code. When using docker, be it locally or on cloud, a lesser known, yet incredibly powerful tool available to us is docker attach.

Quoting from the docker docs.

Use docker attach to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.

Image for post
Image for post

Debuggers

Most programming languages have debuggers that allow you to halt execution at specific points by defining break points in your program at which point you can inspect the current state of the program. From there you could either go step by step or continue execution until instructed to halt again at some other code path or the program exits. This is a very commonly used approach for debugging your application and allows the developer to be able to inspect the exact state of program at key points to determine if it’s behaving as intended or not.

Most debuggers also support a client-server architecture that allows IDEs (Integrated Development Environment) to be able to connect to the debugger server and allow us to use it’s features from within the IDE like defining break points, etc. For keeping this article short, I am not going to get into how to do that with docker. You can read up documentation for your IDE on how to do the same.

Demos

For ruby, as an example you can use either irb or pry to define break points in your code, by adding statement binding.irb or binding.pry respectively and when your code execution reaches at that point, it will spawn a irb or pry repl for you to be able to inspect the state of your application.

Here’s a demo showing what that looks like :

For golang we have delve that allows us to do a similar thing, here’s a demo on what that looks like :

For nodejs, we can make use of node inspect to do a similar thing, here’s a demo on what that looks like :

A similar strategy can be applied to other programming languages with their debuggers and since we are using containers it makes it very easy to isolate all the dependencies and makes this fairly generic.

Detaching

One annoyance with docker attach is that if you use ctrl-c to detach from the container, it sends a SIGKILL to the container by default and this may not be what you want. To get around this, you can use :

docker attach --detach-keys 'ctrl-c' <container id>

The above tells docker attach to treat ctrl-c as the key sequence to detach from the container, instead of sending a SIGKILL to the container and hence keep it running in the background once you detach from it.

With Docker Compose

If you’re using docker-compose to manage your development infrastructure, you need to add a couple of options to your application service in order to be able to attach to it, namely tty and stdin_open .

Following is an example docker-compose configuration with these options enabled for the app service.

Reference

All the above code is available at https://github.com/dhruvasagar/docker-attach

Tarka Labs Blog

Tarka Labs is a team of passionate hackers, designers and…

Dhruva Sagar

Written by

hacker. loves to code, lives inside terminals, vim advocate, open source enthusiast.

Tarka Labs Blog

We’re a collective of thinkers, dreamers and doers out to reimagine how software is built. We experiment, instead of working on technology du jour. Check out: https://tarkalabs.com/about

Dhruva Sagar

Written by

hacker. loves to code, lives inside terminals, vim advocate, open source enthusiast.

Tarka Labs Blog

We’re a collective of thinkers, dreamers and doers out to reimagine how software is built. We experiment, instead of working on technology du jour. Check out: https://tarkalabs.com/about

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store