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.
Join the DZone community and get the full member experience.
Join For FreeDocker 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/
Published at DZone with permission of Ethan J Jackson. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments