Sitemap
Krishna Prasad Varma

Krishna Prasad Varma

Passionate writer and avid learner sharing insights on programming and data science with a focus on ruby, python and node.js.

Using a Utility Docker Image for creating a new Node.js API

3 min readApr 5, 2023

--

Press enter or click to view image in full size

What are utility docker containers?

Docker utility containers are containers that are used to provide specific utility functions, rather than running applications or services. These containers can be used to perform a variety of tasks, such as monitoring, logging, debugging, and more.

Some examples of docker utility containers include:

  1. Logspout — a utility container that collects logs from all containers running on a Docker host and forwards them to a log aggregator.
  2. Sysdig — a container that provides system-level monitoring and troubleshooting capabilities for containers and hosts.
  3. Nginx Proxy — a container that provides reverse proxy and load balancing capabilities for web services.
  4. Consul — a container that provides service discovery and configuration management capabilities for distributed systems.
  5. Docker Registry — a container that provides a centralized location for storing and distributing Docker images.

Overall, Docker utility containers can be incredibly useful for simplifying the management and operation of containerized environments. By using these containers, users can easily add new functionality to their Docker environments without having to manually install and configure additional software.

Why use a docker utility container to create a new node.js application?

Using a utility container to create a Node.js application has several benefits, including:

  1. Simplified development: By using a utility container, developers can easily set up a development environment that includes all the necessary dependencies and tools needed to build and test their applications. This can help to reduce the time and effort required to get up and running with a new Node.js project.
  2. Consistent environment: Docker utility containers provide a consistent environment for development and deployment, which can help to reduce the likelihood of errors and issues that can arise from differences in software versions and configurations across different environments.
  3. Isolation: Docker containers provide isolation for the Node.js application, which helps to prevent conflicts with other applications running on the same host machine. This can be especially useful when running multiple applications on the same server.

Prerequisite

You’ll want to make sure you have the following components installed on your machine:

Lets Dive In

Clone the utility-docker-images repository. This is a one-time task. Once this is set up, you could use the same utility container for creating multiple node.js applications.

$ git clone git@github.com:kpvarma/utility-docker-images.git
$ cd utility-docker-images

Build the docker image for node_utility

$ docker-compose build node_utility

Starting the Node Utility

Get Krishna Prasad Varma’s stories in your inbox

Join Medium for free to get updates from this writer.

Start the node_utility service in a container

$ docker-compose run — rm node_utility init

Replace the name new_app or new_api with what you like.

You will now see the node project files created in the node/new_app folder

You could now install any npm you need. whatever you type after node_utility is passed as the argument to the npm command running inside the docker utility container. Check the CMD command in Dockerfile to learn how this is done.

# for e.g to install Express.js, do like below:
$ docker-compose run --rm node_utility install express

# Install multiple libraries
$ docker-compose run --rm node_utility install mongoose nodemon dotenv

Alternatively, you could edit the node/new_app/package.json and add your desired dependencies and do an npm install in one go.

$ docker-compose run — rm node_utility install

Add Dockerfile and docker-compose.yml

touch node/new_app/Dockerfile
touch node/new_app/docker-compose.yml

Copy the content below to the newly created Dockerfile

FROM node

# Update and install required dependencies
RUN apt-get update && apt-get install -y build-essential && apt-get clean

# Create a directory for the application
RUN mkdir /app
WORKDIR /app

COPY package.json /app
RUN npm install

# Expose a port
EXPOSE 8002

Copy the content below to the newly created Dockerfile

version: '3.9'
services:
nodejs:
build:
context: ./nodejs/image
image: new_node_app:latest
container_name: new_node_app
command: ["/bin/bash"]
tty: true
stdin_open: true
restart: always
env_file:
- ./nodejs/image/nodejs.env
volumes:
- ./nodejs/lessons:/app/lessons/
volumes:
nodejs_lessons:

You can now move the node/new_app folder outside of this folder where ever you wish and keep developing it.

$ mv node/new_app ~/Projects/2023/new_project_name

--

--

Krishna Prasad Varma
Krishna Prasad Varma

Passionate writer and avid learner sharing insights on programming and data science with a focus on ruby, python and node.js.