Containerize a React JS App

Containerize a React JS App

Dear Friend,

Building and packaging is a most frequent activity in software development.

Docker makes it easier to create, deploy, and run applications by using containers.

TL;DR

Building and running docker container is a three step process as illustrated here.

Read on further for detailed steps :)

No alt text provided for this image
source:jfrog.com

How to containerise an app?

Step 0 : Create sample react application

Create and open a folder in visual studio code, then open a terminal and run the below command. This will create a sample react application.

npx create-react-app .        

Step 1: Write Dockerfile and place inside your project

The Dockerfile is a set of instructions that specify how to build a Docker image.

When you run the docker build command, Docker reads the Dockerfile to build the image.

No alt text provided for this image

In this Dockerfile:

  1. The FROM instruction specifies the base image. In this case, the base image is node:12.18.3-alpine. This is a version of Node.js which is built on top of the lightweight Alpine Linux distribution.
  2. The WORKDIR instruction sets the working directory for instructions in the Dockerfile. In this case, the working directory is set to /app.
  3. The COPY instruction copies files from the host filesystem into the image. In this case, it is copying the entire current directory (.) into the /app directory in the image.
  4. The RUN instruction runs a n this case, npm install to install the dependencies specified in package.json file.
  5. The RUN instruction again runs a command within the image, npm run build to build the application.
  6. The CMD instruction specifies the default command to execute when a container is started from the image.

Step 2: Build a Docker Image

The docker build command will build a Docker image from a Dockerfile.

The -t option specifies the name. The . at the end of the command indicates the build context, which is the location of the Dockerfile.

In this case, the docker build command will build an image from the Dockerfile in the current directory (indicated by the .) and give it the name simple-app.



docker build -t simple-app .?        

After the above step, you an find a Docker Image named simple-app in your docker desktop

No alt text provided for this image

Step 3: Run the application using the container image



docker run -p 3000:3000 simple-app        

The docker run command would to start a new container from an image.

The -p 3000:3000 option maps the container's port 3000 to the host's port 3000. This means that traffic sent to the host's port 3000 will be forwarded to the container's port 3000.

The simple-app at the end of the command is the name of the image that the container will be created from.

The docker run command starts a new container from the simple-app image. And forward traffic from the host's port 3000 to the container's port 3000.

This is useful when the simple-app image contains a web server that listens on port 3000. It allows you to access the service running in the container by connecting to the host's port 3000.

In this example you could access the app by opening a web browser and navigating to https://localhost:3000.

No alt text provided for this image

Next steps

You can get the sample app to get started on building and running a container.

Read about containerization

Sirajeddine Bouasker

?? PMP? || SMC? || Outsystems Certified Developer || Believer, challenger, achiever ||????? ?? ???? ???? ???? ????

2 年

Thanks for sharing

要查看或添加评论,请登录

Rajesh Muthusamy的更多文章

  • Terraform in 6 Weeks - Week 2: Build a Scalable Static Website

    Terraform in 6 Weeks - Week 2: Build a Scalable Static Website

    Dear friend, The week 2 objective is to create a scalable and highly available static website hosted on S3, served…

    2 条评论
  • Terraform in 6 Weeks - Week 1: Architectural view

    Terraform in 6 Weeks - Week 1: Architectural view

    Dear Friend, In Week 0, we covered the basics of Terraform to get you started. Now, let’s dive deeper into Terraform’s…

    2 条评论
  • Terraform in 6 Weeks - Week 0: Laying the Foundation

    Terraform in 6 Weeks - Week 0: Laying the Foundation

    Dear Friend, Terraform is the gold standard in Infrastructure as Code (IaC), and mastering it is a skill that will stay…

    8 条评论
  • Understanding the OSI Model

    Understanding the OSI Model

    Dear Friend, The Open Systems Interconnection (OSI) model is a fundamental conceptual framework that illustrates how…

    2 条评论
  • Understanding Linux File Permissions

    Understanding Linux File Permissions

    Dear Friend, Linux file permissions are a fundamental aspect of the operating system's security model, determining who…

  • Kubernetes Networking

    Kubernetes Networking

    Dear Friend, In the world of kubernetes, we have different levels of networking requirements. Container-to-container…

    1 条评论
  • How To Design A Container-Based Application?

    How To Design A Container-Based Application?

    Dear Friend, Container is an important moving part of microservices architecture. You will need to place your…

  • The Journey Of A Code To Pod

    The Journey Of A Code To Pod

    Dear Friend, The journey of code towards Pod has multiple stages, and would feel overwhelming to dig the path. But…

    2 条评论
  • How Pods Connect Over Network?

    How Pods Connect Over Network?

    Dear Friend, Kubernetes allows unrestricted communication between pods by default. All pods can communicate with each…

    5 条评论
  • What The Helm?

    What The Helm?

    Dear Friend, In the Kubernetes world, there's no shortage of buzzwords. One among them is Helm.

    1 条评论

社区洞察

其他会员也浏览了