This post is all about Linux exec vs Docker exec.
Let’s take two examples. First, we will talk about what happens when you run “exec ls” in Linux terminal. In Linux, exec command is used to replace the current shell process with the command after exec, here it is “ls”. If you run only “ls” command, shell process will start a new process & run “ls” program in it. Now the control is transferred to that process. When “ls” command is executed & list of files/directories have been displayed, control will return back to the shell process. But if you run “exec ls” command, “ls” program will take over the current process where shell program was running. So when “ls” command is executed, then there is no shell program to return to. And terminal will disappear. This is not a good use case for Linux exec command. But it clearly explains how it works.
So what about docker exec command? It has a different purpose. It basically starts a new process in a running Docker container & executes the command mentioned after it. If you execute “docker exec -it <container_name> /bin/bash” command, it will create a new process inside the running container & start a bash program in it. By the way, “-it” flag is used to create an interactive shell. Otherwise you bash will start & stop immediately as it is not interactive.
So I hope there is no more confusion regarding Linux exec & Docker exec commands.