Using Node.js with Docker

I’ve looked Docker a few times, but I never had much of a need in my workflow. Now though, I’m more interested – we’ve got a few microservices that need to be running for our main application. They each are run using Node.js, and the “simple” method is to just open a new terminal window for each, starting them up manually. Of course, that’s something I forget to do until the main app’s tests show failures.

Docker Last week, after watching Scott Hanselman‘s Computer Stuff They Didn’t Teach You video #8: Containers? So What? Docker 101 Explained, I got to thinking about better ways to have those Node servers running.

The first thing I did was to put together a very simple Node Node.js logoapplication, using Express to serve static html files. To follow along, install Node.js, then open Powershell in Windows Terminal or your favorite console, and create a basic server app:

md node-docker
cd node-docker
node init
npm install express --save

This creates a simple Node application, which I then edited to serve files from a specific directory. In this case, I created a \content folder at the root of my drive and put a simple index.html file there.

var express = require('express');
var app = express();
var port = process.env.PORT || 5000;
var path = require('path');
var options = {
index: "index.html",
extensions: ['htm', 'html']

app.use('/', express.static('/content', options));

var server = app.listen(port, function () {
var host = server.address().address;
console.log('listening at http://%s:%s', host, port);

We can then start the little server using the command node server.js. Opening a browser to http://localhost:5000/ displays the index file from the content directory. That’s done via line 10, which basically maps the root directory of the webserver (‘/’) to the \content directory, and serves files from there. Control-C in the terminal will stop the server.

Next, install Docker. We’ll need to create a Dockerfile file containing the instructions on how to build an image.

FROM node:12
WORKDIR \temp\dockerwork
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node","server.js"]

Docker’s build command will use that to build an image:

docker build --tag nodedock .

You can use docker images to see a list of all the images you have on your system, at this point you should see at least your newly-build nodedock app and an image with Node version 12 itself.

docker's IMAGES command

If you were to start up that image now, the app would run but it would have no data – if you watched the video I mentioned above and look at the Dockerfile, you know that the image only contains the app – not the \content directory. I created it this way on purpose, because I want to have a server app running but I want to be able to create data (html files) outside the app. This means we need give our container (running image) access to our hard disk.

docker run -p 3000:5000 -v /c/content:/content --name nodedock --rm nodedock

This command
* runs our nodedock application,
* maps its port (-p) 5000 to docker’s port 3000,
* creates a volume (-v) that points to our c:\content directory and lets our Node app see it as /content
* and removes the container when we stop it running (--rm).

Docker Desktop

Now with that image running in our container, we can open a browser to http://localhost:3000/. We can open the Docker Desktop application to see all our containers, look at logs and other stats on each, and stop them when we don’t need them. If we don’t use the -rm flag, they’ll stick around.

Code behind this blog post can be found in this github repo.

When Chrome Auto-Updates

Using Selenium Webdriver or similar frameworks to “drive” a browser, usually for UI or end-to-end test automation, you may occasionally get this message:

session not created: This version of ChromeDriver only supports Chrome version 83 (SessionNotCreated)
Exception doesn't have a stacktrace

This means that, perhaps without you even realizing it, an update to Chrome has been installed on your computer. It’s time to also update to a new version of chromedriver.

1. use Chrome’s Help / About menu (chrome://settings/help) and look for your currently-installed version number.

2. search for [Chromedriver downloads] (and most likely be pointed to; download the version appropriate for your computer.

3. put it in the right place, replacing all existing copies of chromedriver.exe. Where exactly? Open a Powershell window and enter ls -path c:\ -include chromedriver.* -file -recurse -ErrorAction SilentlyContinue

Michael Vore

Michael “Mike” Vore, 80, of Columbia, Maryland, succumbed to cancer at Johns Hopkins Hospital on February 11, 2020. He is survived by his son Steven (Hilary), Grandsons James (Tory) and Robert (Brooke), two great-grandsons, his sisters Anita (Bob) and Kathie and brother Don (Kathy).

Mike was born in the Chicago area to Milton and Rita Vore, and the family moved to Catonsville Maryland shortly thereafter where he grew up. He married Una Yort, of Chevy Chase Maryland in 1963, and they had one son. Mike worked in several technical fields over the years. He enjoyed many activities and hobbies, including sailing, photography, flying sailplanes, woodworking, geocaching, and kayaking. Mike was active in his church community at the Shrine of St. Anthony.

Funeral service will be held at the Shrine of St. Anthony, Folly Quarter Road, Ellicott City Maryland, on Friday February 21st at 10am, and will be followed by a short reception. In lieu of flowers, donations may be made to the Shrine St. Anthony or to the American Cancer Society.

Atomic Habits, by James Clear – Book of the Month Jan ’19

Ah January — a time for setting goals and making resolutions to change. So many of us do it, yet by the end of the month so many of us have gone astray, reverting to our old ways. This month’s book is one to help us define and stick to our resolutions by forming new goals habits.

Yes, focusing on habits is much more likely to end in success than trying to focus on goals. As James Clear puts it, Goals are good for setting a direction, but systems are best for making progress. Every team intends to win the championship, but it’s the ones who put systems (habits) in place to make sure the work’s done right every day who are more likely to succeed.

Atomic Habits will change your understanding and approach to habits. It’s not only a good, solid description of how to to get those habits built (or torn down, in the case of exiting bad habits), but also the reasons why habits work or don’t, stick or don’t. It’s all about making small, sustainable changes in your daily life, intentionally creating an environment to encourage those changes, and making yourself accountable. While those may sound like easy things to do, actual change takes planning, and persistence — building the daily system, the end result of which is the change you desire.

Besides, February 1st — or whenever you read this — is just as good a day to start your journey to a new you as January 1st was. Get yourself going today; watch yourself turn tiny changes into remarkable results, with Atomic Habits.

The Calculating Stars, by Mary Robinette Kowal – Book of the Month Dec ’18

This time ’round, my Book Of The Month is actually two books: Mary Robinette Kowal‘s The Calculating Stars and The Fated Sky.

Dr. York, the protagonist here, is a smart, hard-working survivor of an immense environmental disaster. Along with a team of brilliant and talented — and often petty, sexist, and condescending — scientists, pilots, and managers, she does her part to save humanity from its eventual demise.

These novels – historical fiction about humanity’s first manned mission to Mars – are super-well written and delightful to read prequels for the short story “The Lady Astronaut of Mars.”

They also give us a good look behind the curtains of classism, racism, and sexism. They are, as Rick Klau put it, “a master-class in privilege: what it looks like when you have it, how it affects those who don’t, and how it can make progress harder.”

The perhaps unexpected exploration is that of anxiety, and how it can be debilitating for even the most “successful” and “got it all together” people. In my opinion, the most important paragraph of the two-book set is one that many readers may never get to — the very end of the final afterword.

Pause here for a moment, and re-read that.

The Calculating Stars — get your copy today, and enjoy the journey; I trust you’ll want share it with your friends and immediately follow it up with The Fated Sky.

Natural Born Heroes, by Christopher McDougall – Book of the Month Nov ’18

November’s #book recommendation, a true-life adventure, a detective story to uncover it, and more: “a story of remarkable athletic prowess: On the treacherous mountains of Crete, a motley band of World War II Resistance fighters—an artist, a shepherd, and a poet—abducted a German commander from the heart of the Axis occupation. To understand how, McDougall retraces their steps across the island that birthed Herakles and Odysseus, and discovers ancient techniques for endurance, sustenance, and natural movement that have been preserved in unique communities around the world.”

Having enjoyed McDougall’s Born To Run I was looking forward to his latest book, and it did not disappoint. He weaves together historical events — the mysterious kidnapping of a Nazi General and the subsequent hunting and evading — with a current-day search for clues to the whole story, and combines these with his own quest to understand just how it was done. How were these mostly-untrained, very unequipped men able to get themselves and their prisoner across the rocky, dangerous mountains of Crete? Journey with him as he uncovers history along with truths about the physical capabilities of the human body.

book cover: Natural Born Heroes
Christopher McDougall’s Natural Born Heroes.

grep, in PowerShell

Here is a sample of a PowerShell script that I use for finding text in files, having “grown up” with a more Unix-like syntax. I know, this isn’t exactly a clone of grep‘s functionality, but it gets me closer than having to remember exactly how to wrangle PowerShell’s Select-String commands to my liking. Note that I normally am looking for things recursively, so my script does that automatically.

ls -r $filename | sls $target

I call that by using an alias, set in my $PROFILE

Set-Alias grep c:\code\ps\Grep.ps1

Then I can just use a command like one of these

c:\> grep \code\*.ps1 version
c:\> grep *.txt hobbits
c:\> grep $HOME alpharetta

Also available, with any changes since this was published, in this repo.

Most Wanted, by Rae Carson

cover of the bookYay! Another new Star Wars book – this one in support of Solo: A Star Wars Story. That movie gave us some backstory to Han Solo; this novel gives us more and sheds light on the history between him and Qi’ra as well as (the first?) mention of an ancient Jedi manuscript, The Annals of Light and Being. Ignore the YA designation; Most Wanted is a fun story and adds even more depth to our favorite characters.

[more short book reviews]

Darktown, by Thomas Mullen

Atlanta Georgia, 1948 – quite a different city than we know today. Darktown is a historical novel telling the story of the first negro police officers here; men with what has always been a difficult job, made even tougher given the racial bias of the time.

Many pages were, like those of Colson Whitehead’s Underground Railroad, difficult and almost painful to read, but it’s important to face and learn from our history – a long and tough journey along the road toward democracy, freedom and equality for all.

I know, there are plenty of people who say “it’s over, it’s in the past – get over it.” They might have said the same thing about slavery in ’48, the year in which this book was set. But cultural history has multi-generational impact. We don’t think twice about a coworker who’s great-grandparents moved to the US from Italy and never looked back, working to speak with a bit of an accent and sharing his family recipes. Or those of us who do our best to never wear orange because of our great-grandfather‘s struggles. I have a friend who is vocal about his refusal to buy German- or Japanese-manufactured vehicles because his grandfather fought in WWII. Families and people-groups have history, and they’re all part of the fabric of our nation.

The characters, in particular some of the white offers, seem a bit over-the-top but that’s when reading from my current-day suburban mindset. When I put myself into the story, I find myself thinking that plenty of Dunlows likely existed; it’s Rakestraw and his internal struggle that portray the hope that the novel needs.

recommended: Darktown: A Novel by Thomas Mullen (Be prepared though; it’s set in a period in which racial epithets were thrown with impunity; you’ll see ‘the N word’ and other slurs used quite a bit, though never glorified or used lightly. It’s historical, be ready for it.)

[more short book reviews]