How can I run a docker exec command inside a docker container

+15 votes

When I try to run the following command I get an error :

docker exec -i nullmailer sendmail -f user@yahoo.com

This shows the following error:

the docker command does not exist
Jul 18, 2018 in Docker by Tyrion anex
• 8,700 points
1 flag 1,229,337 views
After download virtualbox i will Click on virtualbox it is asking 32-bit windows hosts are not supported by this virtualbox release please tall me fast bro
Hi@Prasad,

You need to enable Virtualization in your windows system. Go to your BIOS and enable it. It will work.
  1. Use docker ps to get the name of the existing container.
  2. Use the command docker exec -it <container name> /bin/bash to get a bash shell in the container.
  3. Or directly use docker exec -it <container name> <commandto execute whatever command you specify in the container.

May be this can help. I am new here and found this. 

19 answers to this question.

+2 votes
Best answer

you can run any command in a running container just knowing its ID (or name):

docker exec -it <container_id_or_name> echo "I'm inside the container!"

Note:The container needs to be running.

Join Edureka's DevOps Training program now and take your career to the next level!

answered Dec 10, 2018 by Pramiti

selected Dec 6, 2019 by Kalgi

If you’re walking the boxes on the equal host then you may execute docker commands in the field. This can be executed by way of defining the Mailinator for using temp emails docker socket inside the container.

are you sure about it?
Did you try this?
0 votes

If you’re running the containers on the same host then you can execute docker commands within the container. This can be done by defining the docker socket in the container.

To do this, run the container and mount the 'docker.sock' in the following manner:

docker run -v /var/run/docker.sock:/var/run/docker.sock ...

You can finally execute docker commands from inside the container.

Ready to level up your Docker skills? Enroll now in our comprehensive Docker Course!

answered Jul 18, 2018 by Sophie may
• 10,620 points
0 votes

You need to get inside the container and then try to execute your command

Follow these steps:

  • Use docker ps to get the name of the existing container
  • Use the command docker exec -it <container name> /bin/bash to get a bash shell in the container
  • Or directly use docker exec -it <container name> <command> to execute whatever command you specify in the container.
answered Aug 24, 2018 by Kalgi
• 52,350 points
+2 votes

Run the container in the following manner:

docker run -it -d ashok/pyashokproj bin/bash

If you would like to attach to an already running container:

docker exec -it CONTAINER_ID /bin/bash
answered Dec 10, 2018 by Ashok
0 votes

This seemed pretty simple and helped me.

  • To start an existing container which is stopped
docker start <container-name/ID>
  • To stop a running container
docker stop <container-name/ID>
  • Then to login to the interactive shell of a container
docker exec -it <container-name/ID> bash
answered Dec 10, 2018 by Sahil
0 votes

This shouldhelp

docker create --name=new_container -it ubuntu
docker start new_container
// ps -a says 'Up X seconds'
docker exec new_container /path/to/my/command
// ps -a still says 'Up X+Y seconds'
docker exec new_container /path/to/another/command
answered Dec 10, 2018 by New_container
0 votes

To make debugging easier, we’re introducing docker exec, which allows a user to spawn a process inside their Docker container via the Docker API and CLI.  For example…

$ docker exec -it ubuntu_bash bash

will create a new Bash session inside the container ubuntu_bash.

answered Dec 10, 2018 by Bash
+1 vote

You can run a command in a container:

 docker-compose run <container name> <command>

 For example, to get a shell into your web container you might run 

docker-compose run web /bin/bash

To run a series of commands, you must wrap them in a single command using a shell. For example:

docker-compose run <name in yml> sh -c '<command 1> && <command 2> && <command 3>'
answered Dec 10, 2018 by Haldia
+1 vote

Use bash with -c

docker exec -it CONTAINER_ID bash -c "mydocker_sql /usr/share/zoneinfo | mysql mysql"
answered Dec 10, 2018 by Kiyara
+1 vote

Another approach is to start and attach the container at the same time. Something like this:

docker start <CONTAINER_ID/CONTAINER_NAME> && docker attach <CONTAINER_ID/CONTAINER_NAME> 

answered Dec 10, 2018 by Manoharan
0 votes
I guess there’s no docker runtime in your container.

I am not sure if this will work. Run the container interacitvely and install docker there. Docker can run on a host or virtual machine, I guess not be able to run at a container
answered May 14, 2019 by Frank
What do you mean by "Run the container interactively"?

