Docker

What is Docker run -it Flag?

Docker is one of the most powerful tools that can be utilized through the Docker GUI platform and Docker CLI. Docker CLI provides many Docker commands that are used to build, manage, and run applications and the Docker run command is one of them. The “docker run” command runs Docker images as containers and deploys projects.

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:

$ docker run --help

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:

FROM python:3.6

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:

$ docker build -t pythonimage .

Step 3: Use the “docker run -it” Command
Now, use the “docker run -it” command to execute the newly created image:

$ docker run -it pythonimage

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.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.