Open In App

Docker exec

Last Updated : 23 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In containerization, the `docker exec` command stands out as a powerful tool for interacting with running Docker containers. This article explores the capabilities and usage of `docker exec`, detailing how it facilitates seamless communication and control over containerized applications.

Before trying to run the Docker commands ensure that the Docker software is set up and the service is active. Check the status of the docker service with the command `systemctl status docker` If the status is active then the docker service is ready to use and perform the container operations and management. To start the docker service try to run the following command

systemctl start docker 

( or ) To automatically start the docker service, Once the base OS is started try to use this command:

 systemctl enable docker --now 

For Installation refer to the Article – How to Install Docker on Linux Distributions

What is Docker Exec ?

The `docker exec` is a docker command that allows users to run commands inside a running Docker container, bridging the gap between the container environment and the host system. This command opens up possibilities for troubleshooting and debugging. It performs administrative-related tasks within a live container.

Basic Syntax Of Docker Exec

The following is the basic syntax of the docker exec command usage:

docker exec [ OPTIONS ] CONTAINER Command [ ARG… ]

  • `OPTIONS`: It provides the additional settings for the execution of docker commands.
  • `CONTAINER`: you have to provide the ID or name of the specifying container upon replacing it.
  • `Command`: The command that you want to execute within the container has to be specified here.
  • `ARG`: It provides the additional arguments that need to be passed to the command.

How to Use Docker Exec?

  • Through the below figure, you will understand the usage of the docker exec. Firstly I viewed the list of running containers with the `docker ps` command.
  • Next, enter into the particular container environment from the available running containers. I used the container_name with the command `docker exec -it myos1 bash`.
  • It provide the docker an execution environment with interactive ( -i ) and terminal ( -t ) mode on running the bash program.
  • Once you entered into that specified container environment you will can see the unique hostname on left side of the command prompt that can also be know through the command `hostname`.
  • The unique hostname is the proof of identity that you are working in that container.
  • To come back of the container try on using [ ctrl + p + q ], by this you landed back to the host system environment.

Docker-exec

Using Interactive Shell with Docker Exec

This command opens an interactive shell within the running docker container. It allows in facilitating real-time interaction through running the bash program. If you terminate ( exit ) the bash program inside the container, the life span of the container will ended and you will be landed on the host environment.

docker exec -it container_name  /bin/bash

Usage of Docker exec with Single Command

Execute a single command within the container without entering an interactive shell. In this the command will be executed inside the container and the container will be terminated .

docker exec container _name command

Usage of Docker Exec with Arguments

Pass arguments to the executed command within the container. In this echo command with argument “Hello , Docker” is passed , docker executes this command with its arguements inside the docker.

The following is the example of docker exec usage with arguments:

docker exec container_name echo "Hello, Docker"

Passing Environmental Variables using Docker exec

On using -e flag with docker exec command you can set the environmental variables inside the running docker container.

Syntax

 docker exec -e  variable1=value1 -e variable2=value2 <container_name> command

The following is the example of passing environmental variables using docker exec :

docker exec  -e mykey='6174' mycontainer echo $myname

Setting the Working Directory Using Docker Exec

On Using -w flag with `docker exec` command you can specify the working directory from a command and run the commands from their inside the running container.

Syntax

 docker exec -w <directory_name> <container_name> command

Example

 docker exec -w /mydir mycontainer ls 

Options of Docker Exec Command

  • `-i` or `–interactive`: This option is useful to keep the standard input (STDIN) open for the docker command. It allows the interactive communication with the running process making it suitable for interactive applications.
  • `-t` or `–tty`: This option will provides a pseudo-Terminal ( TTY ) for the command. A TTY is a terminal emulation that provides more interactive and user-friendly interface, resembling the behavior of a local terminal.
  • `–user`: This option is used to specify the user context either as username or a User ID (UID) for the container. This is particularly useful when you want to execute a command with specific user privileges or where you would like to switch to a different user account for security or access control reasons.

Usage Of Docker Exec

1. Troubleshooting and Debugging with Docker Exec Command

  • When troubleshooting issues raises within a live container, inspecting various aspects becomes crucial. Examing the File systems to identify any unexpected changes, Insights of the logs on the application’s behavior, and inspecting environment variables helps understand the runtime context. Tools like docker exec allow users to enter a running container to interactively troubleshoot and debug issues. The real-time inspection with tools like docker exec facilitates rapid diagnosis making it an essential aspect of maintaining healthy containerized applications.

2. Significance of Docker Exec in Application Maintenance

  • Containers provides seamless application maintenance with routine maintenance tasks such as applying software updates or performing database migrations can be executed without the need to stop the entire container. This can be achieved through the strategies like rolling updates or using orchestration tools ensuring of zero downtime enhancing the system reliability and availability.

3. Security Audits

  • Security audits improves the overall robustness of the containerized applications. It involves in examining the containerized environments in various elements such as analyzing running processes helps in detecting unauthorized activities within a container, by proactively addressing security concerns we can safeguard the sensitive data inside the container.

Conclusion

In conclusion, the `docker exec` command is mostly used for providing a seamless interaction with running containers. It is useful in troubleshooting the issues, performing the maintenance tasks and for conduction of security audits. `docker exec` provides the ability to enter inside the container environment and to execute the commands effortlessly. Mastering this command enhances control over container applications working more efficient and streamlined container management workflow.

Docker Exec – FAQs

What is Exec in Docker?

The `docker exec` is a docker command that helps in excuting the instructions or commands inside a running docker container.

Example: Step1: Create a docker container

 docker run -dit --name myos1 centos:latest

Step 2: Executing command inside the docker container

 docker exec myos1 date

What is the Entrypoint For Docker Exec?

The name or ID of the Docker container that is currently running is used as an entrypoint for docker exec providing as arguement and specifying the command you want to execute.

Syntax: “docker exec <container_name or ID> command

Example:

docker exec mycontainer date

How Do I Run Two Commands in Docker Exec?

To run multiple commands in `docker exec` specify the commands speparating with a semicolon.

Syntax: docker exec <container> sh -c “command1;command2”

Example:

docker exec mycontainer sh -c "date;cal"

How to Run a Command in Docker Exec Bash?

To run an interactive bash shell program inside the specified running docker container use the following command:

Syntax: docker exec -it <container_name or container_id> bash

Example: Executing bash program inside mycontainer with interactive terminal options using docker exec

docker exec -it mycontainer bash

Why Do We Use Docker exec?

To execute commands inside the running docker containers we use docker exec. It facilitates with debugging, troubleshooting inside the running container , you want to run.

What is the Difference between Docker run and Docker exec ?

Docker run command is used to start a new container from a docker image whereas Docker exec command is used to execute commands within the running container that are already created and running.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads