Skip to content

Run Node.js CLI tools in Docker container

Published: | 2 min read

Table of contents

Open Table of contents

Intro

So you want to test a fancy Node.js CLI tool, but do not have Node.js installed, or do not want to install it locally? Docker to the rescue!

Prerequisites

You have Docker installed on your laptop. At the time of writing I have Docker Community Edition Version 17.03.0-ce-mac2 (15654).

A NPM installable Node.js CLI tool, you want to try. In this tutorial, I will explore https://github.com/santinic/how2.

Step one: Define and build your Docker image

The first thing to do, is to define a new Docker image, which will run our Node.js app. A simple Dockerfile will do. In your favourite text editor create a new file called Dockerfile and fill in the content:

FROM node:boron

RUN npm install -g how2

Save the file.

Back in the Terminal, in the directory you have placed the Dockerfile, built the image with:

$ docker build -t how2-node .

how2-node is the image name, I have chosen.

Step two: Run the container and test the CLI tool

After you have successfully built the image, it’s time to run the image in interactive mode, and enter the bash shell of the running container:

$ docker run --name myhow2 --rm -ti how2-node /bin/bash

Again, myhow2 is the name I have chosen for the container.

After running this command, you should be in the interactive shell of the container.

To test the CLI tool (how2 in this case), I would run something like:

$ how2 untar

… and enjoy the results. After you have finished, exit the shell with exit.

Conclusion

This was a minimal example, how you can easily try new Node.js tools without messing with your local environment.