DEV Community

Danillo Gomes
Danillo Gomes

Posted on • Updated on

Scalable web application development with React and Docker

Combining the technologies of React and Docker is a great way to build and deploy modern web applications more efficiently and securely. React is a popular JavaScript library for creating user interfaces, which offers a great development experience and makes it easier to build complex interfaces. Docker, on the other hand, is an application virtualization platform that simplifies the process of creating, distributing, and running applications in different development and production environments.

By using React in conjunction with Docker, developers can create a more agile development environment with fewer dependencies and configurations, as well as facilitate deployment in different environments, such as production servers, test environments, and development. Furthermore, by using a Docker container to host the React application, developers can isolate the application from the execution environment, ensuring that it runs consistently and reliably on different platforms and environments.

In this article, we will show how to create a small project using these technologies and how to configure them to work together, providing an overview of how React and Docker can be combined to create modern and efficient web applications.

To get started, let's create a simple React project using create-react-app. To do this, open your terminal and execute the following command:

npx create-react-app my-app
Enter fullscreen mode Exit fullscreen mode

This will create a folder called my-app with a basic React project structure. Next, let's add the Dockerfile for the application image.

Create a file called Dockerfile at the root of the project and add the following code:

dockerfile

Installs the node image
FROM node:latest

Sets the working directory inside the container
WORKDIR /app

Copies package.json and package-lock.json into the container
COPY package*.json ./

Installs application dependencies
RUN npm install

Copies all application files into the container
Compiles the application for production
RUN npm run build

Defines the port to be exposed
EXPOSE 3000

Starts the application
CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

This Dockerfile basically creates a Docker container image for the React application. It uses the latest Node image as a base, copies the project files into the container, installs the application dependencies, compiles the React code for production, and starts the server.

Now that we have the Dockerfile, let's start Docker with the application. To do this, open your terminal at the root of the project and execute the following command:


docker build -t my-app .
Enter fullscreen mode Exit fullscreen mode

This will create a Docker image with the name my-app. Next, execute the following command to start the container with the image we just created:

docker run -p 3000:3000 my-app
Enter fullscreen mode Exit fullscreen mode

This will start the React server on port 3000. Go to http://localhost:3000 to view the application.

Finally, let's add the .dockerignore file to the root of the project and add the following lines:

node_modules

build

Enter fullscreen mode Exit fullscreen mode

This will prevent the node_modules folder and the build folder from being included in the Docker image, reducing the size of the image and improving performance.

With these settings, we can now push our project.

Top comments (1)

Collapse
 
sloan profile image
Info Comment hidden by post author - thread only accessible via permalink
Sloan the DEV Moderator

Hey, this article seems like it may have been generated with the assistance of ChatGPT.

We allow our community members to use AI assistance when writing articles as long as they abide by our guidelines. Could you review the guidelines and edit your post to add a disclaimer?

Some comments have been hidden by the post's author - find out more