This post will elaborate on what Docker’s run -it option is and how to use it.
What is Docker run -it Flag?
The “docker run” command supports many options to perform additional functionalities along with executing the Docker images, and the “-it” flag is one of them. It combines the two options, “-i” and “-t”:
- The “-i” option is utilized to run the Docker image in interactive mode (keep command input stream open)
- The “-t” option is used to allocate the “TTY-pseudo” terminal to the Docker container.
To view all options of the Docker run command along with a description, utilize the “docker run –help” command:
How to Use Docker run -it Command?
To utilize the “docker run -it” command, users must have a docker image. For this purpose, we will utilize the Visual Studio code editor and create a new Dockerfile through which a Docker image will be generated.
In our case, we will start the procedure by creating a Dockerfile.
Step 1: Create New Dockerfile
Create a new Dockerfile by clicking on the highlighted icon and set the file name as “Dockerfile”:
Paste the below-mentioned code in Dockerfile. These instructions will first install mentioned dependencies and then execute the simple python program:
RUN apt-get update && apt-get install -y --no-install-recommends \
python3-setuptools \
python3-pip \
python3-dev \
python3-venv \
git \
&& \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
EXPOSE 8000
CMD python -c "print('Docker is more simple Deployment Tool')"
Step 2: Build a Docker Image
Next, generate the new Docker image using the “docker build” command. Here, the “-t” option is used to specify the image name:
Step 3: Use the “docker run -it” Command
Now, use the “docker run -it” command to execute the newly created image:
It can be observed that, with the help of the “docker run -it” command, we have successfully deployed the simple Python program:
We have elaborated on what is Docker run -it flag and how to use it in Docker.
Conclusion
The Docker run “-it” flag is a combination of two options, “-i” and “-t”. The “-i” option runs the Docker image in interactive mode (keep Standard Input Stream) open. However, the “-t” option is utilized to allocate the “pseudo-TTY” terminal to the container. In order to use “docker run -it”, first create a Docker image through Dockerfile. Then, utilize the “docker run -it” command. This post discussed the “docker run -it” and how to use it.