Docker is a container management framework that is free and open source. It allows developers to bundle programs into containers which are standardized executable parts that combine source code with the OS modules and requirements needed to run that program on almost any platform.

Docker framework came up with a console platform that allows us to execute several commands on it to get data from containers. There are some execution commands of dockers as well run and exec. The exec command lets us execute the commands in a running container without opening a new console.

So, this article will be demonstrating the use of exec –it command of docker in Ubuntu 20.04 system.

To the opening of the shell application for the Ubuntu 20.04 system, we need to remove all the old versions of docker along with other packages installed from our system.

So, the apt-get instruction with 4“remove” keywords has been used for this purpose. It asks for our current account password to continue. We have added a password and pressed Enter to continue this process.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-01.png" data-lazy- height="80" src="data:image/svg xml,” width=”624″>

While executing, it will ask you to press “y” to continue this process as shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-02.png" data-lazy- height="38" src="data:image/svg xml,” width=”492″>

Within a few seconds, the whole docker and its related packages will be removed successfully.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-03.png" data-lazy- height="54" src="data:image/svg xml,” width=”424″>

Now, you need to update your Linux system via the apt-get command of the update shown below. It is a necessary step to be followed to avoid any inconvenience.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-04.png" data-lazy- height="52" src="data:image/svg xml,” width=”558″>

Make sure to download the docker from its website if you don’t know any other way to install it. The curl instruction shown below must be utilized in this regard.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-05.png" data-lazy- height="48" src="data:image/svg xml,” width=”624″>

Now, to install the docker platform, try using the “apt-get” installation command followed by the name of a platform as “docker.io” shown below. It will start calculating all information.

$ sudo apt-get install docker.io

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-06.png" data-lazy- height="68" src="data:image/svg xml,” width=”472″>

While processing, it requires your affirmation to continue installing docker at your Ubuntu 20.04. You need to press “Y” as an answer to the question.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-07.png" data-lazy- height="36" src="data:image/svg xml,” width=”596″>

If the docker cannot be installed at your end using the above command due to some issues, try using the snap package to install it as per the shown below instruction.

$ sudo snap install docker

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-08.png" data-lazy- height="50" src="data:image/svg xml,” width=”456″>

You can find out the installed version of docker on your system via the below version command with the keyword “docker”. Now, the docker is installed and ready for use.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-09.png" data-lazy- height="32" src="data:image/svg xml,” width=”456″>

The docker “ps” command can be used to get the information regarding all the containers it has in your system. We have been using the sudo privileges to execute the “docker ps” instruction to get this information. We have found out that there is no container in our docker right now.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-10.png" data-lazy- height="70" src="data:image/svg xml,” width=”624″>

You need to find out all the available images of docker in your system using the “docker images” instruction demonstrated below. You can see that we have currently 4 images available to be used in our docker system along with their information.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-11.png" data-lazy- height="108" src="data:image/svg xml,” width=”536″>

You need to pull these images in your system utilizing the docker pull instruction along with the image name. So, we have pulled 3 images one after another i.e. Ubuntu, busy box, hello-world.

$ sudo docker pull ubuntu


$ sudo docker pull busybox


$ sudo docker pull hello-world

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-12.png" data-lazy- height="300" src="data:image/svg xml,” width=”624″>

Now, all these images can be utilized at our end. So, we have to run the images to create containers. For this, try making use of the “docker run” instruction along with the name of an image. We have done it for all 3 images as below.

$ sudo docker run ubuntu


$ sudo docker run busybox


$ sudo docker run hello-world

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-13.png" data-lazy- height="52" src="data:image/svg xml,” width=”500″>

After checking the container information via the docker “ps” instruction, you can see that 3 container images have been created. Now, we can use them by their ID or Names.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-14.png" data-lazy- height="144" src="data:image/svg xml,” width=”624″>

Now, we will be looking at the “run” instruction first so that when we use the “exec” command, it will be more clear to you. We have been using the “docker run” instruction with the “—name” option to rename the image “busy box” to “Test1”. Along with that, -it option is used to open the interactive console on our shell.

Now, a container named “Test1” has been running for the image busybox and its console is opened. We have used the list “ls” instruction to display all the contents in a running container “Test1”. When the user exits the container, the container will automatically be removed from the docker queue and the “ps” command will now show it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-15.png" data-lazy- height="72" src="data:image/svg xml,” width=”588″>

Using the docker “ps” instruction, we can see the running container with its status “up” and other information.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-16.png" data-lazy- height="82" src="data:image/svg xml,” width=”624″>

Now, we will be taking a look at the “exec –it” instruction in comparison to the “run” instruction. So, start the “Test1” container with a docker start instruction. Then, use the “docker exec –it” instruction along with the container name “Test1”. You have to put any instruction you want to execute after the container name. You will get similar results as you got in the “run” instruction. This means, the “exec –it” instruction allows you to run the instruction within the running container.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/docker-exec-it-17.png" data-lazy- height="76" src="data:image/svg xml,” width=”608″>

Conclusion

The introduction paragraph of this tutorial guides you about the working of docker and how it manages data within the containers. We have discussed the installation of docker in the Ubuntu 20.04 system along with pulling and running its images. After that, we have demonstrated the comparison between “run” and “exec –it” instructions to show how the exec command can run commands in the already running container without the console as the “run” command does. All the commands we have used are quite simple to understand and implement.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content