title: docker/nodejs/mongodb deployment : MongooseError [MongooseServerSelectionError]: connection timed out
body: <p><strong>update 2</strong></p>
  
  <p>I face another error,
  if I use path as follows, I will get "invalid volume specification" error
  even change path to "C:/docker-entrypoint-initdb.d/", the init-mongo.sh will not be executed, then I get "UserNotFound: Could not find user "xxxx" for db "admin"</p>
  
  <pre><code>    volumes:
        - ./init-mongo/:/docker-entrypoint-initdb.d/
        - dbdata:/data/db
  </code></pre>
  
  <p>so
  I switch to linux container, and all error message are gone.</p>
  
  <p><strong>update</strong></p>
  
  <p>I've solved my problem after disable mongodb service on local host</p>
  
  <pre><code>net stop MongoDB
  </code></pre>
  
  <p>and use port 27017</p>
  
  <pre><code>mongodb:
  ....
      ports:
        - "27017:27017"
  .....
  </code></pre>
  
  <p>but I would like to know why I cant change port 27017 to others?
  I change the port because if mongodb is running on my computer (using 27017 port), I will get the following error message, thank you.</p>
  
  <pre><code>for mongodb  Cannot start service mongodb: failed to create endpoint mongodb on network server_app_network: hnsCall failed in Win32: The process cannot access the file because it is being used by another process. (0x20)
  </code></pre>
  
  <p><strong>original</strong></p>
  
  <p>I'm using docker windows container on windows 10.
  After struggling for two days, I still cant deploy my project successfully.
  my configuration is as follows, thanks for help ..</p>
  
  <p>server.js</p>
  
  <pre><code>const dbUrl = "mongodb://mongodb:27018/";
  mongoose
    .connect(`${dbUrl}mydb`, { useUnifiedTopology: true, useNewUrlParser: true })
    .then(res =&gt; {
      db = mongoose.connection;
      app.listen(port, () =&gt; {
        console.log(`listening on port ${port}`);
      });
    })
    .catch(error =&gt; console.log(error));
  </code></pre>
  
  <p>docker-compose.yml</p>
  
  <pre><code>version: "3"
  services:
    express-server:
      build:
        context: .
        dockerfile: Dockerfile
      image: express-server
      container_name: express-server
      restart: unless-stopped
      ports:
        - "3000:3000"
      links:
        - mongodb
      depends_on: 
        - mongodb
      networks:
        - app_network
  
    mongodb:
      image: mongo
      container_name: mongodb
      restart: unless-stopped
      ports:
        - "27018:27018"
      networks:
        - app_network
  volumes:
    dbdata:
  networks:
    app_network:
      driver: nat
  
  </code></pre>
  
  <p>after running docker-compose up --build -d</p>
  
  <pre><code>CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                 NAMES
  7fac2380c803        express-server      "node server.js"         57 seconds ago       Up 43 seconds       0.0.0.0:3000-&gt;3000/tcp                express-server
  9afc21569a1a        mongo               "mongod --bind_ip_all"   About a minute ago   Up 57 seconds       27017/tcp, 0.0.0.0:27018-&gt;27018/tcp   mongodb
  
  </code></pre>