在弄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... 4194 [OK] mongo-express Web-based MongoDB admin interface, written... 225 [OK] tutum/mongodb MongoDB Docker image – listens in port 270... 218 [OK] mvertes/alpine-mongo light MongoDB container 68 [OK] mongoclient/mongoclient Official docker image for Mongoclient, fea... 46 [OK] bitnami/mongodb Bitnami MongoDB Docker Image 30 [OK] frodenas/mongodb A Docker Image for MongoDB 17 [OK] mongooseim/mongooseim Small docker image for MongooseIM - robust... 15 mongooseim/mongooseim-docker MongooseIM server the latest stable version 10 [OK] centos/mongodb-26-centos7 MongoDB NoSQL database server 5 istepanov/mongodump Docker image with mongodump running as a c... 5 [OK] centos/mongodb-32-centos7 MongoDB NoSQL database server 4 eses/mongodb_exporter mongodb exporter for prometheus 4 [OK] khezen/mongo MongoDB Docker image supporting RocksDB st... 4 [OK] cvallance/mongo-k8s-sidecar Kubernetes side car to setup and maintain ... 3 [OK] ekesken/mongo docker image for mongo that is configurabl... 1 [OK] 19hz/mongo-container Mongodb replicaset for coreos 1 [OK] alino/coreos-mongodb-cluster mongo 3.0 1 [OK] extremeprog/mongo-sites-api mongo-sites-api automated build 1 [OK] openshift/mongodb-24-centos7 DEPRECATED: A Centos7 based MongoDB v2.4 i... 1 circleci/mongo MongoDB document databases provide high av... 1 os33/go-mongo go and mongo package for testing 0 quilt/mongo MongoDB container for quilt.io 0 [OK] gebele/mongodb mongodb 0 [OK] targetprocess/mongodb_exporter MongoDB exporter for prometheus 0 [OK]
2、拉取官方的镜像
docker pull mongo
3、运行容器
mkdir -p ~/mongo/db
cd ~/mongo
docker run -p 27018:27017 -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