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:
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:
npm install -g express-generator // installs the express generator globally
express my-node-app // generates an express node.js application
Install node modules and run the application to see if your basic Node.Js application is correctly set up.
cd my-node-app // change directory to the node.js project
npm i // install node modules
npm start // start the node.js application
Head to the browser on
http://localhost:3000 to open the running node.js application.
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:
- Create a new file named Dockerfile in your project directory.
- Add the following lines to your Dockerfile:
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "npm", "start" ]
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:
docker build -t my-node-app .
my-node-app with the name of your Docker image. The
. (dot) at the end specifies the current directory as the build context.
Running a Docker Container
Once you have built your Docker image, you can run a Docker container using the following command:
docker run -p 3000:3000 my-node-app
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.
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.
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:
docker stop <container-id>
<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:
docker rm <container-id>
<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:
docker rmi <image-name>
<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.
docker ps -a
It will list out all the containers including running and non-running.
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
<port> is the port number that you mapped to your container.