简体   繁体   English

从 shell 到 docker 容器执行多个命令

[英]Execute multiple commands from shell to docker container

I am trying to do in one command, do a bash from docker container, open mongo and make a request with something like我试图在一个命令中执行,从 docker 容器执行 bash,打开 mongo 并使用类似的内容发出请求

'db.test.find({});' 'db.test.find({});'

I am reading from others similar post without success.我正在阅读其他类似的帖子,但没有成功。

This is the command that I am executing.这是我正在执行的命令。

docker exec -it mongoDB bash -c 'mongo ; docker exec -it mongoDB bash -c 'mongo ; use test;使用测试; db.test.find({})' db.test.find({})'

And that command give me that那个命令给了我那个

bash: -c: line 0: syntax error near unexpected token {}' bash:-c:第 0 行:意外标记附近的语法错误 {}'

bash: -c: line 0:mongo ; bash: -c: 行 0:mongo ; use test;使用测试; db.test.find({})' db.test.find({})'

It is that possible?这可能吗?

I hope that you can help me.我希望你能帮助我。

Regards,问候,

Roth.罗斯。

This is actually possibla using a file to execute multiple commands.这实际上是可能的,使用一个文件来执行多个命令。 Put your mongo script into a file.将您的 mongo 脚本放入一个文件中。

Example:示例:

myFile.js

File content:文件内容:

use myDB
show collections

Then you can execute this file with然后你可以执行这个文件

mongo < myFile.js

In your situation:在你的情况下:

docker exec -it mongoDB bash -c 'mongo < myFile.js'

Don't forget to copy this file into your docker container.不要忘记将此文件复制到您的 docker 容器中。

You can read more here你可以在这里阅读更多

You don't really need to copy the script into the container.您实际上并不需要将脚本复制到容器中。

You can simply run :你可以简单地运行:

docker exec -it mongoDB bash -c 'mongo' < myFile.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM