Dockerizing a Node.js App

Dockerizing a Node.js App

·

2 min read

In this tutorial, we'll guide you through the process of Dockerizing a Node.js application and deploying it on App Server. The task involves creating a Dockerfile, building a Docker image, and running a container with specific configurations.

Prerequisites:

  • Access to App Server.

  • Basic knowledge of Docker and Node.js.

Task Overview:

The task requires Dockerizing a Node.js application located under the /node_app directory on App Server. The Dockerfile must install dependencies from the package.json file and use server.js as the entry point. Additionally, the container should expose port 6400 and map it to port 8092 on the host.

1. SSH into the Host Server:

First, establish an SSH connection to App Server where the Node.js application is located.

ssh user@app_server_ip

Replace user with your username and app_server3_ip with the IP address of App Server.

2. Create the Dockerfile:

Navigate to the /node_app directory and create a Dockerfile with the following content:

FROM node

COPY . .

RUN npm install

CMD ["node", "server.js"]

EXPOSE 6400

This Dockerfile specifies to use the official Node.js image as the base, copies the application files, installs dependencies, sets the command to run the server.js file, and exposes port 6400.

3. Build the Docker Image:

Execute the following command to build the Docker image named nautilus/node-web-app:

docker build -t nautilus/node-web-app .

This command builds the Docker image based on the Dockerfile in the current directory (.) and tags it as nautilus/node-web-app.

4. Run the Docker Container:

Next, run a container named nodeapp_nautilus using the built image and mapping port 8092 on the host to port 6400 in the container:

docker container run -d --name nodeapp_nautilus -p 8092:6400 nautilus/node-web-app

This command starts a Docker container in detached mode (-d), names it nodeapp_nautilus, and maps port 8092 on the host to port 6400 in the container.

5. Verify the Deployment:

To verify that the Node.js application is running successfully, use curl to send a request to the application endpoint:

curl http://localhost:8092

This command should return the response from the Node.js application, indicating that it is up and running.

Conclusion:

In this tutorial, you learned how to Dockerize a Node.js application and deploy it on App Server using Docker containers. By following the provided steps, you can efficiently package and deploy Node.js applications in Dockerized environments, enabling easy scalability and management of your applications.