|
| 1 | +# PeerPrep Docker Compose Guide |
| 2 | + |
| 3 | +This project uses Docker Compose to manage multiple services such as a frontend, backend, and a database. The configuration is defined in the `docker-compose.yml` file, and environment variables can be stored in environment files for different environments (e.g., development, production). |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before you begin, ensure you have the following installed on your machine: |
| 8 | + |
| 9 | +- [Docker](https://www.docker.com/get-started) |
| 10 | +- [Docker Compose](https://docs.docker.com/compose/install/) |
| 11 | + |
| 12 | +## Project Structure |
| 13 | + |
| 14 | +In the `./apps` directory: |
| 15 | + |
| 16 | +```plaintext |
| 17 | +. |
| 18 | +├── docker-compose.yml # Docker Compose configuration |
| 19 | +├── .env # Global environment variables (optional) |
| 20 | +├── frontend |
| 21 | +│ ├── Dockerfile # Dockerfile for frontend |
| 22 | +│ └── ... (other frontend files) |
| 23 | +├── question-service |
| 24 | +│ ├── Dockerfile # Dockerfile for question-service |
| 25 | +│ └── ... (other question-service files) |
| 26 | +├── user-service |
| 27 | +│ ├── Dockerfile # Dockerfile for user-service |
| 28 | +│ └── ... (other user-service files) |
| 29 | +└── README.md # Project documentation (for docker compose) |
| 30 | +``` |
| 31 | + |
| 32 | +## Docker Compose Setup |
| 33 | + |
| 34 | +By using multiple Dockerfiles in Docker Compose, we can manage complex multi-container applications where each service has its own environment and build process. |
| 35 | + |
| 36 | +1. Build and Start the Application |
| 37 | + |
| 38 | +To build and run both the frontend and backend services, you can change your directory to the `./apps` directory and run: |
| 39 | + |
| 40 | +```bash |
| 41 | +docker-compose up --build |
| 42 | +``` |
| 43 | + |
| 44 | +This will: |
| 45 | + |
| 46 | +- Build the Docker images for all services using the specified Dockerfiles |
| 47 | +- Start the containers and map the defined ports |
| 48 | + |
| 49 | +2. Access the Application |
| 50 | + |
| 51 | +Once running, you can access: |
| 52 | + |
| 53 | +- The **frontend** at http://localhost:3000 |
| 54 | +- The **user service** at http://localhost:3001 |
| 55 | +- The **question service** at http://localhost:8080 |
| 56 | + |
| 57 | +3. Stopping Services |
| 58 | + |
| 59 | +To stop the running services, run: |
| 60 | + |
| 61 | +```bash |
| 62 | +docker-compose down |
| 63 | +``` |
| 64 | + |
| 65 | +This command will stop and remove the containers, networks, and volumes created by docker-compose up. |
| 66 | + |
| 67 | +## Troubleshooting |
| 68 | + |
| 69 | +**Common Issues** |
| 70 | + |
| 71 | +- Port Conflicts: If you encounter port conflicts, ensure the host ports specified in docker-compose.yml (e.g., 3000:3000) are not in use by other applications. |
| 72 | +- Environment Variables Not Loaded: Ensure the `.env` files are in the correct directories as found in the `docker-compose.yml` file. |
| 73 | + |
| 74 | +**Logs** |
| 75 | + |
| 76 | +You can view the logs for each service using the following command: |
| 77 | + |
| 78 | +```bash |
| 79 | +docker-compose logs |
| 80 | +``` |
| 81 | + |
| 82 | +**Useful Commands** |
| 83 | + |
| 84 | +Rebuild a specific service: |
| 85 | + |
| 86 | +```bash |
| 87 | +docker-compose build <service_name> |
| 88 | +``` |
| 89 | + |
| 90 | +Start services in detached mode (run in the background): |
| 91 | + |
| 92 | +```bash |
| 93 | +docker-compose up -d |
| 94 | +``` |
| 95 | + |
| 96 | +Remove all containers, networks, and volumes created by Docker Compose: |
| 97 | + |
| 98 | +```bash |
| 99 | +docker-compose down --volumes |
| 100 | +``` |
0 commit comments