Open In App

Docker exec

Last Updated : 07 May, 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.

Starting a Test Container

The docker exec command is used to know the container that is running. So to execute the command you need a container which is already running if any container is running in the docker you can execute the command on that container. To know the containers which are running in the docker you can use the following command to list all the docker containers.

docker ps 

The above command is used to list all the containers that are running in the docker host. Know to take the container <ID> or <Container name> and use it with the docker exec command.

If there is no container is running in your docker host then run a sample container by using the following command.

docker run <Name of the image> 

Finding the Name of a Docker Container

To execute the docker exec command you need to know the <name of the container> or <container ID> which is already running or the container that you have ruined for the test purpose. For that, you can use the docker ps command it will list all the docker containers as discussed above in the Starting a test container. To know more about the docker ps command you can refer to the Docker Commands.

You can also rename the docker container according to your requirements by using the following command.

docker rename <Present name of container> <New name of the Container>

Running an Interactive Shell in a Docker Container

The main reason behind to interact with the shell container is to to do some debugging like in the production servers you may face issues like the docker container was restarting automatically for certain interval of time in that case you can interact with the docker container shell and you perform the debugs.

The Second scenario can be you want to install some packages in that application which is running inside the docker container in that case you can interact directly with the container shell and perform the installation which are required.

To interact with the docker shell you can use the following command and refer to the screenshot attached below.

docker exec -it <Name of the Container> Or <Container ID> bin/bash

Docker Exec COmmand

Running a Non-interactive Command in a Docker Container

In the above example you are going to interact with the docker container shell. In some cases there is no need of interacting with the docker container you can directly install or perform the debugging operations just bu running the following command.

docker exec <name of the container> <Operation which you want to perform> 

The major difference between the interactive and non-interactive is that security interacting every time with the docker container shell will creates security concerns.

Running Commands in an Alternate Directory in a Docker Container

In some use cases like while downloading any type of software or package they will use the /opt directory. Instead of changing the to that directory each and every time you can use the following command to make the changes in that directory as shown in the below command.

docker exec --workdir /opt <Name of the container> pwd

“pwd” will prints the present working directory.

Running Commands as a Different User in a Docker Container

Running the commands as a different user is one of the best practices which is followed by everyone in the production server. The reasons are mentioned below.

  • Security
  • Isolation
  • Docker community prefers this practice.

Use the following command to run the docker commands as an another user

docker exec --user ec2-user <Name of the container> yum update

The above command will run the docker command in the docker container as an ec2-user and it will update the yum package.

Passing Environment Variables into a Docker Container

Environmental variable in the docker are used to perform the configurations in the docker container instead of hard coding the values you set them as an variable so you can pass them or changes them according to the requirements. Follow the steps mentioned below to pass the environmental variable of docker container.

Step-by-Step Proceses To Set ENV variables

Step 1: Use the “-e flag” to set the environmental variable as show below.

docker exec -e <Name Of The Variable>: <Value Of the variable> 

Step 2: Use the env command at the end of the above command it will print outs the all the environmental variables in the docker container. As shown below

docker exec -e <Name Of The Variable>: <Value Of the variable>  env

Example docker exec with env variable :

You want to set the webapplication for the PROD variable then you can use as the following.

docker exec -e PROD: webapplication env

Run a Command from Outside the Container

No need to access the docker container each and every time when you want to run the command inside the docker container. You can run the docker container command from the outside of the docker container so it will helps you to inspect the docker container and run the docker command at the same time for that you can use the following command

docker exec <Name of the container> cat /file.txt

it will show all the content in the the file.txt with out need of interacting with container.

Access the Running Container’s Shell

Acessing the running container shell will give you full command on the container it will be like executing the commands in the virtual machine to interact with the container shell you can use the following command

docker ecec -it <Name of the container  /bin/bash

The reason we are giving the /bin/bash is it will start the interacting with the bash shell.

Install Docker

Read the following to install the docker on different OS platform.

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 – FAQ’s

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