In this post, we will look at how to run Docker container in interactive mode. The advantage of Docker interactive mode is that it allows us to execute commands at the time of running the container.

As a result, running a container in interactive mode can be a useful tool in the arsenal of a developer.

If you are new to Docker, you can check out this introductory post about basics of Docker. Also, we have a detailed post about Docker basic commands. Apart from that, you can also visit our Docker Step-By-Step Learning Guide.

1. Docker Container Interactive Mode for Redis Container

To demonstrate the process of running Docker container in interactive mode, we will take the example of Redis.

We can first start a Redis Docker container in background using the below command.

docker run -d redis

This will basically pull the Redis Docker image from Docker Hub and start up a container running the same.

Next, we can get the id of the running container using the below command.

docker ps

Lastly, using the id of the container, we can use the below command to issue a different command to the running container in interactive mode.

docker exec -it 659312e0dd32 redis-cli

Here, 659312e0dd32 is the id of the running container we got from the previous command. Basically, here we are issuing the command redis-cli on the container. This will open a redis-cli command prompt where we can execute commands on the Redis server as below.

run docker container interactive mode

2. Getting a command prompt in a running Linux container

Using the interactive mode, we can also get access to a command prompt in a running container. In other words, we can get a command prompt to be able to issue typical Linux commands.

Below is the command.

docker exec -it 659312e0dd32 sh

Here, 659312e0dd32 is the id of the running Redis container. The sh command opens up a typical Linux shell where we can execute commands.

docker interactive mode shell access

3. The meaning behind -it flag

The -it flag we use for interactive mode has a meaning in itself. Basically, it is a combination of two flags. The flags are -i flag and -t flag.

In other words, the Docker exec command can also be written as below.

docker exec -i -t 659312e0dd32 redis-cli

A Docker container while running has a STDIN communication channel and STDOUT communication channel. There is also a STDERR channel.

Basically, anything you type in your host machine’s terminal is passed to the STDIN channel of the container. The output from the container, if any, is sent back to the terminal using the STDOUT channel in case of proper execution. However, in case of error the STDERR channel is used.

Basically, by using the -it flag, we attach the STDIN channel to our terminal as well as the STDOUT to the terminal. The -i flag is for STDIN and the -t flag is for STDOUT and STDERR. The -t flag is responsible for displaying the output in a pretty format.

We can actually omit the -t flag as below.

docker exec -i 659312e0dd32 redis-cli

However, the response will be not as neatly formatted. There will not be any suggestions as well as the command prompt will be hidden.

docker interactive mode redis cli

Conclusion

With this, we have basically understood how to run Docker container in interactive mode.

Also, we have explored the meaning of the interactive mode and the -it flag in general.

If you have any queries or comments, do sound off in the comments section below.

Categories: BlogDocker

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

1 Comment

Anonymous · May 4, 2021 at 5:26 am

Informational Post

Leave a Reply

Your email address will not be published. Required fields are marked *