Node.js with MongoDB and Docker Demo
Application demo designed to show how Node.js and MongoDB can be run in Docker containers. The app uses Mongoose to create a simple database that stores Docker commands and examples.
Interested in learning more about Docker? Visit https://www.pluralsight.com/courses/docker-web-development to view my Docker for Web Developers course.
Starting the Application with Docker Containers:
-
Install Docker for Windows or Docker for Mac (If you're on Windows 7 install Docker Toolbox: http://docker.com/toolbox).
-
Open a command prompt.
-
Run the commands listed in
node.dockerfile
(see the comments at the top of the file). -
Navigate to http://localhost:3000. Use http://192.168.99.100:8080 in your browser to view the site if using Docker toolbox. This assumes that's the IP assigned to VirtualBox - change if needed.
Starting the Application with Docker Compose
-
Install Docker for Windows or Docker for Mac (If you're on Windows 7 install Docker Toolbox: http://docker.com/toolbox).
-
Open a command prompt at the root of the application's folder.
-
Run
docker-compose build
-
Run
docker-compose up
-
Open another command prompt and run
docker ps -a
and note the ID of the Node container -
Run
docker exec -it <nodeContainerID> sh
(replace with the proper ID) to sh into the container -
Run
node dbSeeder.js
to seed the MongoDB database -
Type
exit
to leave the sh session -
Navigate to http://localhost:3000 (http://192.168.99.100:3000 if using Docker Toolbox) in your browser to view the site. This assumes that's the IP assigned to VirtualBox - change if needed.
-
Run
docker-compose down
to stop the containers and remove them.
To run the app with Node.js and MongoDB (without Docker):
-
Install and start MongoDB (https://docs.mongodb.com/manual/administration/install-community/).
-
Install the LTS version of Node.js (http://nodejs.org).
-
Open
config/config.development.json
and adjust the host name to your MongoDB server name (localhost
normally works if you're running locally). -
Run
npm install
. -
Run
node dbSeeder.js
to get the sample data loaded into MongoDB. Exit the command prompt. -
Run
npm start
to start the server. -
Navigate to http://localhost:3000 in your browser.