简体   繁体   中英

docker-compose only running my mongodb server but not running my js file

I'm new to using docker. I'm developing a simple form that submits data inputted by a user into a mongodb database and redirects him/her to a thank you html page. It's working perfectly on my localhost but when I dockerize it, it doesn't work. Thing is, my code works perfectly on Mac but not on Windows OS. Any help will be appreciated.

Here's my js code:

 var express = require('express'); var app = express(); var fs = require('fs'); var bodyParser = require('body-parser'); var urlencodedParser = bodyParser.urlencoded({ extended: false }); var MongoClient = require('mongodb').MongoClient; app.use(express.static(__dirname + '/../public')); app.get('/', (req,res)=> file = fs.createReadStream(__dirname + '/clientlogin.html').pipe(res)); app.get('/thanks', (req,res)=> file = fs.createReadStream(__dirname + '/formthanks.html').pipe(res)); app.post('/', urlencodedParser, function(req,res){ var firstname = req.body.firstname; var lastname = req.body.lastname; var email = req.body.email; var phonenumber = req.body.phonenumber; var pass = req.body.password; // Use connect method to connect to the server var url = "mongodb://mongo:27017/clientdb"; MongoClient.connect(url, function(err, db) { if (err) throw err; var obj = { 'FirstName': firstname, 'LastName': lastname, 'Email': email, 'PhoneNumber': phonenumber, 'Pass': pass }; db.collection("myclients").insertOne(obj, function(err, res) { if (err) throw err; console.log("1 client registered!"); db.close(); }); res.redirect('/thanks'); }); }); app.listen(8081, function(){ console.log("Listening on port 8081"); }); 

My package.json code

 { "name": "loginpage", "version": "1.0.0", "description": "login page", "main": "signupmongo.js", "scripts": { "start": "node signupmongo.js" }, "author": "David", "license": "ISC", "dependencies": { "body-parser": "^1.18.2", "express": "^4.16.2", "fs": "0.0.1-security", "mongodb": "^2.2.33", "mongoose": "^4.13.4" } } 

My dockerfile

 FROM node:argon WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . EXPOSE 8081 CMD ["npm", "start"] 

My docker-compose.yml file

 version: "3" services: web: build: . volumes: - ./:/app ports: - "8081:8081" depends_on: - mongo mongo: image: mongo ports: - "27017:27017" 

Here's the command line interface where the code appears to stop running. enter image description here

You have to link the mongo container in your app container. Use the links feature for this Example:

version: "3"
services:
  web:
    build: .
    volumes:
      - ./:/app
    ports:
      - "8081:8081"
    links:
      - mongo
    depends_on:
      - mongo
  mongo:
    image: mongo
    ports:
      - "27017:27017"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM