What is the difference between docker exec -ti and docker exec -it? - docker

I am newbie with docker, I was reading few of things and I realized some one using -it and someone -ti.
So I want to know what is the difference between docker exec -ti my_container and docker exec -it my_container.

There's no difference. -it is just a shorthand way of specifying the two flags -i and -t, as explained in the documentation:
Single character command line options can be combined, so rather than typing docker run -i -t --name test busybox sh, you can write docker run -it --name test busybox sh.
The -t flag assigns a pseudo-tty or terminal inside our new container and the -i flag allows us to make an interactive connection by grabbing the standard in (STDIN) of the container.
Since these boolean flags can be specified in any order, -ti is equivalent to -it.
This is a very common feature of Unix-y command-line tools.

Related

What does it mean when Docker is simultaneously run in interactive and detatched modess

I'm new to Docker and came across this confusing (to me) command in one of the Docker online manuals (https://docs.docker.com/storage/bind-mounts/):
$ docker run -d \
-it \
--name devtest \
--mount type=bind,source="$(pwd)"/target,target=/app,readonly \
nginx:latest
What I found confusing was the use of both the -it flag and the -d flag. I thought -d means to run the container in the background, but -it means to allow the user to interact with the container via the current shell. What does it mean that both flags are present? What am I not understanding here?
The -i and -t flags influence how stdin and stdout are connected, even in the presence of the -d flag. Furthermore, you can always attach to a container in the future using the docker attach command.
Consider: If I try to start an interactive shell without passing -i...
$ docker run -d --name demo alpine sh
...the container will exit immediately, because stdin is closed. If I want to run that detached, I need:
$ docker run -itd --name demo alpine sh
This allows me to attach to the container in the future and interact with the shell:
$ docker attach demo
/ #

Docker run command without -it option

Why when i run the command
docker run ubuntu
without option '-it' is not possible to interact with the created container even when running command start with the -a -i options
docker start -a -i CONTAINER_ID
or when i run
docker start CONTAINER_ID
simply the container has the status "Exit (0) 4 seconds ago"
But when i run
docker run -it ubuntu
i can use bash shell of ubuntu using 'docker start -a -i'
When you run docker run without -it it's still running the container but you've not given it a command, so it finishes and exits.
If you try:
docker run ubuntu /bin/bash -c "echo 'hello'";
It'll run ubunu, then the command, and then finish because there is no reason for it to be kept alive afterwards.
-i is saying keep it alive and work within in the terminal (allow it to be interactive), but if you type exit, you're done and the container stops.
-t is showing the terminal of within the docker container (see: What are pseudo terminals (pty/tty)?)
-it allows you to see the terminal in the docker instance and interact with it.
Additionally you can use -d to run it in the background and then get to it afterwards.
Ex:
docker run -it -d --name mydocker ubuntu;
docker exec -it mydocker /bin/bash;
TLDR is -it allows you connect a terminal to interactively connect to the container.
If you run docker run --help, you can find the details about docker run options.
$ docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
...
-i, --interactive Keep STDIN open even if not attached
...
-t, --tty Allocate a pseudo-TTY

Using Docker in interactive mode using an official Microsoft .Net Core SDK image

I'm trying to enter interactive mode using an official Microsoft .Net Core image and use typical .Net commands such as 'dotnet build', but all I get is an '>' cursor. What am I doing wrong?
I'm using the following command:
docker run -it -v $(pwd):/app' -w '/app' -p 8000:80 mcr.microsoft.com/dotnet/core/sdk /bin/bash
I was hoping to get a root command prompt, but all I'm getting is '>'
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a
container
-e, --env list Set environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format:
<name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
After running your container, run command docker ps to take [Container ID]
And after you are able to run the command like there docker exec -it [Container ID] bash .
You are misssing the initial quote here:
-v $(pwd):/app'
That should be:
-v "$(pwd):/app"
It needs to be a double-quote for $(pwd) to be evaluated correctly by the shell. Otherwise the shell will send the literal $(pwd) which is not a valid path.
It seems no one gives a direct answer, this one works for me:
docker run --rm -it -v $PWD:/app -w /app -p 8000:80 mcr.microsoft.com/dotnet/core/sdk /bin/bash

Question about docker run command parameters, -t -i

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.)

Why does docker -itd make a container run while docker -d makes it exit

I would like to know why docker -i -t -d centos /bin/bash makes a container run in background while docker -d centos /bin/bash make a container go to exit state
Hey guys, I am trying to understand why -i -t makes a container stay in active state. I would have thought that -d would have been enough in above scenario.
$ docker run -d --name mycentos3 centos
0bebdb11f3796bf5ac1ee9b0f132c3b3a4fcc2390f77aa971e6700d025025ebb
docker run -i -t -d --name mycentos4 centos
2be599d7310677c53c6f1dd1b5c70106f4c33f3193aad466ca34f0855173e559
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2be599d73106 centos "/bin/bash" Less than a second ago Up 1 second mycentos4
Bash is an interactive prompt. It consumes input from STDIN.
-i is necessary in order for Docker to keep a STDIN open. Without it: bash would reach the end of input and terminate.
-d is not related to your problem.
-t is not responsible for bash's exiting, but it's good to use -t for interactive prompts like bash, so that Docker provides a pseudo-TTY into which bash can write prompts (e.g. your PS1) and control sequences (e.g. colour).

Resources