Using Docker Exec for Interactive Shell Access
Learn how to effortlessly access and navigate a Docker container's environment using Docker Exec. Dive into this essential tool's usage, from executing commands to troubleshooting
Managing Docker containers with Docker Exec
Imagine you have a web application running inside a Docker container, and users are reporting issues with the application. You need to investigate and troubleshoot the problem, but you don't want to stop or restart the container as it might disrupt the service for other users.
In this case, you can use docker exec to access the shell inside the running container and perform various debugging tasks.
What is Docker Exec
The docker exec
command allows you to run a new command or start an interactive shell session inside a running Docker container. It provides a way to inspect, modify, or troubleshoot the container's environment without having to stop or restart the container.
Using Docker Exec
To use the docker exec
command, you need to know the container ID or container name first. You can find the container ID or name using the docker ps
command, which lists all running containers.
I will be using Ubuntu OS in this article:
Find the Container ID or Name
First, you need to find the container ID or name of the container running your web application. You can use the docker
command to list all running containers.
ps
This will output a list of running containers with their container IDs and names.

Access the Container Shell
To access the shell inside the running container, you can use the docker exec
command with the -it
flags (interactive mode and pseudo-TTY):
docker exec -it <container-name-or-id> /bin/bash
Here's what each part of the command means:
docker exec
: The Docker command to execute a new command inside a running container.-it
: This flag enables interactive mode (-i
) and allocates a pseudo-TTY (-t
), which allows you to interact with the container's shell.<container-name-or-id>
: The container ID or container name./bin/bash
: The command to be executed inside the container. In this case, we're starting the Bash shell. E.g
$ docker exec -it gracious_khayyam /bin/bash
root@f01c02bfdca9:/app#
Try Kodaschool for free
Click below to sign up and get access to free web, android and iOs challenges.
Example
let's assume that you're troubleshooting an issue with your application that interacts with the Redis database running inside this container. Here's how you can proceed:
- Check Redis Configuration
You can inspect the Redis configuration file to ensure that the settings are correct for your application. Inside the container's shell, navigate to the Redis configuration file directory (usually /etc/redis
) and open the configuration file (e.g., redis.conf
) using a text editor like nano
or vim any of you editors
.
cd /etc/redis
vim redis.conf
Review the configuration settings, such as the bind
address, port number, and any other application-specific settings. Ensure that the configuration matches your application's requirements.
2 Check Redis Logs
If you're experiencing issues with Redis, it's a good idea to check the Redis logs for any error messages or warnings. The location of the Redis log file may vary depending on the Redis version and configuration, but it's often located in the /var/log/redis
directory.
tail -n 100 /var/log/redis/redis.log
This command will show the last 100 lines of the Redis log file, which can help you identify any issues or errors related to Redis.
3 Test Redis Connectivity
To ensure that your application can connect to the Redis server running inside the container, you can use the redis-cli
utility. First, make sure that the redis-cli
is installed inside the container:
apt-get update apt-get install redis-tools
Once installed, you can test the Redis connectivity by running the redis-cli
command
This will open the Redis command-line interface. From here, you can try running various Redis commands to test the connectivity and functionality of the Redis server.

As you can see, you can effectively use docker exec
to access the shell inside the container, inspect the Redis configuration and logs, test connectivity, and troubleshoot any issues related to the Redis server running within the container.
That is it.
Happy Coding!