Why and When to use Docker, Developing React App using Docker Container with Live Reload
Tags: docker, react, development, webdev
Published: 2024-01-02 21:29:29


![developing React in Docker container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3hcaf5yr6fv2v2mjs9o1.png)
**Why to use Docker?**
Docker is an open platform offers portability, consistency, and scalability for developing, shipping, and running applications in different environments. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.
A Docker container, unlike a virtual machine, does not require a separate operating system. Which gives Docker container advantages such as light-weight, consumes less space, more portability and needs short boot-up time.

![Docker vs VM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qleiwyg3ps10h511m3sl.png)

**When to use Docker?**
1- If your business grows and new developers can join your team every few months. So instead of wasting valuable time setting up their local development environment. Such as the database or third-party libraries which may take a few hours to a couple of days. Which depends on the complexity of the project. Docker helps automate this setup so new developers can get right on board. 

2- If the same software needs to run in a different environment: for example, a developer working on two PCs, development PCs and production servers, developers PCs, and testers PCs. Docker, you can run your application in separate containers, so your application can run in a consistent and predictable way in each environment.

3- If you need to test new technologies for your program, such as a new database, a different programming language, or new tools. So Docker Hub can be leveraged to pull the required package, tool or database to test with your application into an isolated container.

4- In case your software requires running multiple applications on the server? So, with Docker, you can keep the components of each application in separate containers even if they share the same resources.


**When to avoid using Docker?**
1- Working on the desktop application. Because Docker is more useful with web applications running on a server. Also, the rich GUI also requires additional solutions to work with Docker.

2- Working on a small application that does not require any additional tools or services.

3- If your development team consists of one developer. In this case, the benefits of using Docker will be less, and you can start using Docker when your software starts to grow.

4- If the majority of your team developers use a MacBook because Docker faces many performance issues when running on the Mac operating system.
 
**Run React App on a Docker container to test a new environment:**
1- Install Docker in your PC form the official website
https://docs.docker.com/desktop/install/windows-install/

2- Add Dockerfile to your project directory which will be used to build a Docker image (Dockerize the React App)

```
# Stage 1
# Use the desired Node.js runtime as the base image
FROM node:18-alpine AS build
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
# Set the working directory in the container
WORKDIR /app

# Copy package.json and pnpm-lock.yaml to the working directory
COPY package.json ./
COPY pnpm-lock.yaml ./

# Install project dependencies
RUN pnpm install

# Copy the entire React app code to the container
COPY . .

# Stage 2
# Expose the port of your application to bind with the host port
EXPOSE 3000
 
# run your app
CMD ["pnpm", "run", "start"]
```

3- Add .dockerignore file:

```
/node_modules
/.history
/build
npm-debug.log
.git
.gitignore
```

4- Build a Docker image based on Dockerfile
docker image build -t <image_name>:<tag> <path>
-t option specifies the name and tag (latest by default) for the image. <path> represents the path in which you want to run the command (. (dot at the end) represent the current directory)
 
After building the image, you can review the result in Docker Desktop software as the following:

![Docker Desktop software](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kcjsrta3ew146t1zyv5q.png)

5- Run Docker Container based on the generated image. By clicking on run or by using the following command
docker run --name <image_name> -p 3005:3000 -d -v ${pwd} <image_name>
For example: docker run --name ecommerce-website-react-reactquery-redux -p 3005:3000 -d -v ${pwd} ecommerce-website-react-reactquery-redux
Navigate to http://localhost:3005/ in your browser to view the app.

_Another way to run a container based on a Docker image is by clicking the run button next to the image name in the Docker desktop software_:

![Run Docker container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyvbibhi1p4uep2dsu4b.png)

6-Install the following extensions in VS Code
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers
https://marketplace.visualstudio.com/search?term=docker&target=VSCode

![VS Code required extensions for Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dm3qnlbds9ylsfrao0um.png)

7- In the lower left cornet in VS Code you will find a button “Open a remote view”. After clicking this button, click on “Attach a running container”. 

![Open VS Code in a remote view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8za2vlv2vi6zv8ud5ja.png)

After selecting the desired container, a new VS Code window will appear, in which you can make all the changes that you want and it will reflect in the browser.

![VS Code remote view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6j8f3bp5q2pi3csiab7i.png)

🎉🎉[Project on GitHub] E-commerce Web Application: React, Redux, React Query, MUI ... (https://github.com/alaa-m1/ecommerce-webapp-react-redux)