NodeJS + MongoDB App handling all CRUD operations with Docker Deployment
Clone the repository
Run npm install
to install all dependencies stored in package.json
Run npm start
- Connection base URL :
http://localhost:3000/api/
- MongoDB Connection URL :
mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false
Pull mongo image by running : docker pull mongo:latest
While running 1st time, docker containers have to be built.
So run docker-compose up --build
- Connection base URL :
http://localhost:5000/api/
- MongoDB Connection URL :
mongodb://localhost:27019/?readPreference=primary&appname=MongoDB%20Compass&ssl=false
After building containers, to run the program, use :
docker-compose up
When docker is not used, then the app is running locally on default ports specified in .env
file which are :
PORT | Usage |
---|---|
3000 | Running Node Express Server |
27017 | Running MongoDB Database |
So the connection strings are :
$HOST:3000
(currentlylocalhost:3000
)mongodb://localhost:27017/
When docker is enabled, we require 2 diff containers to run at the same time - one running the MongoDB database and the other running Node Express Server
The info for the Server container is stored in Dockerfile
where details related to starting of server and installing npm
scripts are stored.
The info for mongo
container and environment variables for server is stored in docker-compose.yml
. Here we have specified that port 5000
of container be mapped to port 8080
of machine at run time.
The port details are :
PORT | Usage |
---|---|
8000 | Specified in Dockerfile to Inform docker that container is listening at specified port at runtime |
8080 | Machine's port 8080 used to bind to container |
5000 | Container's port mapped to specified machine port(currently 8080 ) |
27017 | Machine's port runnning MongoDB |
27019 | Docker Container's MongoDB port mapped to local machine's MongoDB port(currently 27017 ) |
So the connection strings are :
$HOST:5000
(currentlylocalhost:5000
)mongodb://database:27017/
(Heredatabse
is name of container running MongoDB)
Connection String : mongodb://localhost:27019/?readPreference=primary&appname=MongoDB%20Compass&ssl=false
Here, 27019
is the port on which Mongo container is running
- Remove composed mongo container(container name : database) :
docker-compose rm database
https://medium.com/faun/managing-mongodb-on-docker-with-docker-compose-26bf8a0bbae3