This project is a full-stack web application that uses a Django backend and a React frontend. The backend provides a RESTful API, and the frontend consumes this API to provide a dynamic user interface. Both the frontend and backend are containerized using Docker, allowing for easy deployment and scaling.
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can create and start all the services from your configuration with just a single command.
To start the application using Docker Compose, run the following command from the root directory:
docker compose up
Open your browser and navigate to http://localhost:5173/
.
This will start both the backend and frontend services. The Django application will be accessible at http://0.0.0.0:8000/
, and the React application will be accessible at http://localhost:5173/
.
To stop the services, use the following command:
docker compose down
If you make changes to the application and need to rebuild the containers, use the following command:
docker compose up --build
Before running the setup commands, ensure you are in the correct directory. For backend setup, navigate to the backend directory. For frontend setup, navigate to the frontend directory.
The backend is a Django application. To set it up:
- Navigate to the backend directory.
- Create a virtual environment and activate it (optional but recommended).
- Install the required packages using pip:
pip install -r requirements.txt
. - Run the Django migrations:
python manage.py migrate
. - Start the Django server:
python manage.py runserver
.
The Django application will start on
http://0.0.0.0:8000/
.
- Build the Docker image for the backend using
docker build -t backend .
from the backend directory. - Run the backend container using
docker run --publish 8000:8000 backend
.
The frontend is a React application bootstrapped with Vite. To set it up:
- Navigate to the frontend directory.
- Install the required packages using npm:
npm install
. - Start the Vite server:
npm run dev
.
The React application will start on
http://localhost:5173/
.
- Build the Docker image for the frontend using
docker build -t frontend .
from the frontend directory. - Run the frontend Docker container:
docker run --publish 5173:5173 frontend
.
This project follows a specific structure that separates the frontend and backend into their own directories, each with its own Dockerfile. This allows each part to be developed, tested, and deployed independently.
Create a folder structure in the following template:
.
├── backend
│ ├── api
│ ├── core
│ ├── db.sqlite3
│ ├── Dockerfile
│ ├── manage.py
│ └── requirements.txt
├── frontend
│ ├── Dockerfile
│ ├── index.html
│ ├── package.json
│ ├── public
│ ├── README.md
│ ├── src
│ └── vite.config.js
├── compose.yml
└── README.md
To use this structure for future projects, follow these steps:
- Clone or download this repository to your local machine.
- Replace the
frontend
andbackend
directories with your own applications. - Update the Dockerfiles in each directory to match the requirements of your applications.
- If necessary, update the
compose.yml
file to match the services of your applications. - Build and run your applications using Docker or Docker Compose as described in the sections above.