Docker attach command: limitations

Docker attach command: limitations

Knowing about the shortcomings of docker attach command

Prerequisites

  • Should know basic docker commands
  • Should know how to write Dockerfiles & Docker-compose files
  • Should have docker installed in your machine

What is docker attach command and why we use it?

Use docker attach to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container's primary process 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.png

Lets take an example

Taking an example of a simple docker compose file, here we are running two separate containers for react-app and tests

image.png

when we run "docker compose up" command then it creates two containers, one for react app and another one for tests

image.png

And if we open a new terminal and run "run docker exec -it sh", now we are inside the test container.

image.png

Now, we run "npm run test" command so we get an output something like this:

image.png

We can run the tests multiple time and choose any of the options mentioned in the list.

Lets try this with docker attach

After running "docker attach container-id" we are not getting that interactive CLI which we got earlier:

image.png

This is where we can see limitations of "docker attach". Lets talk more about the limitations.

Limitation with docker attach command

When you have an interactive test CLI that lets you run the tests multiple times like I have here:

image.png

You can see it is asking inputs whether to run the tests again or to run only failed tests and few more options, but why "docker attach" is not able to open this interactive CLI is because this interactive CLI is not running as the primary process of that container and with docker attach command we can only attach our terminal STDIN with the STDIN of the primary process with id 1 of the container.

Example: when we run a simple "ps", npm start is the primary process that runs with the process Id as 1 and then it runs to whatever is next like start.js and so on.

image.png

So, In real there are multiple commands and processes that are executed further are responsible for giving that interactive CLI, but we are only able to attach our terminal to the primary process, so that is why we were not getting the interactive CLI with docker attach command.

So, the actual image of test container running processes, looks something like this

image.png

Workarounds for running secondary processes

Approach-1: You can run the web container and open a new terminal get inside the container using docker exec command and then run npm run test to be able to see use the interactive CLI.

Approach-2: (recommended)You can have two separate containers for both web-app and test, but you'll not be able to run the interactive CLI, but also there are other test suites available that do not offer this command line interactions, you can go for those test suites as well.

That's it for the limitations part of docker attach, hope it helped you somehow get a better view of things running in background.

Thanks for reading the Blog!! Any suggestions and questions are welcomed in the comment section.