A React application demonstrating Docker containerization best practices for both development and production environments. The application features hot-reload development setup, automated testing, and a multi-stage production build with Nginx.
The application is built using Docker containerization with the following components:
- React Frontend: React 18 application built with Create React App
- Development Container: Node.js container with hot-reload enabled via volume mounting
- Test Container: Automated testing service with watch mode
- Production Container: Multi-stage build with Nginx serving optimized static files
- Docker Compose starts the development container with volume mounting
- Source code is mounted as a volume for live updates
- React development server runs with hot-reload enabled
- Changes to source files automatically trigger browser refresh
- Test service runs in watch mode for continuous testing
- Multi-stage Docker build process:
- Stage 1: Builds the React application using Node.js
- Stage 2: Serves the optimized build files using Nginx
- Static files are served efficiently through Nginx
- Application runs in a lightweight production container
- Docker and Docker Compose installed on your system
- Git (for cloning the repository)
-
Clone the repository:
git clone git@github.com:aqibh25/react-docker.git cd react-docker -
Start the development environment:
docker-compose -f docker-compose-dev.yml up
This will:
- Build and start the development container with hot-reload enabled
- Mount your local code as volumes for live updates
- Expose the application on
http://localhost:3000 - Start the test service in watch mode
-
Access the application:
- Open your browser and navigate to
http://localhost:3000 - Make changes to files in
src/and see them update automatically
- Open your browser and navigate to
-
Build the production image:
docker build -t react-docker-training:latest . -
Run the production container:
docker run -p 8080:80 react-docker-training:latest
The application will be available on
http://localhost:8080
- React 18.2.0
- React DOM 18.2.0
- React Scripts 5.0.1
- Web Vitals 2.1.4
- Testing Library (Jest DOM, React, User Event)
- Docker & Docker Compose
- Node.js LTS (Alpine)
- Nginx (Production)
In development mode, changes to your code are automatically reflected:
- React app: Changes in
src/will hot-reload in the browser - The
WATCHPACK_POLLINGenvironment variable ensures reliable file watching in Docker - Node modules are stored in a named volume to improve performance
Run tests in a Docker container:
docker-compose -f docker-compose-dev.yml run testOr start the test service with watch mode:
docker-compose -f docker-compose-dev.yml up testTo stop all containers:
docker-compose -f docker-compose-dev.yml downTo stop and remove volumes:
docker-compose -f docker-compose-dev.yml down -vIf you prefer to run the application locally:
-
Install dependencies:
npm install
-
Start the development server:
npm start
-
Run tests:
npm test -
Build for production:
npm run build