DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How To Use the Node Docker Official Image
  • 7 Ways of Containerizing Your Node.js Application
  • Hardware-Accelerated OpenGL Rendering in a Linux Container
  • Getting Started With Windows Containers

Trending

  • Using Java Stream Gatherers To Improve Stateful Operations
  • Efficient API Communication With Spring WebClient
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Code Reviews: Building an AI-Powered GitHub Integration
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Develop Your Node.Js Docker Applications Faster

How to Develop Your Node.Js Docker Applications Faster

In this article, we’ll show a tutorial and example on how to use Docker’s host volumes and nodemon to write Node faster and radically reduce the time you spend testing.

By 
Ethan J Jackson user avatar
Ethan J Jackson
·
Jun. 16, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
12.6K Views

Join the DZone community and get the full member experience.

Join For Free

Docker has revolutionized how Node.js developers create and deploy applications. But developing a Node.js Docker application can be slow and clunky. The main culprit: the process for testing your code in development.

In this article, we’ll show a tutorial and example on how you can use Docker’s host volumes and nodemon to code faster and radically reduce the time you spend testing.


How Host Volumes and Nodemon Can Speed Up Your Node.js Development

One of the irritating things about testing during development with Docker is that whenever you change your code, you have to wait for the container to rebuild. With many Node.js applications, this can chew up a lot of time.

As a result, you end up with a development workflow that looks like this:

  • You make a change.
  • You wait for the container to rebuild.
  • You make another change.
  • You wait some more.

And if you have CI/CD and are continually running your code through automated tests? You’re going to be spending even more time waiting for the container to rebuild.

This process gets pretty tedious. And it’s hard to stay in the flow.

But there’s a way to change a container’s code without having to rebuild it. The trick is to use a Docker host volume.

Host volumes sync file changes between a local host folder and a container folder. If you use a host volume to mount the code you’re working on into a container, any edits you make to your code on your laptop will automatically appear in the container. And as you will see in the next section, you can use the nodemon package to automatically restart your application without having to rebuild the container – a technique known as “live reloading.”

The result: instead of having to spend lots of time waiting, your code-test-debug loop is almost instantaneous.


Example: Using Host Volumes and Nodemon In Node.Js Docker Development

The idea of using a host volume to speed up your Node.js coding might seem a little intimidating. But it’s pretty straightforward to do.

To demonstrate this, let’s use a Node.js example: Node-todo, a simple to-do application created by scotch.io. To clone the repo:

$git clone https://github.com/kelda/node-todo

The repo assumes you are using Docker Compose. You can also use Blimp, our alternative to Compose that runs in the cloud.

Here’s Node-todo’s docker-compose.yml:

version: '3'
services:
  web:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - mongo
    volumes:
      - "./app:/usr/src/app/app"
  mongo:
    image: "mongo"
    ports:
      - "27017:27017"

This file tells Docker to boot a container, the Node.js application, and a MongoDB database where the application stores the to-dos. It also tells Docker to mount a host volume:

volumes:
  - "./app:/usr/src/app/app" 

As a result, Docker will mount the ./app directory on your laptop, which contains your code, into the container at /usr/src/app/app.

Now, all you need to do is ensure that whenever you’ve edited your code, your Node.js application restarts so it’s using your latest code. That’s where nodemon comes in.

nodemon is a Node.js package that automatically restarts an application when it detects file changes in one or more specified directories. Once you’ve changed your code on your laptop/desktop, nodemon detects that change and restarts the process without rebuilding the container.

To make this happen, you need to tell Docker to set the entrypoint to nodemon instead of node.js. You do that in the Dockerfile:

FROM node:10-alpine
ENV PORT 8080
WORKDIR /usr/src/app
COPY . /usr/src/app

RUN npm install -g nodemon
RUN npm install

ENTRYPOINT ["nodemon", "/usr/src/app/server.js"]

In short, by using a host volume and nodemon, you can set up your Node.js application’s container so it automatically syncs code changes between the container and your laptop. If you didn’t do this, you’d have to rebuild the container every single time you made a change to your code.

Over time, this technique can substantially speed up your Node.js development. For example, we’ve heard from users that it’s not uncommon for container rebuilds to take 5-30 minutes. With host volumes and nodemon, your code sync is almost instantaneous. Imagine what your day would look like if you could save yourself 5-30 minutes every time you change and test your code.


Syncing Your Own Code When Developing a Node.js Application

Now that you’ve seen how it works in a sample application, let’s walk through how to enable code syncing in one of your existing Node.js projects.

Prerequisites

Just like the example above, before you get started, we recommend your Node.js project includes the following:

  • A git repo that contains your code
  • A Dockerfile that builds that code into a working container
  • A docker-compose.yml file you use to run that container

How to Configure Your Container to Automatically Sync Your Node.js Code

1) Locate the folder in your Docker container that has your code. The easiest way to figure out where your code is stored in your container is to look at your Dockerfile’s COPY commands. In the Node-todo example, its Dockerfile tells Docker to put the code in . /usr/src/app:

COPY . /usr/src/app

2) Find the path to the folder on your laptop that has the same Node.js code.

3) Add a host volume to your docker-compose file. Find the container in your docker-compose file that you want to sync code with, and add a volume instruction underneath that container:

volumes:
  "/path-to-laptop-folder:/path-to-container-folder"

4) Switch from using node.js to nodemon. In the Node-todo example, you implemented it via Dockerfile commands:

RUN npm install -g nodemon
RUN npm install

ENTRYPOINT ["nodemon", "/usr/src/app/server.js"]

As a result, Docker will install nodemon with npm install -g nodemon and change the entrypoint from node to nodemon.

5) Run Docker Compose or Blimp. Now all you need to do is either run docker-compose:

$ docker-compose up

Or if you’re using Blimp:

$ blimp up

Docker will overwrite the container’s code with the code that’s on your laptop.

Now that you’ve modified your project so it uses a host volume and nodemon, any changes you make to your Node.js code on your laptop will now automatically appear in the container.


Conclusion

Using host volumes to link your Node.js code on your laptop with your container can take a little getting used to. But it’ll make developing your Node.js Docker apps easier and faster.


Originally posted at: https://kelda.io/blog/develop-nodejs-docker-applications-faster/

Docker (software) Node.js application Host (Unix)

Published at DZone with permission of Ethan J Jackson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Use the Node Docker Official Image
  • 7 Ways of Containerizing Your Node.js Application
  • Hardware-Accelerated OpenGL Rendering in a Linux Container
  • Getting Started With Windows Containers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!