简体   繁体   English

如何使用Docker Compose在容器中运行命令?

[英]How to run a command in a container using Docker Compose?

I have the following situation: I have one Node.js app which uses MongoDB so that with Docker Compose I can have two containers: one for the Node app and another one for the MongoDB. 我遇到以下情况:我有一个使用MongoDB的Node.js应用程序,因此使用Docker Compose可以有两个容器:一个用于Node应用程序,另一个用于MongoDB。

Now, the app must suppor the following feature: one can upload a csv file which will be imported into Mongo using mongoimport . 现在,该应用必须支持以下功能:可以上传一个csv文件,该文件将使用mongoimport导入到Mongo中。

With Node, running mongoimport is easy, because we have the exec function. 使用Node,运行mongoimport很容易,因为我们具有exec函数。 That is, it would be a matter of doing something like: 也就是说,将需要执行以下操作:

const exec = require('child_process').exec;

exec('mongoimport -d database -c collection --type csv file.csv', function (error, stdout, stderr) {
    // Other stuff here...
});

The only problem is: mongoimport will not be available, because MongoDB is inside another container than the Node app! 唯一的问题是: mongoimport将不可用,因为MongoDB位于Node应用程序之外的另一个容器中!

Now how can this be solved? 现在如何解决? I thought on using ssh through exec to run this on the Mongo container, but I'm unsure about it. 我曾想过通过exec使用ssh在Mongo容器上运行它,但是我不确定。

How can I run the mongoimport from the Node app into the Mongo container? 如何从Node应用程序将mongoimport运行到Mongo容器中?

I would install the required tool into my Node.JS Container. 我将所需的工具安装到我的Node.JS容器中。 If you don't want to create your own image, you could use this one: 如果您不想创建自己的图像,则可以使用以下图像:

https://hub.docker.com/r/bitliner/nodejs-bower-grunt-mongodbclients/ https://hub.docker.com/r/bitliner/nodejs-bower-grunt-mongodbclients/

Including: 包含:

  • Mongoclient-Tools Mongoclient-工具
  • Node.JS / Bower and Grunt Node.JS / Bower和Grunt

If you want to build your own image, include: 如果要构建自己的映像,请包括:

FROM image:latest

RUN apt-get install -y mongodb-clients

and build it with: 并使用:

docker build -t node-mongoclient . 

and replace the image in your docker-compose.yml file the self created one. 并将映像替换为自己创建的docker-compose.yml文件。

If the job is that heavy, another approach would be to create a separate service for it, ie, another container that shares a volume with the Node app (so it can get the files) and a build spec (Dockerfile) that includes mongoimport and a little app that allows you to communicate with the Node app (http or pub/sub, etc.). 如果工作如此繁重,则另一种方法是为其创建单独的服务,即,另一个与Node应用共享一个卷的容器(以便它可以获取文件)以及一个包含mongoimport和一个小应用程序,可让您与Node应用程序进行通信(http或pub / sub等)。 This model is nice in that you can abstract the actual functionality (mongoimport, whatever) from your main app, as well as from the db. 该模型非常不错,因为您可以从主应用程序以及数据库中提取实际功能(mongoimport,无论如何)。

For an http service, you can use Node or anything really, and yes some kind of API would be good. 对于http服务,您可以使用Node或其他任何东西,是的,某种API会很好。 For pub/sub you could use Redis with Node as one example that would be pretty lightweight. 对于发布/订阅,您可以将Redis与Node一起使用,这是一个非常轻量级的示例。 It would allow any of your services that have access to a Redis client to participate. 它将允许您有权访问Redis客户端的任何服务参与。 Of course, you can also use Mongo if you're more comfortable with it, but Redis pub/sub is very straightforward. 当然,如果您更喜欢Mongo,也可以使用它,但是Redis pub / sub非常简单。 The one thing in your case is you would want to make sure the file was fully uploaded before notifying your service to work on it. 您遇到的一件事是,您需要确保文件已完全上传,然后再通知您的服务以进行处理。

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

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