• Latest
  • Trending
How to use Docker in your Node and React Applications

How to use Docker in your Node and React Applications

June 16, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Monday, 1 June, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

How to use Docker in your Node and React Applications

by ITECHNEWS
June 16, 2022
in Data Science, Leading Stories
0 0
0
How to use Docker in your Node and React Applications

Docker gives developers the ability to package all of their applications inside of containers. These containers can run on any machine that has Docker installed and the application will be identical. This is a great way to run a clone of a codebase on multiple systems and you can be sure that they are all going to be the same.

CI/CD workflows and DevOps testing environments are significantly better when using Docker which is essentially a set of software tools that can be shared. Kubernetes is another tool which is used for operating multiple Docker containers but in a much larger scale.

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

In this tutorial we will learn how to create and run a NodeJS Express backend and a React frontend inside of a Docker container.

Running a NodeJS Express backend inside Docker

Before you begin make sure that you have Docker installed and running on your computer.

Now use the command line to navigate to a directory like your desktop then run the commands below.

mkdir my-app-docker
cd my-app-docker
touch docker-compose.yml
mkdir api
cd api
npm init -y
npm i express
touch app.js Dockerfile .dockerignore
cd ..

We setup a backend called api and created some Docker files. Now open the project in your code editor and add the code below to their corresponding files.

Put this in the docker-compose.yml file. Be careful with the yaml formatting otherwise you will get Docker errors when you try to run it.

version: '3.8'
services:
  api:
    build: ./api
    container_name: api_backend
    ports:
      - '4000:4000'
    volumes:
      - ./api:/app
      - ./app/node_modules

Add this is the app.js file.

const express = require('express');

const app = express();

const port = process.env.PORT || 4000;

app.get('/', (req, res) => {
  res.send('Home Route');
});

app.listen(port, () =>
  console.log(`Server running on port ${port}, http://localhost:${port}`)
);

Now add this line to the .dockerignore file.

node_modules

Next add this code to the Dockerfile file.

FROM node:16-alpine

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 4000

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

Lastly add this run script to the package.json file.

"scripts": {

"start": "node app.js"

},

We just created a basic NodeJS Express app that runs on port 4000. That port is also mapped to 4000 in docker which lets us run it in a Docker container.

Using Nodemon to have the server auto restart when changes occur

If you want to have the server restart every single time you make a change to the files in the backend then you can configure it to use Nodemon.

All you have to do is update the Dockerfile and package.json file inside of the api folder.

Update the code in the Dockerfile using the code below. We are now installing Nodemon at the start and using dev as the run command.

FROM node:16-alpine

RUN npm install -g nodemon

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 4000

CMD ["npm", "run", "dev"]

Now update the package.json file with this run script for Nodemon.

"scripts": {

"start": "node app.js",

"dev": "nodemon -L app.js"

},

You will probably need to restart your Docker containers and maybe recreate the containers but the backend should be auto updating anytime you make a change inside of the app.js file and refresh your browser window.

Starting the servers

To run the server outside of a Docker container using Node like normal just run the code below in your command line. You need to be sure that you are inside of the api folder. If you go to http://localhost:4000 you should see the home route in your browser window.

npm run start

Getting the NodeJS Express app to run inside of Docker requires a different command. First you need to be in the root folder where the docker-compose.yml file is. Now run the command below and it should run inside of a Docker container.

Don’t forget to stop the node server running first because you can only have one server running on port 4000.

docker-compose up

If you go to http://localhost:4000 you should see the home route in your browser window.

You can stop the server with the command below or you can go to the Docker app and stop the container from running.

docker-compose down

Running a React frontend inside Docker

Now lets create a React frontend! Use your command line to get inside of the root folder for my-app-docker. Run the commands below to setup the project.

npx create-react-app client
cd client
touch .dockerignore Dockerfile

Now add the code below into their corresponding files.

Add this line into the .dockerignore file.

node_modules

Put this code into the Dockerfile file.

FROM node:17-alpine

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

Finally update the docker-compose.yml in the root folder with the code below. We have added a client section at the bottom with settings for getting React running inside of a Docker container. Be careful with the yaml formatting otherwise you will get Docker errors when you try to run it.

version: '3.8'
services:
  api:
    build: ./api
    container_name: api_backend
    ports:
      - '4000:4000'
    volumes:
      - ./api:/app
      - ./app/node_modules
  client:
    build: ./client
    container_name: client_frontend
    ports:
      - '3000:3000'
    volumes:
      - ./client:/app
      - ./app/node_modules
    stdin_open: true
    tty: true

Starting the servers

To run the server outside of a Docker container using Node like normal just run the code below in your command line. Make sure that you are inside of the client folder. If you go to http://localhost:3000 you should see the home route in your browser window.

npm run start

Getting the React app to run inside of Docker requires a different command. First you need to be in the root folder where the docker-compose.yml file is. Now run the command below and it should run inside of a Docker container.

Don’t forget to stop the React app server running first because you can only have one server running on port 3000.

docker-compose up

If you go to http://localhost:3000 you should see the home route in your browser window.

You can stop the server with the command below or you can go to the Docker app and stop the container from running.

docker-compose down

With this setup you can have a NodeJS backend and React frontend running at the same time inside of Docker! If you encounter any errors then you might need to open your Docker desktop application and remove any images that are related to this project. Then you can try running the docker-compose upcommand again and hopefully this time everything should be working as expected.

Source: Andrew Baisden
Tags: How to use Docker in your Node and React Applications
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version