Docker exec: How to Run Commands in a Docker Container

June 16th, 2023
Docker exec: How to Run Commands in a Docker Container

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. If you're a macOS user, check macOS versions list to verify that your operating system is compatible with Docker. Once you've confirmed compatibility, the only thing you need to do is research the exact steps to configure it properly.

How to use docker exec?

To use 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.

Prerequisites

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.

Ready to supercharge your Docker infrastructure? Scale effortlessly and enjoy flexible storage with Cherry Servers bare metal or virtual servers. Eliminate infrastructure headaches with free 24/7 technical support, pay-as-you-go pricing, and global availability.

Docker exec syntax

The basic syntax for the docker exec command is as follows:

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

The options are:

Name Description
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty 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:

docker ps

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

docker exec

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

docker exec -it

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:

interactive shell inside 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

docker exec -e

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 nginx:

docker exec -u nginx 14728081e141 whoami

docker exec -u

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.

Conclusion

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

With over 20 years in IT, Didier has been creating technical documentation for companies, catering to both technical and non-technical audiences. Didier is an expert in Linux system administration, DevOps, cloud computing, cybersecurity, IT consulting, management consulting, technical writing, Diataxis framework, Doc-as-Code, UX Writing, Jamstack, MkDocs, Docker, containers, open-source, SDLC, and Python programming. His hands-on technical expertise, coupled with his strong communication skills, enables him to bridge the gap between developers and end-users. Didier creates user guides, API References, end-user documentation, how-tos, and tutorials. He is an expert in authoring using modern technologies such as Markdown, Mermaid, and static-site generators. Didier also utilizes frameworks and methodologies such as Diaxiatis and Doc-as-code, applying structured writing techniques. Currently, Didier works as a freelance technical writer and documentation consultant, assisting organizations in creating comprehensive and easy-to-understand documentation for their software and web applications. In his previous roles, Didier worked as a system and applications engineer and implemented style guides, tone and voice best practices, and documentation processes that streamline their release cycles. Didier resides in Vacoas, Mauritius.

Start Building Now

Deploy your new Cloud VPS server in 5 minutes starting from $5.83 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: b0376f5bd.760