Today I learnt how easy it is to wrap your CLI's apps in docker containers to make running them or onboarding people to your application a breeze.
Let's say you have a super sweet CLI app that you built or support but your not happy with the installation or onboarding process for new devs and you would like a simpler alternative to get people up and running with your CLI faster. If this is you docker might be a nice addition to your tooling to get you up and running faster and reliably.
For the dockerfile examples below I will be using the mhulse/node-boilerplate-cli repo and dockerizing the CLI. I'll also assume you already have docker installed, but if not check out the docs here to get you started.
The first thing we will want to do is select the base image our CLI will be installed on. Using docker hub we search for a compatible nodeJs image for our CLI. We'll go ahead and select the official nodeJs image and for this example we will just use the
latest tag for which version of the image to fetch. You can find all the available tags of the node images by checking out the supported tags section of the nodeJS docker hub page
In our repo we will want to create a file called
Dockerfile. And using the
FROM command we will tell docker the base image we want to run the rest of our commands on.
# Base image of the docker container FROM node:latest
Next with the
COPY command we will copy the contents of our repo into the docker container. The
WORKDIR command is useful for changing the current working path of the proceeding commands to save you from having lots of hard to read paths in your
Dockerfile. Using the
RUN command you'll want to follow your CLI's installation steps.
# Copy the contents of the repo into the /app folder inside the container COPY . /app # Update the current working directory to the /app folder WORKDIR /app # Add your CLI's installation setups here RUN npm install RUN npm link
And finally you will want to provide the path to your CLI's executable with the
ENTRYPOINT command. This command means that every time your container is run the entry point executable is run.
Now we should be done and our final
Dockerfile should look something like this.
# Base image of the docker container FROM node:latest # Copy the contents of the repo into the /app folder inside the container COPY . /app # Update the current working directory to the /app folder WORKDIR /app # Add your CLI's installation setups here using the RUN command RUN npm install RUN npm link # Provdide a path to your cli apps executable ENTRYPOINT ["/usr/local/bin/boilerplate"]
Now that your docker file is done we can build and run our image.
# Run the commands in our Dockerfile and create a container # -t can be used to set name:tag of the built image $ docker build -t your-cli-app . Sending build context to Docker daemon 21.5MB ... Successfully built 3fdbb4987594 Successfully tagged your-cli-app:latest # Run the latest `your-cli-app` container and pass it arguments like normal $ docker run your-cli-app --version 1.0.0 $ docker run your-cli-app --help Usage: boilerplate -d [/path/to/directory/] Options: --version Show version number [boolean] --foo, -f Create foo text files? [boolean] --baz, -b What will this option do? [boolean] --other, -o Some other option? [number] [default: 3] --directory, -d A directory! [string] [required] -h, --help Show help. [boolean]
And that's it, Your CLI can now be installed and run from a container with just 2 commands making it easy to integrate into your build process and/or CI/CD.
If you're interested in publishing your docker containers for others to consume without having to build the container themselves check out the docs on publishing images here. And if your interested to dive into Docker containers a bit more a good place to start is the dockerfile best practices docs.