DEV Community

Cover image for Docker-compose an express and mongo App

Docker-compose an express and mongo App

Jamal Al on September 17, 2018

Docker is a Tool that enables you to containerize your dev environment and by so helps you be more productive to focuses more on the code and stop ...
Collapse
 
tommazo profile image
Tom Mazo

Exactly what I was looking for, thanks!

Maybe I missed something, but I think you forgot to mention that you need to fetch the items from the DB to display them in the index route.

Something like that:

app.get("/", (req, res) => {
  Item.find((err, items) => {
    if (err) throw err;
    res.render("index", { items });
  });
});
Collapse
 
jay97 profile image
Jamal Al

Glad it helped and you're absolutely correct. I'll fix it as soon as I get time

Collapse
 
jonnysmith1981 profile image
Jonathan Smith

Depending on your platform (I'm using CentOS), setting the ports property in the mongo section of the docker-compose.yml file will also open port 27017 on your firewall, which you may not want to do.

Collapse
 
jay97 profile image
Jamal Al • Edited

hey Enes K. Thanks for the nice comment.

make a file package.json in your project, copy/past bellow json object
and run npm install to get all the dependences.

{
    "name": "docker-demo",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts":
        "start": "nodemon index.js"
    },
    "keywords": [],
    "author": "Jamal",
    "license": "ISC",
    "dependencies": {
        "express": "^4.16.4",
        "mongoose": "^5.4.19",
        "ejs":"^"
    },
    "devDependencies": {
        "nodemon": "^1.18.10",

    }
}

Collapse
 
junibrosas profile image
Juni Brosas

in my case I used 3000:3000 in my docker-compose yaml port.

Collapse
 
junibrosas profile image
Juni Brosas • Edited

Also, the data storage did not persist. I added extra setup in the yaml file.

 mongo:
    image: mongo:4.2.8
    ports:
      - 27017:27017
    volumes:
      - mongodb:/data/db
      - mongodb_config:/data/configdb

volumes:
  mongodb:
  mongodb_config:
Enter fullscreen mode Exit fullscreen mode
Collapse
 
wmramadan profile image
Wael Ramadan

Nice article, this might be worth a look github.com/WMRamadan/docker-compos...