Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between docker exec and docker exec -it?

Tags:

docker

I am new to Docker. I was trying to implement MySQL using Docker container.

When it comes to executing mysql command in Docker container, the tutorial shows this command docker exec -it mysql1 mysql -uroot -p

Docker document mentioned docker exec means run a command in container .
The optional i tag means "Keep STDIN open even if not attached" .
The optional t tag means "allocate a pseudo tty" .

What means "Keep STDIN open", what means "attached"?
What means "allocate a pseudo tty"?

I'm not familiar with shell commands. I don't know why '-it' should be added here.
Will it be different if I just type docker exec mysql1 mysql -uroot -p?

So confused, looking forward to any help... thanks...

like image 973
Arel Lin Avatar asked Oct 24 '18 13:10

Arel Lin


People also ask

What is the difference between docker run and Docker exec?

What's the Difference between Docker Run and Docker Exec? Docker Run vs Docker Exec! This is a fairly common question – but has a simple answer! In short, docker run is the command you use to create a new container from an image, whilst docker exec lets you run commands on an already running container!

What is Docker Exec for?

Docker exec is a command that allows the execution of any given command within a Docker container. This means it will interpret the arguments passed to it as commands to be run inside the container.

Does Docker Exec run as root?

Docker containers are designed to be accessed as root users to execute commands that non-root users can't execute. We can run a command in a running container using the docker exec. We'll use the -i and -t option of the docker exec command to get the interactive shell with TTY terminal access.

What is docker SSH exec?

Method 2: Shell using the docker exec commandThis command brings you to the container's prompt. From there, you can execute almost any Linux command. For example, you can execute the top command. This command shows information about the running system processes and performance.

How do I run a docker image and exec?

Follow these steps: Use docker ps to get the name of the existing container. Use the command docker exec -it <container name> /bin/bash to get a bash shell in the container. Or directly use docker exec -it <container name> <command> to execute whatever command you specify in the container.

How do I get out of Docker exec?

docker exec You can leave the container with exit or ctrl-D .


2 Answers

-i -- Don't just run the program in the background with no way to send it data; keep it open to accepting input of some form.

-t -- Specifically, give me a place to type commands to send to the program, as if I had an ssh or telnet session open to a remote machine I could feed commands to.

Together they essentially make it so that you can run e.g. your mysql1 program as if you were just running it normally locally, outside a Docker container.

like image 149
Will Avatar answered Oct 20 '22 09:10

Will


Will Cain's answer is more complete, but in short, giving -it lets you get inside the container in interactive mode i.e -
-t : Allocate a pseudo-tty
-i : Keep STDIN open even if not attached

Docs for docker run that explains those arguements.

NOTE: docer exec is for running a command inside an already running container. Hence it is extremely useful for debugging conatiners.

like image 27
jar Avatar answered Oct 20 '22 10:10

jar