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...
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!
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.
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.
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.
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.
docker exec You can leave the container with exit or ctrl-D .
-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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With