3

Edit:

Someone mark duplicate of this question, but it does not explain the underlying mechanism at all.

But on contrast, this stack overflow solve my confusion in Case I, but not Case II.


I am newbie in docker and i am confused about the usage of --interactive, --attach flag and those concepts involved

I will show my confusion using busybox in docker hub.


Case I :

When I run a container using the below commands. docker run --interactive --tty busybox sh

A container is running and accepting input enter image description here

According to the document, --interactive flag used to

Keep STDIN open even if not attached

I don't understand what is the meaning of even if not attached to, attached to what?


Case II : Then I exit the container and try to start it using docker start --attach abdd796820b1 .

The terminal seems accepting input as well, but when I type ls or echo, it does not give response. enter image description here What did the --attach flag do?

Please help.

HKIT
  • 628
  • 1
  • 8
  • 19
  • Does this answer your question? [what is docker run -it flag?](https://stackoverflow.com/questions/48368411/what-is-docker-run-it-flag) – jannis Jan 29 '20 at 12:47
  • @jannis, it does not, but it leads to another page [here](https://stackoverflow.com/questions/30137135/confused-about-docker-t-option-to-allocate-a-pseudo-tty) which gives answer to most of my question. But it does not answer my last question, what id the --attach flag do. – HKIT Jan 29 '20 at 15:06
  • "Then I exit the container" - how do you do that? – jannis Jan 29 '20 at 15:12
  • @jannis type exit – HKIT Jan 29 '20 at 15:33

2 Answers2

9

There are two ways in which you can interact with a running container

  • attach
  • exec

--interactive flag

As you mentioned it already says

Keep STDIN open even if not attached

Which from my understanding means it will read inputs from your terminal/console and reacts or present output to it. If you run docker run --tty alpine /bin/sh and docker run --tty --interactive alpine /bin/sh. One with --interactive will react to it.

attach

Attach to a running process

If the docker container was started using /bin/bash command, you can access it using attach, if not then you need to execute the command to create a bash instance inside the container using exec.

More in depth: If docker container is started using /bin/bash then it becomes containers PID 1 and attach command will attach you to PID 1.

exec

Creates new process

If you want to create a new process inside container than exec it used like exec is used to execute apt-get command inside container without attaching to it or run a node or python script.

Eg: docker exec -it django-prod python migrate

See here -i is for interactive and -t is for --tty that is pseudo-TTY. Interactive so that you can enter if something is prompted by this command.

1

You will need to provide the -i/--interactive option to forward your terminal STDIN to the container's sh.

Try this:

docker start -ai CONTAINER

https://docs.docker.com/engine/reference/commandline/start/

Ajanyan Pradeep
  • 1,097
  • 1
  • 16
  • 26
vinays
  • 11
  • 1