He's trying to say, get into the "interactive mode" with docker container. use the docker run command with this flag: 

docker run -it
+2 votes

Hi,

The command to execute something in a container is - 

docker exec <container_id/container_name> <instruction/cmd to be executed>

In your case, you are trying to send a mail I believe, so:

docker exec -d <container_name> sendmail -f user@yahoo.com

This should run the command in a detached state/ run the process in the background.

In case you want your stdin to be open, replace -d with -i.

sendmail is an utility I believe, incase it is not running, run the command from inside the container and if needed install the sendmail util:

docker exec -i -t <container_name> /bin/bash (or /bin/sh)

This tells docker to run it in an interactive mode, gives you back a tty/terminal and runs the /bin/bash command to provides you a bash terminal basically.

Run the cmd from the terminal and check :)

Also check the root process inside the container : PID 1
The command started using docker exec only runs while the container’s primary process (PID 1) is running, and it is not restarted if the container is restarted.

ref : https://docs.docker.com/engine/reference/commandline/exec/

answered Dec 31, 2019 by Priyak
• 180 points

Hi Priyak. Thanks for such an amazing explanation. I have a doubt though. What do you mean by 

"If you want your stdin to be open"

0 votes

If you want to run or execute commands from one container to another you can to ssh but there is another way to do it.

first, you need to go to your host os and edit the docker.service file to enable TCP requests with docker. with this, any os in your network can access your docker commands (even other containers connected to host).

check the below link where I have enabled docker commands to run from container to host. I have also shared the dockerfile for your reference.

https://hub.docker.com/r/smc181002/docker_enabled

answered Jun 16, 2020 by Meher Chaitanya
0 votes
answered Jul 23, 2020 by Gitika
• 65,890 points
+1 vote
docker exec -it <container name> bash
answered Aug 9, 2020 by Mahantesh
• 220 points
0 votes

You can follow these steps:

  1. Use docker ps to get the name of the existing container.
  2. Use the command docker exec -it <container name> /bin/bash to get a bash shell in the container.
  3. Or directly use docker exec -it <container name> <commandto execute whatever command you specify in the container.
Hope this will help
Best Regards
answered Aug 26, 2020 by Pistle
• 1,000 points
+1 vote
docker exec -it <container_id_or_name> echo "I'm inside the container!"

Run with different codes if its not working

answered Aug 28, 2020 by rehanipa
• 160 points

edited Aug 28, 2020 by Gitika
0 votes

You can run the below command.

$ docker ps --all

Get the image you want to execute commands in and run:

$ docker run -it --entrypoint /bin/bash $IMAGE_NAME -s
answered Oct 28, 2020 by jjordan

edited Oct 28, 2020 by MD
0 votes

Hi,

You can execute a command on the container using the docker exec command.

$ docker exec -d ubuntu_bash touch /tmp/execWorks

This will create a new file /tmp/execWorks inside the running container ubuntu_bash, in the background. Next, execute an interactive bash shell on the container.

$ docker exec -it ubuntu_bash bash
answered Dec 10, 2020 by MD
• 95,460 points

Related Questions In Docker

0 votes
1 answer

How to run a docker command from inside the container?

You must have come across the /var/run/docker.sock file, ...READ MORE

answered Jun 28, 2018 in Docker by Sophie may
• 10,620 points
3,485 views
+1 vote
2 answers

How do I run a docker image as a container?

You can run an image depends on whether you ...READ MORE

answered Sep 7, 2018 in Docker by Damon Salvatore
• 5,980 points
1,571 views
0 votes
1 answer

How can I connect a docker container to two docker networks that I have created?

Hi@akhtar, It's correct that the network argument of ...READ MORE

answered Mar 23, 2020 in Docker by MD
• 95,460 points
10,788 views
0 votes
1 answer

How to run a cron job inside a docker container?

Hii, crond works well with tiny on Alpine RUN apk add --no-cache ...READ MORE

answered May 18, 2020 in Docker by Niroj
• 82,880 points
5,240 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,100 points
2,687 views
0 votes
1 answer

Can I run docker-compose inside a container?

Compose can also be run inside a ...READ MORE

answered Jun 10, 2019 in Docker by Sirajul
• 59,230 points
6,090 views
0 votes
2 answers

How to execute docker exec commande inside a docker container ?

You need to get inside the container ...READ MORE

answered Aug 13, 2019 in Docker by Sirajul
• 59,230 points
3,751 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP