How to Run Docker exec Command in Docker Container? (Docker exec into Container)
Docker is a widely used open-source tool that simplifies application deployment, scaling, and management using container technology. With Docker Containers, developers can package applications and their dependencies into portable containers, ensuring consistency across different environments.
What does Docker exec do?
One of Docker's key commands is docker exec, which lets users execute commands inside a running container. This feature allows direct interaction with the container's processes and environment, making it useful for troubleshooting or managing applications.
Docker is versatile and compatible with various operating systems, including Linux, Windows, and macOS. It can even be installed on a Virtual Private Server (VPS). Setting up Docker requires following specific configuration steps tailored to your chosen platform.
How to Use docker exec Command?
Before using the docker exec command, ensure Docker is properly installed on your system. Then, identify the specific container where you want to run the command.
In this guide, you will explore the docker exec command. We will demonstrate how to run docker exec as root inside a docker container using different use cases.
Prerequisites
- Verify that you have the latest version of Ubuntu installed on your system.
- Install Docker by following the steps in Docker's official documentation.
- Add your user to the Docker group to run Docker commands without needing to enter the administrator password each time.
Start a Docker Container
To illustrate how to use the docker exec command, we’ll first start a container.
First, pull an image into a Docker container:
# docker pull redis
Here’s an example using a Redis container:
# docker run -d --name myredis redis
This command starts a Redis container in detached mode (-d flag), running it in the background. The container, named myredis, will serve as our example environment for executing commands.
Running Commands Inside a Docker Container (Docker Container)
You'll first need its container ID to execute commands within a container. You can find the container ID by listing all running containers using the following command:
# docker ps
Once you have the container ID (e.g., 6a8b7d170a12), you can use it to run commands inside the container.
Docker exec Command Syntax (Docker exec as root)
The general syntax of the docker exec command is:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Here are the commonly used options:
-d, --detach: Runs the command in the background.
--detach-keys: Sets a custom key sequence to detach from the container.
-e, --env: Specifies environment variables for the command.
--env-file: Loads environment variables from a file.
-i, --interactive: Keeps the standard input (STDIN) open even if not attached to a terminal.
--privileged: Grants the command extended privileges.
-t, --tty: Allocates a pseudo-terminal for the command.
-u, --user: Runs the command as a specific user or UID.
-w, --workdir:Sets the working directory inside the container for the command.
Running docker exec into Container in Non-Interactive Mode (docker exec into container)
In non-interactive mode, you can execute a single command inside a running container. For example, to check the Redis version inside a running container, use the following command:
# docker exec <container_id> redis-server --version
This command runs the redis-server --version inside the specified container (replace <container_id> with the actual ID, e.g., 6a8b7d170a12). It retrieves and displays the Redis version directly in your terminal.
Running Commands in an Interactive Shell
Interactive mode allows you to open a shell session inside a container, making it possible to run multiple commands and explore the container’s environment. To access an interactive shell, use the following syntax:
docker exec -it CONTAINER COMMAND [ARG...]
For example, to open a shell in a running Redis container, use:
# docker exec -it 6a8b7d170a12 bash
Replace <container_id> with the ID of your Redis container (e.g., 6a8b7d170a12). This command opens a shell session inside the container, letting you interact with the environment and execute multiple commands.
Example: Running a Command Inside the Interactive Shell (Docker bash)
Once inside the shell, you can execute Redis-specific commands. For instance:
# redis-cli ping
This command checks the status of the Redis server running within the container, returning PONG if it’s functioning correctly. You can now explore the container’s file system and environment as needed.
Running Docker exec as Different User (docker exec as user)
By default, commands are executed with docker exec as root inside the container. However, you can specify a different user for the command. To run a command as a specific user, use the following syntax:
docker exec -u USERNAME CONTAINER COMMAND [ARG...]
For example, to run the “whoami” command as the Redis user inside a Redis container, you would use:
# docker exec -u redis 6a8b7d170a12 whoami
Running Commands with Environment Variables
You can run commands inside a container while setting specific environment variables using the following syntax:
docker exec -e VAR_NAME=VAR_VALUE CONTAINER COMMAND [ARG...]
For instance, to run the env command inside a Redis container with a custom environment variable, use:
# docker exec -e MY_VARIABLE='testing_env' <container_id> env
This sets MY_VARIABLE to testing_env and then runs the env command to display the container's environment variables. This method is useful for passing sensitive data, such as API keys, securely into a container without exposing them in the container’s environment.
Conclusion
This guide covered various ways to interact with Docker containers, including running commands non-interactively, accessing an interactive shell, using environment variables, and executing docker exec as root and as a different user. These methods offer flexibility in managing Docker containers. For more details, refer to the official Docker exec documentation.
BlueVPS.com provides high-performance VPS hosting with unlimited traffic, scalability, and a dedicated environment. Start now and enjoy reliable, customizable cloud hosting!
Blog