[Docker] Linking Node.js and MongoDB Containers

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.

 

posted @   Zhentiw  阅读(223)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2018-04-03 [GraphQL] Fetch Server Data and Client-side State in One Query using React Apollo + GraphQL
2018-04-03 [Angular] Getting to Know the @Attribute Decorator in Angular
2016-04-03 [Angular 2] Async Http
点击右上角即可分享
微信分享提示