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.
While executing, it will ask you to press “y” to continue this process as shown below.
Within a few seconds, the whole docker and its related packages will be removed successfully.
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.
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.
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.
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.
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.
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.
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.
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.
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 busybox
$ sudo docker pull hello-world
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 busybox
$ sudo docker run hello-world
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.
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.
Using the docker “ps” instruction, we can see the running container with its status “up” and other information.
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.
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.