ulysset / nodejs-predixmachine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Node JS containers inside a Predix Machine Docker environment

The goal is to create a two NodeJS containers that will talk to each other via MQTT. The Pxm MQTT brocker is running as a container also.

Requirements

This has only been tested in a DevBox environment but should also work on any other PxM compatible OS You'll need to dowload multiple docker images that you can find in PREDIX-EXT/predix-machine-package/docker-images:

  • PredixMachine bootstrap docker image - predixmachine-bootstrap-alpine--.tar.gz.
  • Predix Machine agent docker image - predixmachine-agent--.tar.gz.
  • Mosquitto Messaging Broker docker image - predixmachine-mosquitto--.tar.gz

Classic PxM-Docker environment Installation

  1. Load the bootstrap image into Docker's cache. docker load -i predixmachine-bootstrap-alpine--.tar.gz
  2. Create a directory to mount to the bootstrap container.
  3. Create and start the bootstrap container. Containers will start in host mode. bash start_bootstrap.sh -d
  4. Copy the docker image archives for Predix Machine and Mosquitto tar.gz to the directory monitored by bootstrap. Bootstrap will load the images and start containers. The process may take a little while, including time for the polling interval, time to load images and start containers.
  5. Verify that the images were loaded and the containers started - docker images docker ps Right now you should have the bootstrap container, the MQTT container running and the Predix_Machine container running. For more information go to the predixmachinesdkdirectory/utilities/containers/bootstrap

Modifying the config files for MQTT communication

All those files are located inside the predixmachinesdkdirectory/configuration/machine Open all the MQTT config files and modify the connection url to tcp://localhost:1883, the mqtt brocker to tcp://localhost:1883 again
In the docs they say that you should use the inet ip instead of localhost but it didn't work for me so I used the localhost config and it works.

Creating and running NodeJS containers

  1. git clone this reposiory
  2. Build the PUB/SUB image
docker build -t ulysset/nodejs-container-pub '/path/to/nodejs-container-pub'
# or
docker build -t ulysset/nodejs-container-sub '/path/to/nodejs-container-sub'
  1. Save it and gzip it
  docker save ulysset/nodejs-container-pub | gzip -c > /path/to/nodejs-container-pub/ulysset-nodejs-container-pub.tar.gz
  # or
  docker save ulysset/nodejs-container-sub | gzip -c > /path/to/nodejs-container-pub/ulysset-nodejs-container-sub.tar.gz
  1. Copy the created image inside of the bootstrap-mount directory
  cp /path/to/nodejs-container-pub/ulysset-nodejs-container-pub.tar.gz '/path/to/bootstrap-mount-directory'
  # or
  cp /path/to/nodejs-container-sub/ulysset-nodejs-container-sub.tar.gz '/path/to/bootstrap-mount-directory'
  1. Wait
  2. Restart the Predix Machine
docker restart Predix_Machine
  1. Look if your nodeJS containers are running
 docker ps
  1. Open two terminal windows and look at the logs of your nodejs-container-pub or nodejs-container-sub logs
 docker logs name-of-your-app -f

Optional: The problem is that if we make a modification to our NodeJS code the changer won't update juste by restarting the container. So you can create a bash function to accelerate the process. Modify and copy this at the end of you .bashrc file located at the user root e.g:

restartPub() {
    docker build -t ulysset/nodejs-container-pub '/path/to/nodejs-container-pub'
    docker save ulysset/nodejs-container-pub | gzip -c > /path/to/nodejs-container-pub/ulysset-nodejs-container-pub.tar.gz
    cp /path/to/nodejs-container-pub/ulysset-nodejs-container-pub.tar.gz '/path/to/bootstrap-mount-directory'
    sleep 30
    docker restart Predix_Machine
}

restartSub() {
    docker build -t ulysset/nodejs-container-sub '/path/to/nodejs-container-sub'
    docker save ulysset/nodejs-container-sub | gzip -c > /path/to/nodejs-container-sub/ulysset-nodejs-container-sub.tar.gz
    cp /path/to/nodejs-container-sub/ulysset-nodejs-container-sub.tar.gz '/path/to/bootstrap-mount-directory'
    sleep 30
    docker restart Predix_Machine
}

About


Languages

Language:JavaScript 100.0%