# Day 12 : Docker Compose

Since, we Dockerized all services, let’s use **Docker Compose** to run them together locally with one command, simulating a microservices environment. This introduces orchestration (a taste of Kubernetes) while reinforcing Docker

**Objective**

* Create a docker-compose.yml to orchestrate User, Product, Order, and Recommend Services.

* Run and test all services as a system, commit to GitHub

* Each container (e.g., order-service, product-service) runs in its own isolated environment with its own network stack.

* Inside the order-service container, localhost refers to *itself*, not the host machine or other containers. So, <http://localhost:8080/users> tries to find a service running at port 8080 *inside* the Order Service container, which doesn’t exist—hence the Error: User Service unavailable

* Containers run in a virtual network. Service names act like DNS entries, enabling communication (e.g., Order calls User via user-service:8080).

* This setup mirrors real-world microservices, where services discover each other dynamically (e.g., via DNS, service registries)

<figure><img src="/files/vDeT1t6cC2qY7sSqsf0a" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/wYSAxwmglZStAh2lv4kC" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/ROR6ZjNrXw0NlQg4vVip" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/bHfT4uAKUfpuzUWxvpRB" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/9gdcpNOMDHDkCnh3WScb" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/WHe9OijKZV32b58SOJP5" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/C49DrRUP2ZEzChdkuul5" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/vCWczBlwKmJZbz0qBsjH" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/6jd0Pz2odus704iuK0lj" alt=""><figcaption></figcaption></figure>

### Let's make our application Dynamic (CRUD Functionality)

Let’s add **CRUD** operations (Create, Read, Update, Delete) to both services, allowing users to:

* **Add** new users/products.
* **Retrieve** all or specific users/products.
* **Update** existing ones.
* **Delete** them. This will make your e-commerce app interactive

Approach :&#x20;

* **Storage**: Use in-memory lists (e.g., Python dictionaries, Java HashMaps) for simplicity—no database yet (we’ll add one in Phase 5).
* **APIs**: Add REST endpoints (POST, GET, PUT, DELETE) to User and Product Services.
* **Order/Recommend**: Update to handle dynamic data (e.g., fetch by ID).
* **Compose**: Keep running all services seamlessly.

### User-service

* Adds express.json() for parsing POST/PUT bodies.
* Stores products in an array.
* Endpoints:
  * GET /products: List all.
  * GET /products/:id: Get by ID.
  * POST /products: Create ({"name": "Phone", "price": 499}).
  * PUT /products/:id: Update.
  * DELETE /products/:id: Delete.

### orders-service

* Changed to POST /orders—accepts {"user\_id": 1, "product\_id": 101}.
* Calls specific user/product by ID.
* Validates input, returns 404 if not found.

**Purpose**: Serves a webpage at <http://localhost:8080/> with:

* A table listing users.
* Forms to add (name) and update (ID + name).
* Delete buttons per user.
* JavaScript for API calls to /users

<figure><img src="/files/at1tem13ZNUxT3OSQlRi" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/o6OSbrCSP4pcrDUpGP4Z" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/zXROnCgHPtptGg6VyGPE" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/K0x4zcqIrEhdr4OOoDae" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://charan-techjourney.gitbook.io/charan-techjournal/50-days-of-devops-ecommerce-microservices/project-daily-tasks/day-12-docker-compose.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
