docker node项目 连接mongodb

时间:2021-07-01 07:58:52
在弄docker部署node项目的时候遇到了连接mongdb的问题,记录一下问题解决办法

一、Docker 安装 MongoDB

1.查找Docker Hub上的mongo镜像

[root@VM_49_93_centos mongo]# docker search mongo
NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mongo MongoDB document databases provide high av... [OK]
mongo-express Web-based MongoDB admin interface, written... [OK]
tutum/mongodb MongoDB Docker image – listens in port ... [OK]
mvertes/alpine-mongo light MongoDB container [OK]
mongoclient/mongoclient Official docker image for Mongoclient, fea... [OK]
bitnami/mongodb Bitnami MongoDB Docker Image [OK]
frodenas/mongodb A Docker Image for MongoDB [OK]
mongooseim/mongooseim Small docker image for MongooseIM - robust...
mongooseim/mongooseim-docker MongooseIM server the latest stable version [OK]
centos/mongodb--centos7 MongoDB NoSQL database server
istepanov/mongodump Docker image with mongodump running as a c... [OK]
centos/mongodb--centos7 MongoDB NoSQL database server
eses/mongodb_exporter mongodb exporter for prometheus [OK]
khezen/mongo MongoDB Docker image supporting RocksDB st... [OK]
cvallance/mongo-k8s-sidecar Kubernetes side car to setup and maintain ... [OK]
ekesken/mongo docker image for mongo that is configurabl... [OK]
19hz/mongo-container Mongodb replicaset for coreos [OK]
alino/coreos-mongodb-cluster mongo 3.0 [OK]
extremeprog/mongo-sites-api mongo-sites-api automated build [OK]
openshift/mongodb--centos7 DEPRECATED: A Centos7 based MongoDB v2. i...
circleci/mongo MongoDB document databases provide high av...
os33/go-mongo go and mongo package for testing
quilt/mongo MongoDB container for quilt.io [OK]
gebele/mongodb mongodb [OK]
targetprocess/mongodb_exporter MongoDB exporter for prometheus [OK]

2、拉取官方的镜像

docker pull mongo

3、运行容器

mkdir -p ~/mongo/db
cd ~/mongo
docker run -p : -v $PWD/db:/data/db -d --name mongodb mongo:latest

4、操作mongodb

docker exec -it ‘mongo CONTAINER ID’ bash
cd /bin
mongo

5、修改项目中连接mongodb (把mongodb连接地址localhost:27017更改为运行自己项目docker镜像时 --link指定另一个容器)

DB_URL = 'mongodb://admin:sunjianping@localhost:27017/changxiang';
DB_URL = 'mongodb://admin:sunjianping@mongodb/changxiang';

6、运行自己的node项目(myp 为自己项目的docker镜像 )

docker run -i -t --name myp --link=mongodb:mongodb myp:1.2 /bin/bash