How to execute docker exec commande inside a docker container

0 votes

I execute a command inside a docker container :

docker exec -i nullmailer sendmail -f username@gmail.com

The container response is :

the docker command does not exist.

How to execute docker exec command inside a docker container ?

Jul 19, 2018 in Docker by Nilesh
• 7,060 points
3,751 views

2 answers to this question.

0 votes

If your containers are running on the same host, you can run docker commands inside the container, by exposing the docker socket to the container.

To achieve this, when running the container, mount the docker.sock as such:

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

Now you can execute docker commands from within the container.

answered Jul 19, 2018 by Kalgi
• 2,680 points
And for Windows it is:

`docker run -v //var/run/docker.sock:/var/run/docker.sock ...`
Hi @Werner, Can you put this up as an answer? It'll be helpful for other readers. Thank you!
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 13, 2019 by Sirajul
• 59,230 points

I am doing this same thing but it isn't working when I have a date variable in it. Have any ideas on how to get this to work? I want to be able to run it from a crontab. 

docker exec -it docker-partkeepr_database_1 bash -c mysqldump --databases partkeepr -upartkeepr -pUSERPASSWORD  > /var/lib/mysql/backup/partkeeprsql.$(date +%Y-%m-%d-%H.%M.%S).sql

It comes back saying -bash: /var/lib/mysql/backup/partkeeprsql.$(date +%Y-%m-%d-%H.%M.%S).sql: No such file or directory

Yet when I run the command mysqldump --databases partkeepr -upartkeepr -pUSERPASSWORD  > /var/lib/mysql/backup/partkeeprsql.$(date +%Y-%m-%d-%H.%M.%S).sql 

after having already done a docker exec -it docker-partkeepr_database_1 bash command to get into the container it runs just fine and puts the date on it.  

Any suggestions? 

Hi,

I think you need to pass your command in a quote. Try with the below command once.

docker exec -it docker-partkeepr_database_1 bash -c "mysqldump --databases partkeepr -upartkeepr -pUSERPASSWORD  > /var/lib/mysql/backup/partkeeprsql.$(date +%Y-%m-%d-%H.%M.%S).sql"

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,487 views
+15 votes
19 answers

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

you can run any command in a ...READ MORE

answered Dec 10, 2018 in Docker by Pramiti
1,229,342 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,241 views
0 votes
0 answers

How to install PHP composer inside a docker container

I'm trying to figure out how to ...READ MORE

Apr 12, 2022 in Docker by Abhijeet
• 180 points
6,700 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
+1 vote
2 answers
0 votes
1 answer
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