English 中文(简体)
Mongodb的连接没有成功,而只靠 do器。
原标题:Mongodb connection failed while running with docker-compose

这是我的服务器。 js file:

let path = require( path );
let fs = require( fs );
let MongoClient = require( mongodb ).MongoClient;
let bodyParser = require( body-parser );
let app = express();

app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(bodyParser.json());

app.get( / , function (req, res) {
    res.sendFile(path.join(__dirname, "index.html"));
  });

  
app.get( /profile-picture , function (req, res) {
  let img = fs.readFileSync(path.join(__dirname, "images/profile-1.jpg"));
  res.writeHead(200, { Content-Type :  image/jpg  });
  res.end(img,  binary );
});
let mongoUrlLocal = "mongodb://admin:password@localhost:27017";
let mongoUrlDocker = "mongodb://admin:password@mongodb";
let mongoClientOptions = { useNewUrlParser: true, useUnifiedTopology: true };
let databaseName = "user-account";

app.post( /update-profile , function (req, res) {
  let userObj = req.body;
    MongoClient.connect( mongodb://localhost:8080 , function (err, client) {
    if (err) throw err;

    let db = client.db(databaseName);
    userObj[ userid ] = 1;

    let myquery = { userid: 1 };
    let newvalues = { $set: userObj };

    db.collection("users").updateOne(myquery, newvalues, {upsert: true}, function(err, res) {
      if (err) throw err;
      client.close();
    });

  });
  // Send response
  res.send(userObj);
});

app.get( /get-profile , function (req, res) {
  let response = {};
  // Connect to the db
    MongoClient.connect( mongodb://admin:password@localhost:8080 , function (err, client) {
    if (err) throw err;

    let db = client.db(databaseName);

    let myquery = { userid: 1 };

    db.collection("users").findOne(myquery, function (err, result) {
      if (err) throw err;
      response = result;
      client.close();

      // Send response
      res.send(response ? response : {});
    });
  });
});

app.listen(3000, function () {
  console.log("app listening on port 3000!");
});

这是我的cker。 垃圾处理

version:  3 
services:
  # my-app:
  # image: ${docker-registry}/my-app:1.0
  # ports:
  # - 3000:3000
  mongodb:
    image: mongo
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    volumes:
      - mongo-data:/data/db
  mongo-express:
    image: mongo-express
    restart: always # fixes MongoNetworkError when mongodb is not ready when mongo-express starts
    ports:
      - 8080:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
  mongo-data:
    driver: local

这是Dockerfile:


ENV MONGO_DB_USERNAME=admin 
    MONGO_DB_PWD=password

RUN mkdir -p /home/app

COPY ./app /home/app
WORKDIR /home/app
RUN npm install
CMD ["node", "server.js"]

当我操作docker-compose -f docker-compose.yml up,发现这一错误:

ms","Start up the replication coordinator":"6 ms","Start transport layer":"2 ms","_initAndListen total elapsed time":"909 ms"}}}}        
mongo-express-1  | /docker-entrypoint.sh: line 15: mongo: Try again
mongo-express-1  | /docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
mongo-express-1  | Wed Apr  3 01:27:38 UTC 2024 retrying to connect to mongo:27017 (2/10)
mongo-express-1  | /docker-entrypoint.sh: line 15: mongo: Try again
mongo-express-1  | /docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
mongo-express-1  | Wed Apr  3 01:27:44 UTC 2024 retrying to connect to mongo:27017 (3/10)
mongo-express-1  | /docker-entrypoint.sh: line 15: mongo: Try again
mongo-express-1  | /docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
mongo-express-1  | Wed Apr  3 01:27:50 UTC 2024 retrying to connect to mongo:27017 (4/10)
mongo-express-1  | /docker-entrypoint.sh: line 15: mongo: Try again
mongo-express-1  | /docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
mongo-express-1  | Wed Apr  3 01:27:56 UTC 2024 retrying to connect to mongo:27017 (5/10)
mongo-express-1  | /docker-entrypoint.sh: line 15: mongo: Name does not resolve
mongo-express-1  | /docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
mongo-express-1  | Wed Apr  3 01:28:00 UTC 2024 retrying to connect to mongo:27017 (6/10)
mongo-express-1  | /docker-entrypoint.sh: line 15: mongo: Name does not resolve
mongo-express-1  | /docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
mongo-express-1  | Wed Apr  3 01:28:05 UTC 2024 retrying to connect to mongo:27017 (7/10)
mongo-express-1  | /docker-entrypoint.sh: line 15: mongo: Try again
mongo-express-1  | /docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
mongo-express-1  | Wed Apr  3 01:28:11 UTC 2024 retrying to connect to mongo:27017 (8/10)
mongo-express-1  | /docker-entrypoint.sh: line 15: mongo: Try again
mongo-express-1  | /docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
mongo-express-1  | Wed Apr  3 01:28:17 UTC 2024 retrying to connect to mongo:27017 (9/10)
mongo-express-1  | /docker-entrypoint.sh: line 15: mongo: Try again
mongo-express-1  | /docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
mongo-express-1  | Wed Apr  3 01:28:23 UTC 2024 retrying to connect to mongo:27017 (10/10)
mongo-express-1  | /docker-entrypoint.sh: line 15: mongo: Try again
mongo-express-1  | /docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
mongo-express-1  | No custom config.js found, loading config.default.js
mongo-express-1  | Welcome to mongo-express 1.0.2

