ON SAME START
I hope that many of you heard about Docker or maybe tried to use it in several ways. This blog post will be for developers that already have a base knowledge of Docker and want to know more about using Docker with NodeJs and MongoDB. The main goal is to have the same docker environment for development and production.
ABOUT THE OPINION POLL API
By definition to be clear opinion polls are usually designed to represent the opinions of a population by conducting a series of questions and then extrapolating generalities in ratio or within confidence intervals and the person who conducts polls is referred to as a pollster.
So, from here we know something more about this domain now lest say a few words about the architecture.
- We will have an API that will be powered by NodeJS-Express-MongoDB-GraphQL and will be deployed on a cloud server with pre-build Docker containers.
- React opinion poll SPA that will communicate with the API and can be hosted elsewhere.
**note: Yeah I know, having this kind of architecture is overengineering but this app is for the learning process only.
**note: This Docker setup can be used with any kind of API/APP that uses Node.js and MongoDBÂ as a development stack.
DOCKERFILE AND DOCKER-COMPOSE.YML
Before anything else navigate to this git repo. There you can find the folder with the name docker.
There we have Dockerfile to build the API
We are using `node:alpine` image because is the smallest, working dir will be `/usr/src/app` then we will copy all files from local to the new build node image. We are setting PORT 5000 to be accessible in the image and as executable we serve then up with `npm run dev` for development and `npm run prod` for production. The last two commands are for testing proposes to build the image and to run the NodeJS API.
Now, we need to continue with the docker-compose.yml
Here we are connecting the MongoDB image with the pre-build docker image that was built on top of the node:alpine image. The inside and the outside ports for NodeJS are 5000 and for MongoDB 27017. We are using volumes to make the sync between the inside and outside the project environment so whenever we change any kind of file in the project that will be visible in the containers that are previously built from the docker images that we are using.
HOW TO USE THIS DOCKER SETUP?
I believe that every nice setup must have a README.md file that will hold on it.
FEW WORDS
I hope that this repo will help you and find your interests. However, the blog post will be a small guide that can explain better the main goal here. So, if you need any help you can contact me here or write me an email.
Take care.