Difference Between Linux Exec And Docker Exec Commands

This post is all about Linux exec command vs Docker exec command. If you already know the usage of both these commands, it is good. But then you wouldn’t have probably stumbled upon here. Most likely either you have used exec command in Linux machine or used it with docker container. Now when you are trying to use it in opposite case, you are seeing unexpected behaviour. Or it might be possible that you are new & just want to learn the difference. Anyways this post would cover these aspects.

exec command in Linux

In Linux, when you use exec with some command in argument, the command in the argument replaces the current shell process. Let’s take an example. Let’s consider bash is the default shell in our Linux machine. You open a terminal. It starts a bash process.

Without exec command:

You type ls in terminal & hit enter. ls command will just print file names & sub-directory names in the current directory. What happens behind the scene? bash process makes a syscall to spawn a new child process for ls command. The new process prints the file & sub-directory names. Child process is done, it exits. bash shell process takes over. You are still in terminal & can run a new command in the bash shell.

With exec command:

You type exec ls & hit enter. exec is a syscall itself. It just replaces the current bash program with the program mentioned in the argument. In this case it is the ls command we supplied in the argument. exec system call doesn’t create any new process. It just replaces the program running in current process with the new one. So it won’t generate a new PID, process remains same. Just that the process would directly execute ls program as it replaced bash program. ls command will just print the file & sub-directory names. But the terminal will exit immediately after that as there is no running shell process to return to. Terminal will vanish.

This is how exec command works in Linux. The example above might not be a very good practical use case for using exec command. But it should help you understand the things behind the scene. You can simply try it in your Linux machine.

Just to explain, a proper use case would be calling a shell script inside a shell script & you don’t have anything to execute in main shell script when child script is done. It saves overhead of creating an extra process & keeping them (both old & new) at the same time. Child shell script will replace the main shell script in the same running process.

#! /bin/bash

while true
do
   echo "1. Confirm to get report. "
   read Input
   case "$Input" in
      1) exec /home/get_report.sh  ;;
   esac
done

docker exec command

First thing to remember, this docker exec command is not related to exec command that we execute in Linux shell. When you run docker exec from terminal, it spawns a new process inside Docker container & run the program supplied as an argument. Let’s say we execute a command similar to below:

docker exec -it <container_name> /bin/bash

This command above will spawn a new process inside the container & run bash shell there. It is a common use case. This way you can run shell commands inside a docker container. By the way, -it flag is used to create an interactive shell. Otherwise bash will start & stop immediately as it is not interactive.

Hope the difference between these two commands is clear now. And you understand what each one of them does.

Leave a Comment