How can I run a ‘docker exec’ command inside a docker container?

+3 votes

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

docker exec -i nullmailer sendmail -f

This shows the following error:

the docker command does not exist
Jul 18, 2018 in Docker by Tyrion anex
• 8,380 points
1 flag 388,424 views

14 answers to this question.

+1 vote
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.

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.

answered Jul 18, 2018 by Sophie may
• 9,940 points
+1 vote

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
• 51,890 points
+1 vote

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:


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
+1 vote


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

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 :

answered Dec 30, 2019 by Priyak
• 160 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.

answered Jun 16 by Meher Chaitanya
0 votes
answered Jul 22 by Gitika
• 33,450 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
• 9,940 points
0 votes
2 answers

How do I run a docker image as a container?

You could probably follow these steps : $ docker ...READ MORE

answered Aug 9, 2019 in Docker by Sirajul
• 55,420 points
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 in Docker by MD
• 41,340 points
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 in Docker by Niroj
• 43,540 points
0 votes
1 answer
0 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
• 57,550 points
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
• 55,420 points
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
• 55,420 points