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.
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
- Load the bootstrap image into Docker's cache. docker load -i predixmachine-bootstrap-alpine--.tar.gz
- Create a directory to mount to the bootstrap container.
- Create and start the bootstrap container. Containers will start in host mode. bash start_bootstrap.sh -d
- 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.
- 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
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.
- git clone this reposiory
- 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'
- 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
- 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'
- Wait
- Restart the Predix Machine
docker restart Predix_Machine
- Look if your nodeJS containers are running
docker ps
- 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
}