How to Run Docker exec Command in Docker Container? (Docker exec into Container)

Posted on

How to Run Docker exec Command in Docker Container? (Docker exec into Container)

Docker is a popular open-source platform that streamlines application deployment, scaling, and management through containerization. Docker containers allow developers to package applications with all their dependencies into portable units, ensuring consistent operation across various environments.

What does Docker exec do?

A key Docker command is docker exec, which allows users to execute commands directly inside a running container. This facilitates interaction with the container’s processes and environment, proving invaluable for troubleshooting and application management.

Docker’s versatility extends to compatibility with operating systems like Linux, Windows, and macOS, and even installation on a Virtual Private Server (VPS). Setting up Docker requires platform-specific configuration steps.

How to Use docker exec Command?

Before using docker exec, ensure Docker is correctly installed. Then, identify the specific container where you wish to execute the command.

This guide explores the docker exec command with practical examples of running it as root inside a Docker container.

Prerequisites

  • Ensure you have the latest version of Ubuntu installed.
  • Install Docker following the official Docker documentation.
  • Add your user to the Docker group to avoid needing the administrator password for Docker commands.

Start a Docker Container

To demonstrate docker exec, we’ll start a container.

First, pull a Docker image:

# docker pull redis




how to run docker exec command in docker container? (docker exec into container)


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 be our example environment.

Running Commands Inside a Docker Container (Docker Container)

To execute commands within a container, you’ll need its container ID. List running containers:

# docker ps

Once you have the container ID (e.g., 6a8b7d170a12), 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 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)

Non-interactive mode executes a single command inside a running container. To check the Redis version:

# docker exec <container_id> redis-server --version

        
            
                
                    
                
            
        
    

Replace <container_id> with the actual ID (e.g., 6a8b7d170a12) to retrieve and display the Redis version.

Running Commands in an Interactive Shell

Interactive mode opens a shell session inside a container, letting you run multiple commands. Use this syntax:

docker exec -it CONTAINER COMMAND [ARG...]

To open a shell in a running Redis container:

# docker exec -it 6a8b7d170a12 bash

Replace <container_id>. This command opens a shell session for interacting with the environment.

Example: Running a Command Inside the Interactive Shell (Docker bash)

Once inside, execute Redis-specific commands:

# redis-cli ping







This command checks the Redis server status, returning PONG if it’s working. Explore the file system and environment as needed.

Running Docker exec as Different User (docker exec as user)

By default, commands are executed as root. To run as a different user:

docker exec -u USERNAME CONTAINER COMMAND [ARG...]

To run “whoami” as the Redis user:

# docker exec -u redis 6a8b7d170a12 whoami








Running Commands with Environment Variables

Set environment variables for commands using:

docker exec -e VAR_NAME=VAR_VALUE CONTAINER COMMAND [ARG...]

Example: run env with a custom variable:

# docker exec -e MY_VARIABLE='testing_env' <container_id> env







This sets MY_VARIABLE to testing_env and displays environment variables, useful for passing sensitive data securely.

Conclusion

This guide covered interacting with Docker containers, including non-interactive and interactive commands, environment variables, and executing docker exec as root or another user. Refer to the official Docker exec documentation for more information.

BlueVPS.com provides high-performance VPS hosting with unlimited traffic and scalability. Start now and enjoy reliable, customizable cloud hosting!


Blog

Key Changes and Improvements:

  • Clarity and Conciseness: Rewritten sentences for better clarity and conciseness.
  • Emphasis: Maintained the original emphasis (bold text where appropriate.)
  • Code formatting: Added to commands inside paragraph for better readability.
  • Link added: Provided anchor tag for the official docker documentation link.
  • Grammar and Style: Improved grammar and overall writing style making the content easier to understand.
  • HTML Preservation: The HTML structure and tags are preserved.
  • Readability: Slightly adjusted the sentences to sound better to read.
  • No information lost: Carefully rewritten to not lose the intent of the context.
  • Changed the alt descriptions: The atl descriptions were also too long, so I changed the alt descriptions to be shorter and more descriptive.
  • removed redundant style tag. The style tag in the paragraph following the "Docker exec Command Syntax" was redundant.

This rewritten version makes the content more accessible and engaging while preserving its original intent and HTML structure.