12

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

2
  • (If you just need to run MySQL commands, run the mysql -h 127.0.0.1 client tool on your host. You don’t need docker exec and its attendant root-level privilege to do it.)
    – David Maze
    Oct 24, 2018 at 14:00
  • In that case, I have to have mysql client pre-installed in host environment, right?
    – Arel Lin
    Oct 24, 2018 at 14:04

2 Answers 2

18

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

2
  • 3
    So, -t gives me an interface to write commands, and -i provide a tunnel for program inside container to receive the written commands. Am I getting it right?
    – Arel Lin
    Oct 24, 2018 at 14:03
  • Thank you! Your explanation makes everything clear to me.
    – Arel Lin
    Oct 24, 2018 at 14:17
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.