Giter VIP home page Giter VIP logo

docker-node-and-mongo's Introduction

Docker Node Demo

Based on Docker Node tutorial

This project shows how to use Docker Composer to build and run multiple containers that are dependent on each other. In this case we have a Node API that uses a MongoDB container to store and retrieve notes.

Important

Before you do any docker builds, make sure you have BuildKit enabled

Windows:
C:\ProgramData\Docker\config\daemon.json

Mac:
/etc/docker/daemon.json

{
  "features": { "buildkit": true }
}

De-alias Curl on Windows

The curl commands below will not work in Windows PowerShell as the curl command is aliased to Invoke-WebRequest

Either "de-alias" the curl command or run in a Unix shell such as Git Bash

Recommend using Git Bash has PowerShell doesn't seem to like the JSON in the curl commands used here.

The App

To run the API directly via Node use:
node server.js

Helpful Docker Tips

Remove Dangling images (created by Docker Compose):

docker rmi $(docker images -f "dangling=true" -q)

List Volumes and Networks

docker volume ls
docker network ls

Build and Run w/ Docker Commands

To build the Dockerfile and creat a Docker image:

docker build --tag mb/node-docker .

Running Docker Image

docker run --name jolly_wilbur --detach --publish 8000:8000 mb/node-docker

(name is optional)

# Clean up containers
docker ps
docker rm <container-name>

Test The Server API

Make a POST request:

curl --request POST --url http://localhost:8000/test --header 'content-type: application/json' --data '{"msg": "testing" }'

Make a GET request:

curl http://localhost:8000/test

Running With MongoDB

First: Setup MongoDB in Docker

Setup volumes

$ docker volume create mongodb
$ docker volume create mongodb_config

Setup network

docker network create mongodb

Run Mongo DB on network

$ docker run -it --rm -d -v mongodb:/data/db -v mongodb_config:/data/configdb -p 27017:27017 --network mongodb --name mongodb mongo

Then: Run App With a Network for MongoDB Connectivity

$ docker run -it --rm -d --network mongodb --name jolly_wilbur_server -p 8000:8000 -e CONNECTIONSTRING=mongodb://mongodb:27017/yoda_notes mb/node-docker

See below for test API calls for saving and retrieving notes in the database

Simplified With Docker Compose

See the Docker Compose section of the tutorial

$ docker-compose -f docker-compose.dev.yml up --build
( --build means build images before starting containers )

Compose To Run Tests

$ docker-compose -f docker-compose.dev.yml run notes-test npm run test

Compose To Run App

$ docker-compose -f docker-compose.dev.yml run notes npm run start

Testing The API

API Commands

Make a POST request:

curl --request POST --url http://localhost:8000/test --header 'content-type: application/json' --data '{"msg": "testing" }'

Make a GET request:

curl http://localhost:8000/test

To Test Server w/ DB

Get Notes

curl --request GET --url http://localhost:8000/notes

Endpoint for debug breakpoint

curl --request GET --url http://localhost:8000/foo

Save A Note

curl --request POST --url http://localhost:8000/notes --header 'content-type: application/json' --data '{ "name": "this is a note", "text": "this is a note that I wanted to take while I was working on writing a blog post.", "owner": "peter" }'

Expected Response:

{
  "code": "success",
  "payload": {
    "_id": "5efd0a1552cd422b59d4f994",
    "name": "this is a note",
    "text": "this is a note that I wanted to take while I was working on writing a blog post.",
    "owner": "peter",
    "createDate": "2020-07-01T22:11:33.256Z"
  }
}

Test Setup

$ docker build -t mb/node-docker --target test .

$ docker run -it --rm -p 8000:8000 mb/node-docker

docker-node-and-mongo's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.