Bobcares

Dockerizing a Node.js web app | How-to guide

by | Nov 27, 2022

If you plan to dockerize a Node.js web app, you are in the right place. Our in-house experts have put together this step-by-step guide to help you out. Our Docker Support team is here to offer a lending hand with your queries and issues.

Dockerizing a Node.js web app

In order to dockerize a Node.js web app, we will first create a simple web app. After that, we have to build a Docker image for that specific application. Finally, we have to instantiate a container from this image. In other words:

  1. Create a Node.js web app
  2. Create Dockerfile
  3. Build an image
  4. Run the image
  5. Shut down the image

Create a Node.js web app

  1. Creating a new Node.js web app involves creating a new directory initially.
  2. Then create a package.json file in this directory. This file will describe the app as well as its dependencies.
  3. Then run npm install command with the new file we created in the previous step.
  4. Next, create a server.js file to define a web app via the Express.js framework.

Create Dockerfile

  1. First, create a new file and name it Dockerfile.
  2. Then, open this file in a text editor and define from which image we want to build from.
  3. After that, we have to create a directory. This directory will hold the application code within the image.
  4. Next, we will install the app dependencies via the npm binary.
  5. Then, use the COPY instruction to bundle the app’s source code within the Docker image.
  6. Additionally, create a .dockerignore file within the same directory as the Dockerfile. Add this content to the files:

    node_modules
    npm-debug.log

Build an image

The next step in dockerizing a Node.js web app is building an image. We can easily build an image by heading to the directory with the Dockerfile and running this command:

docker build . -t <your username>/node-web-app

Run the image

If we run the image with -d, the container runs in detached mood. This will leave the container running in the background. Furthermore, the -p flag is responsible for redirecting public port to private port within the container.

docker run -p 49160:8080 -d /node-web-app

Then, we can print the output of the app as seen below:


# Get container ID
$ docker ps
# Print app output
$ docker logs <container id>

Additionally, we can use the exec command to go inside the container.

Shut down the image

When it is time to shut down the app, we have to run the kill command with the container id as seen below:

$ docker kill <container_id>

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

In conclusion, our Support Engineers took us through the process of dockerizing a Node.js web app. We got a look at the different steps involved as well.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.