I new to docker, when i play with docker exec. I have below question:
Command docker exec -t 26b318e534c0 bash, already have tty, this should be interactive? Why must use docker exec **-it** 26b318e534c0 bash?
You can create a pseudo tty without any input to the tty device. This allows an application generating output to do so using content recognized by the tty device (e.g. color output). If you want to be able to interactively type in that tty, then you need to pass the input option, from a terminal with tty support (not from a shell script, and not from some windows command prompts), to attach your console to the stdin of that container.
As a simple example with docker run, these two commands will look different:
docker run -t --rm debian ls -al --color=always
docker run --rm debian ls -al --color=always
The first will have color output, the second will not, and neither will allow you to type input to the ls command being run inside the container.
Related
I create docker container with
sudo docker run -it ubuntu /bin/bash
in book The docker book I read
The container only runs for as long as the command we specified, /bin/bash , is running.
didn't I created terminal with option -it and /bin/bash isn't required? will anything change if I don't pass any command in docker run?
You will get the same behavior if you run
sudo docker run -it ubuntu
because the ubuntu docker image specifies /bin/bash as the default command. You can see that in the ubuntu Dockerfile. As #tadman wrote in their answer, providing a command (like /bin/bash) overrides the default CMD.
In addition, -it does not imply a bash terminal. -t allocates a pseudo-tty, and -i keeps STDIN open even if not attached. See the documentation for further details.
That's an override to the default CMD specification. You can run a container with defaults, that's perfectly normal, but /bin/bash is a trick to pop open a shell so you can walk around and check out the built container to see if it's been assembled and configured correctly.
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
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.
What did the --attach flag do?
Please help.
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.
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/
I am confused about these three commands, I don't know the difference among them. Sorry, I am new to docker.
I can not see the difference from the result.Could anybody tell me the difference?
docker run -it IMAGE_NAME /bin/bash
docker run -i IMAGE_NAME /bin/bash
docker run -i IMAGE_NAME
From the docker documentation
-t : Allocate a pseudo-tty
-i : Keep STDIN open even if not attached
For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process.
docker run -i imagename /bin/bash
This will attach a shell to the container. You can run any shell command on the shell.
docker run -i imagename
This will dump the stdout on the terminal. Similar to docker run but with ability to take input from pipe.
Docker run command has some parameters to run command in Detached or Foreground mode.
-i and -t falls under Foreground mode.
-i : Keep STDIN open even if not attached
-t : Allocate a pseudo-tty
In case of -i whenever you run docker container command passed to it will be fired. in your case "/bin/bash"
Note from Doc
For interactive processes (like a shell), you must use -i -t together
in order to allocate a tty for the container process. -i -t is often
written -it as you’ll see in later examples. Specifying -t is
forbidden when the client is receiving its standard input from a pipe,
as in:
More Detail Here
docker run -it IMAGE_NAME /bin/bash --> you will be able to enter into container if you use -i(interactive) option (which is for executing any commands in the container) and -t(tty) which gives you the terminal to enter any command, /bin/bash is the type of linux shell (eg. sh,ksh,bash etc.)
I'm new to Docker.
I have a simple DockerFile:
FROM ubuntu:12.04
CMD echo "Test"
I built the image using the docker build command (docker build -t dt_test .).
All that I want to do is run the docker image interactively on Git bash. The path in git has been set up to include the docker toolbox.
When I run the interactive docker run command: "docker run -it dt_test"
it gives me an ERROR:
the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
So I've tried prefixing the run command with winpty, and it executes the command but doesn't show me the interactive shell. When I type something, I cant see any of the commands that I'm typing into the terminal. I have to then type "reset" and then it sets the terminal back to normal. So I guess the winpty command isn't working
Questions:
Is there something wrong with the "winpty docker run -it dt_test" command and why doesn't it work?
How can I fix this issue to make my file run interactively?
FYI: When I run the docker file non interactively it seems to work fine. shows "Test" in the terminal according to the Dockerfile.
Looks to be the same as the input device is not a TTY.
Try without the -t:
docker run -i dt_test
And to run it with a different entrypoint (like bash):
docker run -i --entrypoint bash dt_test
I have a utility program that depends on terminal characteristics. I want to execute it inside a docker container. (the program is not a interactive program as such. It is an old program that was written that way).
docker run -i -t or docker exec -i -t should open a tty into container. But here is what happens..
user#1755e1f3f735:~/region/primer/cobol_v> kickstop
[Error] Unable to run without terminal device (tty)
user#1755e1f3f735:~/region/primer/cobol_v> tty
not a tty
When -t option to docker command (run/exec) should give a 'tty', the tty commands returns with 'not a tty'. This is puzzling.
I experienced this on a openSuse and fedora23 hosts and images, if that matters. I used 'guake', MATE (Gnome?) terminal emulators for this, with same results.
Is there any solution to this? or this is by design and have to replace/rewrite my utility?
I ran into the same issue, and found "docker exec -ti container script /dev/null" solved the problem.
After login to the container with the above command, I can use screen normally.
Reference: https://github.com/docker/docker/issues/8755
I ran some experiments and here are findings. Hope someone finds them useful.
(docker commands are not complete but just brief)
1. docker run -i -t
> tty
/dev/console
> echo $TERM
xterm
>kickstop
works!!
2. docker -d followed by docker exec -i -t
>tty
not a tty
>echo $TERM
dumb
>kickstop
[Error] Unable to run without terminal device (tty)
3. docker -d followed by docker attach
you get attached to /dev/console. No prompt (because I'm running tail -f xxx.log to keep the container alive). In fact I need to stop my application from another terminal (using docker exec) and stop the container to get back to the prompt (host shell)
4. docker start followed by docker attach
same as above