Docker
can work around the "But it works on my machine!.." syndrome.
Let's say you have a
nodejs
project you want to share with others.
The application reads GPS data through a Serial port, and feeds a
WebSocket
server.
The data can then be visualized through a Web interface.
To enable everything, you need to:
- Have a Raspberry PI
- Flash its SD card and connect it to a network
- Install build tools
- Install
git
- Install
NodeJS
andnpm
- Clone the right
git
repository - Install all the required
node
modules - Drill down into the right directory
- Start the
node
server with the right script - Access the Raspberry PI from another machine on the same network, and reach the right HTML page.
This is certainly not difficult, but there are many ways to do several mistakes at each step of the process!
Docker
can take care of the steps 3
to 9
.
It will build the image, and then run it.
The image can also be pushed to a repository, so users would not have to build it.
Just to run it after downloading it.
The only pre-requisite would be to have installed
Docker
on the machine (the Raspberry PI here),
as explained here.
Create a
Dockerfile
like this (available here):
FROM resin/raspberrypi3-debian:latest LABEL maintainer="Olivier LeDiouris <olivier@lediouris.net>" RUN echo "alias ll='ls -lisah'" >> $HOME/.bashrc RUN apt-get update RUN apt-get install sysvbanner RUN apt-get install -y curl git build-essential RUN curl -sL https://deb.nodesource.com/setup_9.x | bash - RUN apt-get install -y nodejs RUN echo "banner Node-PI" >> $HOME/.bashrc RUN echo "git --version" >> $HOME/.bashrc RUN echo "echo -n 'node:' && node -v" >> $HOME/.bashrc RUN echo "echo -n 'npm:' && npm -v" >> $HOME/.bashrc RUN mkdir /workdir WORKDIR /workdir RUN git clone https://github.com/OlivierLD/node.pi.git WORKDIR /workdir/node.pi RUN npm install EXPOSE 9876 CMD ["npm", "start"]
In this case, the full
Docker
image creation (named oliv-nodepi
below) comes down to 1 line (the one in bold red):
$ docker build -t oliv-nodepi . Sending build context to Docker daemon 752.6kB Step 1/20 : FROM resin/raspberrypi3-debian:latest ---> c542b8f7a388 Step 2/20 : MAINTAINER Olivier LeDiouris---> Using cache ---> b2ff0d7c489f Step 3/20 : ADD nodepi.banner.sh / ---> 535733298dd1 Step 4/20 : RUN echo "alias ll='ls -lisah'" >> $HOME/.bashrc ---> Running in 09baf7261a55 Removing intermediate container 09baf7261a55 ---> 71e1e4c95663 Step 5/20 : RUN apt-get update ---> Running in 5d817a941a14 Get:1 http://security.debian.org jessie/updates InRelease [94.4 kB] Get:2 http://archive.raspbian.org jessie InRelease [14.9 kB] Get:3 http://archive.raspberrypi.org jessie InRelease [22.9 kB] ... npm notice created a lockfile as package-lock.json. You should commit this file. added 166 packages in 81.166s Removing intermediate container 13986530db28 ---> 051eb94b8a3c Step 19/20 : EXPOSE 9876 ---> Running in 67b587845fe0 Removing intermediate container 67b587845fe0 ---> 46973b7ba9ac Step 20/20 : CMD ["npm", "start"] ---> Running in 153bf2ea02ad Removing intermediate container 153bf2ea02ad ---> 6bf3d76d38ae Successfully built 6bf3d76d38ae Successfully tagged oliv-nodepi:latest ed9a7d9042dddd3939b1788cf0e89d16f5273192a6456266507f072f90ce91bc $
Once the step above is completed, plug in your GPS, and run
$ docker run -p 9876:9876 -t -i --privileged -v /dev/ttyUSB0:/dev/ttyUSB0 -d oliv-nodepi:latestThen from a machine seeing the Raspberry PI on its network (it can be the Raspberry PI itself), reach http://raspi:9876/data/demos/gps.demo.wc.html in a browser.
This shows you the position the GPS has computed, and the satellites in sight.
You can also login to the image:
$ docker run -it oliv-nodepi:latest /bin/bash # # ###### ### ## # #### ##### ###### # # # # # # # # # # # # # # # # # # # # # ##### ##### ###### # # # # # # # # # # # # ## # # # # # # # # # #### ##### ###### # ### git version 2.1.4 node:v9.11.1 npm:5.6.0 root@b9679d0d65a7:/workdir/node.pi#... and do whatever you like.
The build operation needs to be done once.
There is no need to do it again as long as no change in the image is required.
- Quick comment
-
So, with
Docker
, you do not deliver a software, you actually deliver an image (a virtual machine), on which a software is running.
This is indeed redefining the concept of portability that made Java and other JVM-aware languages so successful.
This may very well explain the rise of languages like Golang (aka Go).
It runs on my machine? Well, here is my machine! You can download and run it. Enjoy!
No comments:
Post a Comment