And unable to run this app at port 3000 with node server.js It goes into app listening at port 3000 but on the browser it s all empty. Reference for this: https://www.youtube.com/watch?v=3c-iBn73dDE&t=7223s

问题回答

基本上,<代码>mongo-express 多克形象是 b。

<代码>mongo-express> 自行使用ME_CONFIG_MONGODB_SERVER。 如果是(并且将更喜欢ME_CONFIG_MONGODB_URL)的条目编号(/docker-enterpoint.sh)只有使用的ME_CONF_MONDODB_URL。 载有

if [[ "$ME_CONFIG_MONGODB_URL" != *,* ]]; then
    work=$ME_CONFIG_MONGODB_URL
    # Remove the scheme (should be "mongodb://" or "mongodb+srv://").
    work=${work#*://}
    # Remove the path component of the URL (should just be a "/").
    work=${work%%/*}
    # Remove the userinfo.
    work=${work#*@}
    if [[ "$work" = *:* ]]; then
        # Match the host.
        host=${work%:*}
        # Match the port.
        port=${work#*:}
    else
        host=$work
        port=27017
    fi

    # wait for the mongo server to be available
    echo "Waiting for $host:$port..."
    wait_tcp_port "$host" "$port" "${ME_CONFIG_CONNECT_RETRIES:-10}"
fi

图像还设定了ME_CONFIG_MONGODB_URL>mongodb://mongo:27017的违约值。 由于你重新设置了<代码>ME_CONFIG_MONGODB_SERVER,但not<>>>>>> 代码>ME_CONFIG_MONGODB_URL,因此,根据这一缺省值,切入点等待与服务器代码>/code>连接。

我们可以确定这一点,为<代码>提供适当的价值。 ME_CONFIG_MONGODB_URL:

services:
  mongodb:
    image: mongo
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: password
    volumes:
      - mongo-data:/data/db
  mongo-express:
    image: mongo-express
    restart: always # fixes MongoNetworkError when mongodb is not ready when mongo-express starts
    ports:
      - 8080:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: admin
      ME_CONFIG_MONGODB_ADMINPASSWORD: password
      ME_CONFIG_MONGODB_SERVER: mongodb
      ME_CONFIG_MONGODB_URL: mongodb://mongodb:27017
volumes:
  mongo-data:

上述堆肥文件成功地建立了<代码>mongo-express。

你也可以通过重新命名<代码>mongodb服务 to mongo,从而解决这个问题,这样就能够与违约行为保持一致。





相关问题
Access DB Ref MongoDB

Whats the best way to access/query a DB Ref: UPDATE: users: name, groupref : {$ref:"groups",$id:"ObjectId ..." } } groups: name, topic, country,...,.. Assumption is that user belongs to only one ...

MongoDB nested sets

What re the best practices to store nested sets (like trees of comments) in MongoDB? I mean, every comment can have a parent comment and children-comments (answers). Storing them like this: { ...

MongoMapper and migrations

I m building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model: class SomeModel include MongoMapper::Document ...

MongoDB takes long for indexing

I have the following setup: Mac Pro with 2 GB of RAM (yes, not that much) MongoDB 1.1.3 64-bit 8 million entries in a single collection index for one field (integer) wanted Calling .ensureIndex(...) ...

Storing and accessing large amounts of data

My application creates pieces of data that, in xml, would look like this: <resource url="someurl"> <term> <name>somename</name> <frequency>somenumber</...