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

+1 vote

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,270 points
101,738 views

11 answers to this question.

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,850 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
• 41,820 points
0 votes

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
0 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
0 votes

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
0 votes

Use bash with -c

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

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 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

Related Questions In Docker

0 votes
1 answer

Can I run docker-compose inside a container?

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

answered Jun 10 in Docker by Sirajul
• 32,020 points
369 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 in Docker by Sirajul
• 32,020 points
333 views
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 in Docker by Sirajul
• 32,020 points
115 views
0 votes
2 answers

How to edit file after I shell to a docker container?

You can even install it using a ...READ MORE

answered Apr 23 in Docker by Ashish
3,116 views
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
• 56,900 points
195 views
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,850 points
375 views
0 votes
1 answer