๐Ÿณ๐Ÿ”€ Building a Multi-Container Application with Docker Compose ๐Ÿšข๐ŸŒ‰

Sprasad PujariSprasad Pujari
4 min read

๐Ÿš€ Project:

In this project, we'll build a multi-container application using Docker Compose. The application will consist of a Node.js backend, a React.js frontend, and a MongoDB database. Let's get started!

๐Ÿ“ Step 1: Set up the Project Structure

  1. Create a new directory for your project: mkdir multi-container-app

  2. Navigate into the directory: cd multi-container-app

  3. Create a directory for the backend: mkdir backend

  4. Create a directory for the frontend: mkdir frontend

๐Ÿ“ฆ Step 2: Initialize Node.js Projects

  1. Initialize a new Node.js project for the backend:

    • cd backend

    • npm init -y

  2. Install required dependencies for the backend (e.g., Express.js, Mongoose):

    • npm install express mongoose

  3. Initialize a new Node.js project for the frontend:

    • npx create-react-app frontend

      The npx command above will create a new directory named frontend in the multi-container-app working directory. Now, cd into the frontend directory.

  4. Install required dependencies for the frontend (e.g., React.js):

    • npm install react react-dom

โš™๏ธ Step 3: Set up the Backend

  1. In the backend directory, create a new file server.js

  2. Add the following code to server.js to set up a basic Express server and connect to MongoDB:

๐Ÿ–ฅ๏ธ Step 4: Set up the Frontend

  1. In the frontend directory, go to src/App.js

  2. Add the following code to src/App.js to create a basic React component:

  3. Create a new file src/index.js and add the following code to render the React app:

๐Ÿณ Step 5: Create Docker Images

  1. In the project root directory, create a new file Dockerfile for the backend:

  2. Create another Dockerfile for the frontend in the frontend directory:

๐Ÿšข Step 6: Set up Docker Compose

  1. In the project root directory, create a new file docker-compose.yml

  2. Add the following content to docker-compose.yml:

๐Ÿš€ Step 7: Build and Run the Application

  1. In the project root directory, run the following command to build the Docker images:

    • docker-compose build

  2. Once the build is complete, run the following command to start the containers:

    • docker-compose up

You should now be able to access the frontend at http://localhost:3000

and the backend at http://localhost:5000.

Monitoring and Maintenance:

๐Ÿ‘‡ Click the link below to access the code ๐Ÿ‘‡

You can find all of the Docker code used in this tutorial on the following GitHub repository:

https://github.com/sprasadpujari/Docker-Projects/tree/main/multi-container-app

This link will take you to the specific GitHub repository and directory where the code for the multi-container application with Docker Compose is hosted. Explore the code, experiment, and enhance your Docker skills!

๐ŸŽ‰ Congratulations! You've successfully built a multi-container application with Docker Compose, including a Node.js backend, a React.js frontend, and a MongoDB database.

Conclusion ๐ŸŽ‰

This project demonstrates how Docker Compose simplifies the process of building and running multi-container applications by defining the services, dependencies, and configurations in a single file.

Feel free to explore and expand upon this project by adding more features, integrating additional services, or deploying it to a production environment using Docker and orchestration tools like Kubernetes.

Happy coding and containerizing! ๐Ÿณ๐Ÿ’ป

Thank you for joining me on this exciting journey through the world of cloud computing! Your interest and support mean a lot, and I'm thrilled to continue exploring this fascinating field together. Let's stay connected and keep learning and growing as we navigate the ever-evolving landscape of technology. ๐Ÿš€

๐Ÿ“ Connect with me on LinkedIn: https://www.linkedin.com/in/prasad-g-743239154/

๐Ÿ“ฌ Or reach out directly at spujari.devops@gmail.com. I'm always open to hearing your thoughts and suggestions, as they help me improve and better cater to your needs. Let's keep moving forward and upward!

๐Ÿค If you found this blog post helpful, please show your support by giving it a round of applause ๐Ÿ‘๐Ÿ‘๐Ÿ‘. Your engagement boosts the visibility of the content and lets other DevOps and Cloud Engineers know that it might be useful to them too. Thank you for your support! ๐Ÿ˜€

Happy deploying, and let's continue our cloud computing adventures together!

Best Regards,

Sprasad

0
Subscribe to my newsletter

Read articles from Sprasad Pujari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sprasad Pujari
Sprasad Pujari

Greetings! I'm Sprasad P, a DevOps Engineer with a passion for optimizing development pipelines, automating processes, and enabling teams to deliver software faster and more reliably.