添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
https://www.liuya.love/article?name=docker%E5%AE%B9%E5%99%A8%E9%97%B4%E7%9A%84%E9%80%9A%E4%BF%A1%E3%80%90%E8%BF%9E%E6%8E%A5MongoDB%E6%95%B0%E6%8D%AE%E5%BA%93%E3%80%91&id=62ea2149ff3382e9054dbdf6
参考了这位大神的博客
  • 问题:若mongodb容器已运行的情况下,还可以再在代码中连接吗?如何做呢?

  • 背景:三个容器环境,分别是 node ,mongodb,mongodb-express

  • 需求 :mongo容器已启动成功,而代码中需要再次连接mongoDB以操作数据库

  • 工具库:mogoose

const mongoose = require('mongoose')
// 为什么这里是@mongodb?? 请重点思考
const URI="mongodb://admin:admin@mongodb:27017/"
mongoose.connect(URI , { useNewUrlParser : true, useUnifiedTopology : true})
mongoose.connection.on("connected",function(){
  console.log(URI+':数据库连接成功!')
   启动容器,输出连接成功 
 
myexpress-node-web-1  | mongodb://admin:admin@mongodb:27017/:数据库连接成功!
首先了解一下容器各自的IP情况,再用name或者ip来连接mongoDB的容器
docker network inspect mongodb_network
PS I:\Frontend> docker network inspect mongodb_network
        "Name": "mongodb_network",
        "Id": "029276b94ec4bb67e63a45aaad79b96f6f5ddf031cffb7d7039f428ddb9d87c0",
        "Created": "2023-01-27T20:08:38.045814Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        "ConfigOnly": false,
        "Containers": {
            "0a0472e20bf61637744f4b09bff44211b25fde9dcb34dbb42ec291717868aa0b": {
                "Name": "mongo-express",
                "EndpointID": "1129e83db10d15ce31aa138a02eed885e2db9dfc0d1dfbef462ca40f98e08e47",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            "f76f3d70730f4400b71fc60d126d1e053276c9153daf97b93e995e72c1b99524": {
                "Name": "mongodb",
                "EndpointID": "24988fa13701d7a64491a220337335d7f2ca2e9134eb1076f6e1f48b661cb3c6",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "myexpress",
            "com.docker.compose.version": "2.13.0"
   即使mongo容器已运行并正常访问,也可以再用代码连接到mongo容器,不会有影响 
 

附赠docker-compse,可参考

version: '3.0'
services:
  node-web: 
    build: ./
    ports:
      - "3000:3000"
      - "3001:3001"
    volumes:
      - ./:/usr/src/app
      - /usr/src/app/node_modules/
    command:  npm start 
  mongodb:
    image: mongo
    container_name: mongodb
    ports:
      - 27017:27017
    expose:
      - 27017
    volumes:
      - ./database:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin
  mongo-express:
    image: mongo-express
    container_name: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=admin
      - ME_CONFIG_MONGODB_SERVER=mongodb
    depends_on:
      - mongodb
networks:
  default:
    name: mongodb_network
sudo tee /etc/docker/daemon.json <<-'EOF'
  "registry-mirrors": ["https://7fsjirqs.mirror.aliyuncs.com"]
sudo systemctl daemon-reload
sudo systemctl restart docker
验证Docker的镜像加速是否生效
[root@
				
docker使用mongo 为什么需要这个? (Why you need this?) 因为JS + Python + Mongo =完整的数据开发 (Because JS + Python + Mongo = full data development) I am a Meteor developer. More precisely I use Vulcan.js, but that’s a ...
根据 freecodecamp ZhichengChen写的文章的学习整理(https://chinese.freecodecamp.org/news/the-docker-handbook/) Docker简介 容器化可以将软件及其所有依赖项打包在一个自包含的软件包,这样就可以省略麻烦的配置,直接运行。 Docker核心概念:镜像是一个只读的模板,类似于安装系统时用到的ISO文件,通过镜像来完成各种应用的部署。容器可以被启动、开始、停止、删除等,每个容器间都是相互隔离的。仓库是一个存放镜像的场所,仓库
1.1 Docker 概念 Docker 于 2013 年开源,其基于 go 语言开发,是一个开源的 PaaS 服务(Platform as a Service,平台即服务的缩写)。而 go 语言由 google 开发,docker 公司最早期 叫 dotCloud。Docker 开源后广受欢迎,于是公司改名为 Docker Inc,总部位于美国 加州旧金山,Docker ... 解决:consumer: Cannot connect to amqp://admin:**@127.0.0.1:5672//: [Errno 111] Connection refused.