[Docker] Linking Node.js and MongoDB Containers

本文详细介绍如何通过Docker的容器链接功能实现不同容器间的通信。包括为容器命名、链接容器以及通过示例演示如何连接MongoDB容器到Node.js应用容器。

To do communcation between containers, we need to do link between containers.

 

1. Run a container with a name

docker run -d --name my-postgres postgres

Give a name call 'my-postgres'

 

2. Link to Running Container By Name:

docker run -d -p 5000:5000 --link my-postgres:postgres danwahlin/aspnetcore

We want to link 'my-postgres' to another container 'danwahlin/aspnetcore'. And you need to make sure, 'danwahlin/aspnetcore' is running.

:postgres // is alias

 

 

Example how to link MongoDB container to Node.

We have a node.dockerfile:

FROM node:latest

MAINTAINER Dan Wahlin

ENV NODE_ENV=development
NEV PORT=3000

COPY ./var/www
WORKDIR /var/www

RUN npm install

EXPORT $PORT

ENTRYPOINT ["npm", "start"]

 

Build dockerfile:

docker build -f node.dockerfile -t danwahlin/node .

Here we point the file '-f node.dockerfile'

 

Currently we have `node`, `mongodb` and `danwahlin/node` images.
 
Start MongoDB container:
docker run -d --name my-mongodb mongo

 

Then start node container and link it to my-mongodb, give my-mongodb an alias as mongodb
docker run -d -p 3000:3000 --link my-mongodb:mongodb danwahlin/node

 

Start mongodb server in container:
docker exec danwahlin/node node dbSeeder.js
This will trigger `danwahlin/node` container and run `node dbSeeder.js` command.

 

转载于:https://www.cnblogs.com/Answer1215/p/10649171.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值