• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Abhishek Tanwar

I build web & mobile experiences

  • HOME
  • BLOG
  • CONTACT ME
  • DISCLAIMER & COPYRIGHTS
You are here: Home / Building APIs / How to setup an express and mongo app with docker

How to setup an express and mongo app with docker

June 19, 2018 by Abhishek Tanwar

Hello folks. I was trying to figure out how to setup express and mongodb with container and depended services. There were lot of tutorials but there were missing things here and there. I had to go through multiple sites to get a simple and short app running.

So, here is a post describing how to set it up!

I am assuming you have docker, docker-compose setup already done. I am using VSCode as editor to do all my stuff which is way easier now with the new releases that Microsoft has done. I highly recommend to switch to VSCode.

Now, I want to create a simple express app using a mongo connection with no business logic for now. Hence my package.json and index.js is very simple.

db.js:

Plain and simple with just DB URL. the catch here is the “mongo” service name. Docker containers will use internal DNS to resolve this to the the mongo container and do the magic.

db.js

index.js

This is a simple main application file for express. Nothing fancy here. Just listens to DB and puts out a json for the homepage.

index.js

package.json

Nothing here just my dependencies for express and mongo.

package.json
package.json

On the vscode project, we can automate the generation of docker files via plugin but lets skip that for now and stick to basics. We have to create 3 files – Dockerfile, docker-compose.yml and .dockerignore.

Dockerfile

This is a simple docker file for express application stating docker to pull the node version and copy the code to server and open 3000 port to outside because that is the port we run the application.

Dockerfile
Dockerfile

.dockerignore

This is also pretty standard which tells docker-compose to ignore few things like gitignore.

.dockerignore
.dockerignore

docker-compose.yml

Now, this is the file where we have to look for the magic.

docker-compose.yml
docker-compose.yml

Few things to look for:

  • this sets up the main application which points to the root directory with “Dockerfile” and exposing container 3000 port to outside port
  • Main application depends on mongo service which we have defined next. This just tells docker to define the order in which the containers should be started. Hence it will start “mongo” before “express-app”. [Note: there is an issue here that I will discuss down the post]
  • “mongo” service name is used. This is same as the db.js file reference to the DNS. This is how docker will connect two containers.
  • “adminmongo” is just an application to browse mongo databases via web. I highly recommend to use it.

That’s it we can now build and run the application.

Below command to build and run

  • docker-compose build
  • docker-compose up -d

Now you will notice that your express-app sometime don’t connect to Mongo. This is because docker only controls the start order of services not the ready state. Hence once docker has issued a start command to mongo, it will issue start of express and express app will throw error for mongo not being up.

Now, to resolve this there are 2 ways:

  1. Just restart the express-app container using command “docker restart <CONTAINER_ID>
  2. Do some scripting in “command” section of express-app to either poll for mongo to be up or wait for predefined time to start the app.

In my view, for development, it is ok to restart the container only but best approach for production grade applications is to have tests and scripts to ensure that all dependencies are up!

I would expect docker to have a check for ready state as well may be via some plugin. Please comment if you know of any!

Drop a like or share and happy coding!

Share this:

  • Facebook
  • X

Like this:

Like Loading...

Filed Under: Building APIs, Fullstack Tagged With: docker, docker express mongo, express mongo docker, how to dockerize express

Primary Sidebar

  • Behance
  • Email
  • Facebook
  • GitHub
  • LinkedIn

Popular Posts

How To Integrate React in JSP Application
SaaS, PaaS, IaaS
XQuery highlighting in Notepad++
Standard Design Patterns – Applied to JAVA
XQDT Installation on Eclipse JUNO

Categories

  • Building APIs
  • Building Mobile Apps
  • Building Web Apps
  • Coding Challenges
  • Extensions
  • Fullstack
  • Patterns
  • Tips & Tricks
  • Uncategorized
  • VSCode

Tags

2022 vscode extensions Abstract Factory alienware amazing vscode extensions apple challenges coding coding-challenges Design Patterns dota2 dota2 items dynamic web service call dynamic webservice invocation enterprise integration express flex helpful vscode extensions how to implement mfa in nodejs increasing productivity using vscode integration pattern macro message channel message endpoint message pattern message router message translator messaging mfa offline action processing offline actions in struts pipes and filters PRG in struts react Steam struts System Emulator threads in struts utilities vscode vscode customization vscode extensions vscode productivity extensions xquery xquery 1.0 xquery design principles

Recent Posts

  • 15+ VSCode Extensions To Improve Your Productivity
  • Productivity and Quality Extensions for ReactJS in VSCode..
  • My goto VSCode plugins for 2022…
  • How to enable MFA for your application in Node.JS?
  • Structuring Express Application – How I do it?

Categories

  • Building APIs (8)
  • Building Mobile Apps (1)
  • Building Web Apps (7)
  • Coding Challenges (3)
  • Extensions (1)
  • Fullstack (6)
  • Patterns (8)
  • Tips & Tricks (41)
  • Uncategorized (43)
  • VSCode (2)

Tags

2022 vscode extensions Abstract Factory alienware amazing vscode extensions apple challenges coding coding-challenges Design Patterns dota2 dota2 items dynamic web service call dynamic webservice invocation enterprise integration express flex helpful vscode extensions how to implement mfa in nodejs increasing productivity using vscode integration pattern macro message channel message endpoint message pattern message router message translator messaging mfa offline action processing offline actions in struts pipes and filters PRG in struts react Steam struts System Emulator threads in struts utilities vscode vscode customization vscode extensions vscode productivity extensions xquery xquery 1.0 xquery design principles

Archives

  • August 2023 (1)
  • March 2022 (1)
  • February 2022 (1)
  • November 2020 (1)
  • April 2020 (2)
  • May 2019 (1)
  • April 2019 (1)
  • March 2019 (1)
  • December 2018 (1)
  • July 2018 (4)
  • June 2018 (3)
  • March 2018 (1)
  • May 2016 (1)
  • December 2015 (1)
  • May 2015 (1)
  • April 2015 (2)
  • March 2015 (1)
  • December 2014 (2)
  • November 2014 (2)
  • October 2014 (1)
  • August 2014 (2)
  • July 2014 (1)
  • April 2014 (1)
  • March 2014 (1)
  • February 2014 (2)
  • January 2014 (1)
  • December 2013 (4)
  • November 2013 (4)
  • October 2013 (3)
  • September 2013 (4)
  • August 2013 (2)
  • July 2013 (5)
  • June 2013 (9)
  • May 2013 (6)
  • April 2013 (2)
  • March 2013 (2)
  • January 2013 (1)
  • November 2012 (2)
  • HOME
  • BLOG
  • CONTACT ME
  • DISCLAIMER & COPYRIGHTS

Copyright © 2025

%d