简体   繁体   English

"码头工人组成:NodeJS + MongoDB"

[英]Docker Compose : NodeJS + MongoDB

I can't make my docker compose work.我不能让我的 docker compose 工作。

Here's my dockerfile:这是我的码头文件:

FROM node:0.12
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev
RUN mkdir /myapp
WORKDIR /myapp
ADD . /myapp
RUN npm install

Sorry @gettho.child, I accepted your answer too quickly. 对不起@ gettho.child,我太快接受了你的回答。 I thought it was working but it wasn't. 我认为它有效,但事实并非如此。 I'll report here my final solution since I have been struggling quite some to achieve it. 我将在这里报告我的最终解决方案,因为我一直在努力实现它。

Dockerfile : Dockerfile

FROM node:0.12
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev libkrb5-dev
RUN mkdir /myapp
WORKDIR /myapp
ADD package.json /myapp/package.json
RUN npm install
ADD . /myapp

docker-compose.yml : docker-compose.yml

db:
  image: mongo
  ports:
    - "27017:27017"
  command: "--smallfiles --logpath=/dev/null"
web:
  build: .
  command: node app.js
  volumes:
    - .:/myapp
  ports:
    - "3000:3000"
  links:
    - db
  environment:
    PORT: 3000 # this is optional, allows express to use process.env.PORT instead of a raw 3000

And the interesting app.js extracts: 而有趣的app.js提取:

var MONGO_DB;
var DOCKER_DB = process.env.DB_PORT;
if ( DOCKER_DB ) {
  MONGO_DB = DOCKER_DB.replace( 'tcp', 'mongodb' ) + '/myapp';
} else {
  MONGO_DB = process.env.MONGODB;
}
var retry = 0;
mongoose.connect(MONGO_DB);

app.listen(process.env.PORT || 3000);

Regarding the process.env.DB_PORT I've tried many things around. 关于process.env.DB_PORT我尝试了很多东西。 If it doesn't work out-of-the-box, I suggest to console.log(process.env); 如果它没有开箱即用,我建议使用console.log(process.env); and look for mongo's IP. 并寻找mongo的IP。

The final URL should look like: mongodb://172.17.0.76:27017/myapp 最终的URL应如下所示: mongodb://172.17.0.76:27017/myapp

Good luck, it is worth it, Docker's awesome! 祝你好运,值得拥有,Docker真棒!


EDIT: 编辑:

If the above works, I now found a techno-agnostic workflow by running: 如果上述工作正常,我现在通过运行以下方法找到了与技术无关的工作流程:

  • docker-compose run web /bin/bash
  • and in there running printenv 并在那里运行printenv

I hope this is not too much self promotion but I wrote a double article on the topic which may help some of the readers: https://augustin-riedinger.fr/en/resources/using-docker-as-a-development-environment-part-1/ 我希望这不是太多的自我推销,但我写了一篇关于这个主题的双重文章,可能会对一些读者有所帮助: https//augustin-riedinger.fr/en/resources/using-docker-as-a-development-环境部分-1 /

Cheers 干杯

确保'Server.js'文件中的mongoDB IP(和端口)设置为'PORT_27017_TCP_ADDR'(也检查端口) - 运行'docker exec {web app container id} env'时可以找到。

Your docker-compose should be 你的docker-compose应该是

environment:
   - MONGODB=3000

See this link for more information on mapping environment variables. 有关映射环境变量的更多信息,请参阅此链接 You are declaring the environment variable as PORT instead of MONGODB . 您将环境变量声明为PORT而不是MONGODB

Since we use docker-compose.yml<\/code> and for reliable connection web service with db<\/code> (mondoDB in this case) the final MONGO_DB<\/code> connection string should look like (without authorization and additional args) as the following:由于我们使用docker-compose.yml<\/code>并且为了与db<\/code> (在本例中为 mondoDB)建立可靠的连接 web 服务,最终的MONGO_DB<\/code>连接字符串应如下所示(无需授权和附加参数):

mongodb://db:27017/myapp

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

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