How to create a Node App within a Docker container with Mongo

Search for a command to run...

No comments yet. Be the first to comment.
Introduction This is the third part of the series for Docker meets NodeJS. We got to add a mongo database as a service and maybe you added a couple of endpoints to test it out, but the whole app is in runs locally and you might want to let the whole...
Recently I started building tools that will help me automate my app deployment process easier and cheaper, to do that I knew CLIs are definitely going to be involved one way or another. So I started building two CLIs: Simp CLI: It's like Makefile ju...

Let's jump into it... fast. You got here because you want to setup Dokku and the other ways don't work... let's goooo I will setup for Dokku only Point your domain to the VPS of choice Spin up new VPS server here: Digital Ocean Linode Buy a dom...

So what is Dokku, another bit, another TL;DR; Let's jump into it. You might know Heroku, (my opinion) the greatest platform to ever make it easy to deploy server and client-side applications so fast and easy. Well Heroku is a business and they have t...

Okay for this one I highly recommend, the speeds is just amazing. I loved it. I am gonna do a comparison real quick, between browser-image-compression and react-image-file-resizer. browser-image-compressionreact-image-file-resizer Really fastCo...

I recently got interested in compressing images on the client-side for a couple of reasons: Reduce my cloudinary bandwidth and storage usage Users were complaining about long waits while uploading images So I ventured to look for a library that wil...

This is the second part of the series for Docker meets NodeJS where we are going to introduce a database service, MongoDB, which we will connect to it with our Node app through a network.
Note: Since we are introducing a new database service, by doing so this also makes our Node app also a service
Note: Ensure you go through the first part of this series since it contains a huge part of how we created our Node app within a Docker container.
Note: Remember to clone the
docker_nodejs_apprepo here
On the first part of the series we were able to:
We should be able to:
Services basically are a group of containers, they make it easier to scale your application.
In order to connect to our Mongo database with Node, we need to install a new dependency called Mongoose.
$ npm install mongoose --save
Let's connect our Node App to a database that does not exist by adding a couple of lines in our app.js file.
// Connect to database
mongoose.connect("mongodb://mongo:27017/docker_nodejs_app", {
useNewUrlParser: true,
useCreateIndex: true
});
mongoose.connection.on("open", err => {
if (err) console.log(chalk.red("Error connecting to our mongo database"));
console.log(chalk.green("Connected to mongo database successfully"));
});
Note: If you have used mongoose and mongo before, your local or mlab connection URL might look like this
mongodb://localhost:27017/<your_db>ormongodb://<dbuser>:<dbpassword>@ds115595.mlab.com:15595/<your_db>respectively and notmongodb://mongo:27017. This is because our mongo database service will be calledmongoand since it exists in a docker container and not locally on your drive we will expose the port 27017 in its container.
Your app.js file should look like this
"use strict"; // Ensures our code is compiled in strict mode
// Lets import our web framework
var express = require("express");
var mongoose = require("mongoose");
// Initialise our app
const app = express();
// Lets set our port
/**
* The default port number is `3000`
* Take note on that as we will come to that.
*/
app.set("port", 3000);
// Connect to database
mongoose.connect("mongodb://mongo:27017/docker_nodejs_app", {
useNewUrlParser: true,
useCreateIndex: true
});
mongoose.connection.on("open", err => {
if (err) console.log("Error connecting to our mongo database");
console.log("Connected to mongo database successfully");
});
/**
* To ensure works as it should we will create a
* simple endpoint to return a json response
*/
// Define our json response
const data = {
blog_name: "docker_nodejs_app",
blog_author: "wachira (tesh254)",
blog_author_twitter: "@wachira_dev"
};
// Define out GET request endpoint
app.get("/", (req, res) => {
res.status(200).json(data);
});
// Initialize our server
app.listen(app.get("port"), () => {
console.log(`Server listening on port ${app.get("port")}`);
});
If we run our Node app we should get an error

In order to build and run a couple of services, we need to introduce a docker-compose.yml file that contains certain configurations that will allow that.
$ touch docker-compose.yml
Type this in the YAML file
# Defines our composer file version
version: "2.2"
# Define our services
services:
# This is our nodejs app built using the Dockerfile
app:
# The name of our node app container
container_name: docker_nodejs_app
# Restarts our node app whenever it fails
restart: always
# Builds the node app docker container from the local -
# Docker file we created
build: .
# Exposes the necessary ports that our node app uses
ports:
- "3000:3000"
# All the services our node app will link to -
# which in our case is only mongo
# You can other services such as a Redis
links:
# The name should be similar to the service you will build
- mongo
# Our database service called mongo
mongo:
# The name of the database container, NOTE: it is similar to the name provided
container_name: mongo
# Builds a mongo image from the docker repository
image: mongo
# Exposes the ports that Mongo uses
ports:
- "27017:27017"
To run our configurations we will use a
docker-composecommand...wait where did this come from? 😳
The command docker-compose comes with the Docker software, it executes the configurations in the docker-compose.yml file.
To build and run our app and mongo services, type this in your terminal
$ docker-compose up
What happens next will take a bit longer...😇...Free time...maybe make yourself a cup of coffee ☕️ or check out Twitter
Downloads Mongo into Dockers cache

Run instances of mongo



You can test out the database by creating a couple of
To summarise, in this post we have:
docker-compose commandIn the next part:
Link to repo https://github.com/werickblog/docker_nodejs_app
Link to download NodeJS https://nodejs.org/en/download/
Link to download Docker https://www.docker.com/get-started
Understanding what docker is https://docs.docker.com/engine/docker-overview/