Instructions to set up our Node app with our MongoDB database:

  1. Firstly, create a Dockerfile with the following parameters. This file copies and installs our dependencies into our container. It also exposes port 3000 and runs app.js:

App Dockerfile

  1. Next, create a Dockerfile for your DB file. This file installs the dependencies for MongoDB:

DB DOckerfile

  1. Next we need to write our Docker Compose file. The below file essentially links our App and DB microservices together.It also sets our DB_HOST environment variable:

Compose File

  1. Once we have created these 3 files and placed them in the appropriate folders, we need to cd into our microservices directory and run the following command:

docker-compose build

This builds both our App and DB containers.

  1. Lastly, we need to execute the following command to run the containers:

docker-compose up

  1. To check we have got the database working, navigate to the following address in your browser:

localhost:3000/posts

Posts should now be showing!