What we will cover

Docker Utility Container

Introduction

In this article we will be using Docker to create a NodeJS utility container. So for this imagine you don't have NodeJS installed on your computer, but you want to use NodeJS for your next project well with NodeJS utility container you can work in NodeJS without having to install NodeJS on your host machine.

This seems very weird to you. Go and check out my YouTube video on this topic. So let's get started first let's look at a summary of Docker.

Summary of Docker

Docker is a set of platform-as-a-service (P.A.A.S) products that use OS-level virtualization to deliver software in packages called containers. The software that hosts the containers is called Docker Engine.\n\nFor this tutorial I expect you to already have Docker engine installed and setup.

What is a utility container

So in the context of Docker a utility container can be seen as a temporary environment that gets spined up - then a few commands get executed within this temporary environment and you can then bind the output of these commands to a folder directory on your host machine.

Let's us NodeJS as and example. If you don't have NodeJS installed on your host machine you won't be able to run or execute NodeJS programs or install any packages using NPM or Yarn.

But with the utility container we will be able to start a NodeJS project without having NodeJS installed on our host machine. And we will be able to use NPM or Yarn within this NodeJS utility container and bind the output of these package managers (usually node_modules folder or package.json) to our host machine directory where we are building our NodeJS project.

Create a utility container

So now that we understand some of the background of Docker and what a utility container is let us try and build our own NodeJS utility container with just a few command and a Dockerfile.

FROM node:18-alpine

WORKDIR /app

With the Dockerfile setup we now need to build a Docker image from this Dockerfile by running the following command in the directory of the Dockerfile.

docker build -t node-util .

Once the build is complete we now have a new Docker image called node-util. Now we want to use this Docker image to spin up a container, run yarn init that will generates a package.json and see this package.json mapped to our current working directory

docker run
--rm \
-it \
-v $(pwd):/app \
node-util \
yarn init -y

So lets break down what this command does.

docker run -- this the leading command to run any docker container.

--rm -- This flag will ensure that the container gets completely removed once it is shutdown.

-it -- This flag also interactive mode when running container. This will allow you to interact with the container terminal.

-v $(pwd):/app -- This flag is creating a volume bind mount to the specified directory.

node-util -- This is the image name we want this container to be based on.

yarn init -y -- This is yarn command to create package.json for a new project.

Conclusion

Now you know how to create a Docker utility container. This can be done for pretty much any other programming language or development run-time.

This technique of using utility containers are widely used in build step when doing CI/CD pipelines. We in a current step we would like to build the app and take the output files and send it of to some server to be downloaded. This can all be done with the help of Docker and utility containers.

Hope you found this article helpful if you want to learn more about Docker checkout my other articles on Docker.