How to use -it option with docker commands

Last Updated on April 16, 2024
In this post we will understand how -i (interactive) and -t (terminal) options work with docker commands such as docker run and docker exec. To understand these options, we will create a simple container with docker run and understanding how these flags are used as we move ahead.
Create a container with docker run
Let us start by creating a container from the busybox image on the Dockerhub. We chose busybox image as it is a light weight environment with a minimal linux OS, and it is perfect for running the basic shell commands used in this post. Next we will run shell commands on the container and check directory structure, system information etc. To start with, let us at first run this image without giving any option
sudo docker run busybox
The above command did not do anything. It just returned the control. Why?
Running docker container in Interactive mode
The previous command did not do anything because the container is not in an interactive mode. This means that the standard input (STDIN) of the host is not mapped to the standard input of the container. Hence, the container has no way of accepting the input.
Next we will run the same command but in an interactive mode as follows:
sudo docker run -i busybox
With interactive mode or the -i option, you bind the standard input of your host, for example, the input from your keyboard, to the standard input of the container. In other words, to have the container accept user input, you need to keep the standard input open by using -i or –interactive option.
Next let us try to run a few shell commands like env, ls, pwd or echo.
What do you notice?
First, there is no command prompt for providing the input. Next, although the input is accepted and you are also provided with the output but the output will be unformatted unlike the output from the terminal. This is shown in the Screenshot in the next section.
Adding pseudo terminal to docker run
What happens now when you run the same command with an additional option -t (or –tty). Rather than mentioning the two options separately, we have placed them together as -it
sudo docker run –it busybox
The first thing that you will notice is that a command prompt will be provided.
This is because -t or –tty option allocates a pseudo terminal.
Now when you run the commands like env, ls, pwd, or echo, you will notice that the output is presented in the same way as it is normally presented on a terminal.
Now let us see the options that we discussed above in action in the below screenshot.

In case you are required to work on container which is already running in a detached mode, then you can use docker exec command with -it options in the same way as seen above.
Why not to use -t flag by itself
Do you wonder what will happen if you run the command with just -t option instead of -it. This will open a terminal in the container but because of missing -i (–interactive) option, the standard input on the host is not mapped to the standard input of the container. Hence the container will not recognize or process any of the commands that you will provide.
Related post:
How to Build A Docker Image and Publish To Docker Hub with Github Actions
To read more posts on Docker >> Click here

Good to know. Do let me know what are some of the ideas on my web site that you like
Hi i think that i saw you visited my web site thus i came to Return the favore I am attempting to find things to improve my web siteI suppose its ok to use some of your ideas