How to Deploy Basic Node.Js Application to Ubuntu with Docker

Node.js is a popular JavaScript runtime environment that allows developers to build fast, scalable, and efficient server-side applications. Docker, on the other hand, is a containerization platform that enables developers to package their applications into containers for deployment. In this article, we will explore how to deploy Node.js application with Docker to Ubuntu, a popular Linux distribution.

Previously I shared an article on how to deploy the NestJs application to Ubuntu Instance using Docker.

Preparing the Environment

Before we begin, make sure that you have the following installed on your Ubuntu machine:

  1. Docker
  2. Node.js
  3. npm (Node Package Manager) [this comes default when you install Node.Js]

Setup a basic Node.Js application

If you already have a Node.Js application ready then you can skip this step.

Generate an ExpressJs framework-based Node.Js application with express-generator by the following command:

Install node modules and run the application to see if your basic Node.Js application is correctly set up.

Head to the browser on http://localhost:3000 to open the running node.js application.

Basic Node.Js Application generated by Express Generator
Basic Node.Js Application generated by Express Generator

Creating a Dockerfile to deploy basic Node.Js application

Now that you have your basic Node.Js application ready, let’s start creating a Dockerfile that will help deploy the Node.Js application to the Ubuntu instance.

A Dockerfile is a script that contains instructions for building a Docker image. It is a text document that contains a series of instructions that tell Docker how to build an image. The instructions are executed in order, and each instruction adds a layer to the image.

To create a Dockerfile for your basic Node.Js application, follow these steps:

  1. Create a new file named Dockerfile in your project directory.
  2. Add the following lines to your Dockerfile:

Save and close your Dockerfile.

Building a Docker Image

Now that you have created a Dockerfile, you can build a Docker image using the following command:

Replace my-node-app with the name of your Docker image. The . (dot) at the end specifies the current directory as the build context.

Docker Build Result Node.Js Application
Docker Build Result Node.Js Application

Running a Docker Container

Once you have built your Docker image, you can run a Docker container using the following command:

Replace my-node-app with the name of your Docker image. The -p option maps port 3000 of your container to port 3000 of your Ubuntu machine.

Docker Run Node.Js Image
Docker Run Node.Js Image

Visit the running Node.Js application in the browser

Now that your basic Node.Js is running in docker on port 3000, you can open it in the browser.

Node.Js Basic App Deployed and Running Using Docker
Node.Js Basic App Deployed and Running Using Docker

Conclusion

In this article, we covered how to deploy a basic Node.js application with Docker to Ubuntu. We started by preparing the environment and installing Docker, Node.js, and npm. Then, we created a Dockerfile that contained instructions for building a Docker image. We built the Docker image and ran a Docker container.

Using Docker to deploy Node.js applications offers a number of benefits, including increased portability, scalability, and efficiency. With Docker, you can easily package your application and its dependencies into a single container, making it easy to deploy and run on any machine that supports Docker.

Frequently Asked Questions

Here are some frequently asked questions on deploying a basic Node.Js application to an Ubuntu instance with Docker.

How do I stop a Docker container?

You can stop a Docker container by running the following command:

Replace <container-id> with the ID of your Docker container. You can get the container id from the example referenced below.

How do I remove a Docker container?

You can remove a Docker container by running the following command:

Replace <container-id> with the ID of your Docker container.

How do I remove a Docker image?

You can remove a Docker image by running the following command:

Replace <image-name> with the name of your Docker image.

How do I get the Docker container id?

You can run the following command to get all the containers in Docker.

It will list out all the containers including running and non-running.

Docker Containers
Docker Containers

Why use Docker to deploy Node.js applications?

Docker provides a number of benefits for deploying Node.js applications, including increased portability, scalability, and efficiency. With Docker, you can easily package your application and its dependencies into a single container, making it easy to deploy and run on any machine that supports Docker.

How do I install Docker on Ubuntu?

You can install Docker on Ubuntu by following the official documentation. This typically involves adding the Docker repository to your system, updating the package list, and installing the Docker engine.

How do I create a Dockerfile for my Node.js application?

To create a Dockerfile for your Node.js application, you will need to specify a base image, set the working directory, copy your application files, install dependencies, expose ports, and define the startup command. See the example Dockerfile in the article for a starting point.

How do I build a Docker image for my Node.js application?

To build a Docker image for your Node.js application, you will need to run the docker build command and specify the name and tag for your image, as well as the build context.

How do I run a Docker container for my Node.js application?

To run a Docker container for your Node.js application, you will need to run the docker run command and specify the name or ID of your Docker image, as well as any port mappings or environment variables.

How do I access my Node.js application running in a Docker container?

You can access your Node.js application by opening a web browser and navigating to http://localhost:<port> where <port> is the port number that you mapped to your container.

Leave a Reply

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