When you start building full-stack applications, sooner or later, you run into the question of how to make them easy to run anywhere. Maybe your React frontend works great locally, and your Node.js backend runs fine on your machine, but how do you hand that project to another developer or deploy it to production without the familiar “it works on my computer” problem?
That is where Docker comes in. Docker lets you package your application into small units called containers. These containers include the code, runtime, libraries, and settings needed to run your app. With Docker, you do not need to worry about differences between machines or operating systems.
In this guide, we will walk through the steps to Dockerize a simple React + Node.js application. By the end, you will have a clear idea of how to run both the frontend and backend inside containers and make them talk to each other.
Why Docker?
Before diving in, let’s answer the basic “why.” Why should you bother learning Docker at all?
Consistency: No matter where you deploy your app, the container will run the same.
Isolation: Each service (React frontend, Node backend, database) runs in its own container without interfering with the others.
Easy sharing: You can give your teammate a single
docker-compose.ymlfile, and they can spin up the whole stack without manual setup.Deployment ready: Most cloud platforms support Docker, so once you Dockerize your app, deployment becomes much smoother.
Think of Docker like shipping your app inside a box. Whoever receives it just needs Docker installed to run it.
Setting Up a Simple React + Node.js App
To keep things simple, we will create a minimal full-stack app. The backend will be Node.js with Express, and the frontend will be a React app built with Create React App.
1. Create the backend
mkdir docker-demo
cd docker-demo
mkdir backend
cd backend
npm init -y
npm install express corsCreate an index.js file inside the backend folder:
const express = require("express");
const cors = require("cors");
const app = express();
const PORT = 5000;
app.use(cors());
app.get("/api", (req, res) => {
res.json({ message: "Hello from the backend!" });
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});This backend serves a simple JSON response.
2. Create the frontend
Go back to the root folder and create a React app:
cd ..
npx create-react-app frontendOpen frontend/src/App.js and update it:
import { useEffect, useState } from "react";
function App() {
const [data, setData] = useState("");
useEffect(() => {
fetch("http://localhost:5000/api")
.then((res) => res.json())
.then((result) => setData(result.message));
}, []);
return (
<div>
<h1>React + Node.js with Docker</h1>
<p>{data}</p>
</div>
);
}
export default App;At this point, if you run both npm start inside the frontend and node index.js Inside the backend, they will work together.
Now let’s containerize them.
Writing Dockerfiles
Each service needs its own Dockerfile that explains how to build the image.
1. Dockerfile for the backend
Create a Dockerfile inside the backend folder:
# Use official Node.js image
FROM node:18
# Set working directory
WORKDIR /app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the code
COPY . .
# Expose port 5000
EXPOSE 5000
# Run the app
CMD ["node", "index.js"]
Join the conversation! Your thoughts help the community grow.