How to use the command 'docker exec' (with examples)

How to use the command 'docker exec' (with examples)

The ‘docker exec’ command is used to execute a command on an already running Docker container. It allows you to interact with a running container, run a command in the background, specify the working directory, set environment variables, and execute a command as a specific user.

Use case 1: Enter an interactive shell session on an already-running container

Code:

docker exec --interactive --tty container_name /bin/bash

Motivation: This use case enables you to access an interactive shell session within a running container. It can be useful when you need to debug or troubleshoot issues within the container environment.

Explanation:

  • --interactive: Allocates a pseudo-TTY, enabling an interactive shell session.
  • --tty: Allocates a pseudo-TTY, which provides a terminal-like interface.
  • container_name: Specifies the name of the container to execute the command on.
  • /bin/bash: The command to execute within the container, in this case, an interactive bash session.

Example output:

root@container:/#

Use case 2: Run a command in the background (detached) on a running container

Code:

docker exec --detach container_name command

Motivation: Executing a command in the background on a running container allows you to perform tasks without needing to interact with the container directly. This can be helpful when you want to run automated scripts, perform background service configurations, or execute long-running processes.

Explanation:

  • --detach: Detaches the container’s process after executing the command, allowing it to run in the background.
  • container_name: Specifies the name of the container to execute the command on.
  • command: The command to execute within the container.

Example output: No output is displayed as the command runs in the background.

Use case 3: Select the working directory for a given command to execute into

Code:

docker exec --interactive --tty --workdir path/to/directory container_name command

Motivation: By specifying the working directory for a command execution within a container, you can ensure that the command operates within the desired directory context. This can be useful, for example, when running build commands that require a specific working directory.

Explanation:

  • --interactive: Allocates a pseudo-TTY, enabling an interactive shell session if required by the command.
  • --tty: Allocates a pseudo-TTY, which provides a terminal-like interface if required by the command.
  • --workdir path/to/directory: Specifies the working directory path within the container for executing the command.
  • container_name: Specifies the name of the container to execute the command on.
  • command: The command to execute within the container.

Example output:

output of the command executed in the specified working directory

Use case 4: Run a command in the background on an existing container but keep stdin open

Code:

docker exec --interactive --detach container_name command

Motivation: Running a command in the background while keeping stdin open enables interaction with the command’s input/output streams without being attached to the container’s terminal. This can be useful, for example, when running database queries or executing scripts that require user input.

Explanation:

  • --interactive: Allocates a pseudo-TTY, enabling interaction with the command’s input/output streams.
  • --detach: Detaches the container’s process after executing the command, allowing it to run in the background.
  • container_name: Specifies the name of the container to execute the command on.
  • command: The command to execute within the container.

Example output:

output of the command executed in the background

Use case 5: Set an environment variable in a running Bash session

Code:

docker exec --interactive --tty --env variable_name=value container_name /bin/bash

Motivation: Setting environment variables in a running Bash session allows for dynamic configuration changes. This can be useful when you need to modify the behavior of a command or script within a container without restarting it.

Explanation:

  • --interactive: Allocates a pseudo-TTY, enabling an interactive shell session.
  • --tty: Allocates a pseudo-TTY, which provides a terminal-like interface.
  • --env variable_name=value: Sets an environment variable in the running Bash session within the container.
  • container_name: Specifies the name of the container to execute the command on.
  • /bin/bash: The command to execute within the container, in this case, an interactive bash session.

Example output:

root@container:/# echo $variable_name
value

Use case 6: Run a command as a specific user

Code:

docker exec --user user container_name command

Motivation: Executing a command as a specific user within a container can help ensure that the command has the necessary privileges and access rights. This can be useful when certain operations or file manipulations require specific user permissions.

Explanation:

  • --user user: Specifies the user to execute the command as within the container.
  • container_name: Specifies the name of the container to execute the command on.
  • command: The command to execute within the container.

Example output: Output of the command executed as the specified user.

Conclusion:

The ‘docker exec’ command is a powerful and versatile tool for interacting with running Docker containers. Whether you need to access an interactive shell session, run commands in the background, set environment variables, or execute commands as a specific user, ‘docker exec’ provides the flexibility to manage and control your containerized applications efficiently.

Related Posts

How to use the command 'export' (with examples)

How to use the command 'export' (with examples)

The ’export’ command in Bash is used to mark shell variables in the current environment to be exported with any newly forked child processes.

Read More
How to use the command gunzip (with examples)

How to use the command gunzip (with examples)

The gunzip command is used to extract files from a gzip (.

Read More
How to use the command "git" (with examples)

How to use the command "git" (with examples)

Git is a distributed version control system that allows developers to track changes in their codebase, collaborate with others, and easily manage different versions of their code.

Read More