Running commands inside a Docker container is a powerful feature that allows you to interact with the container's environment, execute scripts, or perform administrative tasks. Docker provides the docker exec command for this purpose. In this how-to tutorial, we will explore how to use docker exec with the example of a Docker Nginx container.
What is Docker exec?
Docker is an open-source platform that enables developers to automate application deployment, scaling, and management using containerization. Docker exec is a command used in the Docker containerization platform to execute a command within a running Docker container. It allows users to run commands inside a container and interact with the processes running within that container.
The platform can work in just about any environment. For example, you can install Docker on a VPS and run it on Linux, Windows, or even macOS. The only thing you need to do is research the exact steps to configure it properly.
How to use docker exec?
docker exec, ensure that Docker is installed on your system, and identify the container in which you intend to execute a command. The next section will guide us through this process.
Ensure you have the latest version of Ubuntu installed on your system. Docker: Install Docker or follow Docker's official documentation. Make sure your user is a member of the docker group. This gives you enough permission to use docker commands without having to type the administrator password on every execution.
Docker exec syntax
The basic syntax for the
docker exec command is as follows:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
The options are:
|Detached mode: run command in the background
|Override the key sequence for detaching a container
|-e, --env list
|Set environment variables
|Read in a file of environment variables
|Keep STDIN open even if not attached
|Give extended privileges to the command
|Allocate a pseudo-TTY
|-u, --user string
|Username or UID (format: <name
|-w, --workdir string
|Working directory inside the container
Docker: Start a container
To demonstrate the usage of docker exec, we will start an Nginx container using the following command:
docker run -d --name mywebserver nginx
The preceding command starts an Nginx container in the background using the
-d flag. This container will serve as our example environment for running commands.
Docker: Run commands in a container
Before running commands inside the container, we need the container ID. To list the container ID, we use the
docker ps command:
Now that we have the container ID
14728081e141 we can run commands inside the container.
Option 1: Run commands in non-interactive mode
In non-interactive mode, we can execute a single command inside a running container. To run a command in non-interactive mode inside the Nginx container, we will use the
docker exec command as follows:
docker exec 14728081e141 nginx -v
The preceding command uses
docker exec to run the
nginx -v command inside container
14728081e141. This command retrieves the Nginx version from the container and displays it in your terminal.
Option 2: Run commands in an interactive shell
Running commands in interactive mode allows us to open a shell inside the container, enabling us to execute multiple commands and interact with the container's environment. To run a command in interactive mode, use the following syntax:
docker exec -it CONTAINER COMMAND [ARG...]
We will open an interactive shell inside container
14728081e141 using the following command:
docker exec -it 14728081e141 bash
The preceding command opens a bash shell inside the container, allowing us to run commands and browse the container's file system. We can now execute various commands and interact with the container like in a regular shell.
Here is an example of executing a command inside the interactive shell inside the container:
Option 3: Run commands with environment variables
We can also run commands inside a container with specific environment variables. To do this, use the following command syntax:
docker exec -e VAR_NAME=VAR_VALUE CONTAINER COMMAND [ARG...]
For example, let's run the env command inside the Nginx container with a custom environment variable:
docker exec -e MY_VARIABLE='testing_env' 14728081e141 env
In the preceding command, we set the environment variable
MY_VARIABLE to the value
testing_env and run the env command to display the container's environment variables.
Declaring an environment variable is often used for security purposes. For example, we can pass API keys to a command without exposing them in the docker container.
Explore how web hosting service provider Debesis improved its service quality, performance, and reliability by migrating to Cherry Servers' bare-metal servers. "Cherry Servers engineers always help when we need them, while their customer service quality is a blast!"
Option 4: Run commands as a different user
By default, commands executed with
docker exec run as the
root user inside the container. However, we can specify a different user to run the command. To run a command as a different user, use the following syntax:
docker exec -u USERNAME CONTAINER COMMAND [ARG...]
We will run the following command using the user
docker exec -u nginx 14728081e141 whoami
In the preceding command, we run
whoami as the nginx user inside the container. The command will return nginx, indicating that it executed successfully with the specified user context.
In this tutorial, we covered various options, such as running commands non-interactively, running in an interactive shell, running with environment variables, and running as a different user. These techniques allow you to effectively interact with your Docker containers, perform tasks, and manage their environments. You can learn more about docker container commands in the official Docker exec documentation