Git Product home page Git Product logo

nodejs-app's Introduction

Docker is a container platform that allows you to build, test, and deploy applications quickly.

Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime.

Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.

Here we are deploying a simple node app using docker. lately, we will see how to reduce the image size with the help of a multi-stage Dockerfile.

Prerequisites:

Ubuntu o/s platform (I am using AWS EC2 ubuntu instance)

Docker installed on our o/s.

the node js application with package file (if package file does not exist then you should create using run command "npm init -y")

make sure the inbound rule for port 3000 is open.

I used Ec2 Instance

AWS

Dockerfile uses the official Node.js Docker image as the base, copies your application files into the image, installs dependencies, exposes port 3000, and specifies the command to run your Node.js server.

Open a terminal, and clone the GitHub repo: clone repo

navigate to your project directory: And run the following command to build your Docker image:

build dockerfileocker build

This command will build a Docker image with the tag my-nodejs-app. you can now see the image.

Dockerimage

Run the Docker container: container

This command will start a Docker container based on your image, and it will forward port 3000 from the container to port 3000 on your EC2.

Now, your Node.js application is Dockerized, and you can access it in your web browser at http://public-ip:3000

Page

You can see the image size is 911 MB isn't it so much, so to reduce the size of this image we use a multistage Dockerfile.

With multi-stage Dockerfiles, you can also share data between build stages. This way, you can build the application in one stage and copy only the necessary components that the application needs to run to the final image, resulting in smaller and more optimized Docker images.

first, we need to deallocate port 3000.

Multistage Dockerfile

Stage 1: Build the application

FROM node:14 AS builder

WORKDIR /usr/src/app

Copy package.json to the working directory

COPY package*.json ./

Install dependencies

RUN npm install --only=production

Copy application source code

COPY . .

Stage 2: Create the production image

FROM node:14-alpine

WORKDIR /usr/src/app

Copy only necessary files from the previous stage

COPY --from=builder /usr/src/app .

Expose port 3000

EXPOSE 3000

Command to run the application

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

In this Dockerfile:

Stage 1 (Builder): We use the node:14 image as the base image to build the application. We copy package.json install dependencies (excluding development dependencies), and copy the application source code.

Stage 2 (Production): We use a lighter-weight node:14-alpine image as the base image for the production stage. We copy only the necessary files from the builder stage, namely the built application and the dependencies installed earlier. This reduces the size of the final image.

To build the Docker image, run:

docker build . -t new-node-js-app

new build

new container from multistage dockerfile

newcontainer

Then browser at http://public-ip:3000

newpage

So what have we achieved? We reduced the docker image by 77%.

difference

So we do our project let clean of them.

cleaning

Thanks for reading, happy learning.

Tripti Dewangan

nodejs-app's People

Contributors

tripti-td avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.