# Build Context

The "Build Context" field specifies the directory that will be utilized during the build process. Proper configuration of this field is crucial to ensure that the build process can locate all necessary files and resources. Below are the details on how to use this field effectively for different builders:

# Default Behavior

The "Build Context" is set to `/` by default. If the specified directory does not contain a **Dockerfile**, CloudStation will default to using the **Nixpacks** builder. You only need to update this field if your project has a special directory structure or specific build requirements.

# Usage

## For Docker Builder


1. **Project Root**
   * **Description**: Use this option if the root directory of your project contains all the necessary files for building your application.
   * **Example**: If your project structure is as follows:

     ```javascript
     /
     ├── app/
     ├── config/
     ├── Dockerfile
     └── src/
     
     ```

     Set the "Build Context" to `/`

     .
2. **Subdirectory with Dockerfile**
   * **Description**: Use this option if your **Dockerfile** is located in a subdirectory within your project.
   * **Example**: If your project structure is as follows:

     ```javascript
     /
     ├── app/
     ├── config/
     ├── docker/
     │   └── Dockerfile
     └── src/
     
     ```

     Set the "Build Context" to `/docker`

     .

## For Nixpacks Builder


1. **Project Root**
   * **Description**: Use this option if the **root** directory of your project contains all the necessary files for building your application using **Nixpacks**.
   * **Example**: If your project structure is as follows:

     ```javascript
     /
     ├── app/
     ├── config/
     └── src/
     
     ```

     Set the "Build Context" to `/`

     .
2. **Subdirectory with Build Configurations**
   * **Description**: Use this option if your build configurations or necessary build files are located in a subdirectory within your project.
   * **Example**: If your project structure is as follows:

     ```javascript
     /
     ├── app/
     ├── config/
     ├── build/
     │   └── build.config
     └── src/
     
     ```

     Set the "Build Context" to `/build`

     .

## Common Scenarios

#### Frontend Applications

**Scenario 1: Single Page Application (SPA) with Docker Builder**

* **Project Structure:**

  ```javascript
  /
  ├── public/
  ├── src/
  ├── Dockerfile
  └── package.json
  
  ```
* **Build Context:** Set the "Build Context" to `/`

  .

**Scenario 2: SPA with Nixpacks Builder**

* **Project Structure:**

  ```javascript
  /
  ├── public/
  ├── src/
  └── package.json
  
  ```
* **Build Context:** Set the "Build Context" to `/`

  .

#### Backend Applications

**Scenario 3: Node.js Backend with Docker Builder**

* **Project Structure:**

  ```javascript
  /
  ├── src/
  ├── Dockerfile
  └── package.json
  
  ```
* **Build Context:** Set the "Build Context" to `/`

  .

**Scenario 4: Node.js Backend with Nixpacks Builder**

* **Project Structure:**

  ```javascript
  /
  ├── src/
  └── package.json
  
  ```
* **Build Context:** Set the "Build Context" to `/`

  .

#### Mono-repo with Multiple Services

**Scenario 5: Mono-repo with Separate Dockerfiles**

* **Project Structure:**

  ```javascript
  /
  ├── service1/
  │   ├── src/
  │   ├── Dockerfile
  │   └── package.json
  ├── service2/
  │   ├── src/
  │   ├── Dockerfile
  │   └── package.json
  └── common/
  
  ```
* **Build Context for Service 1:** Set the "Build Context" to `/service1`.
* **Build Context for Service 2:** Set the "Build Context" to `/service2`.

**Scenario 6: Mono-repo with Nixpacks**

* **Project Structure:**

  ```javascript
  /
  ├── service1/
  │   ├── src/
  │   └── package.json
  ├── service2/
  │   ├── src/
  │   └── package.json
  └── common/
  
  ```
* **Build Context for Service 1:** Set the "Build Context" to `/service1`**.**
* **Build Context for Service 2:** Set the "Build Context" to `/service2`.

#### Using Multiple Dockerfiles

**Scenario 7: Backend Service with Separate Dockerfiles for Development and Production**

* **Project Structure:**

  ```javascript
  /
  ├── src/
  ├── Dockerfile.dev
  ├── Dockerfile.prod
  └── package.json
  
  ```
* **Build Context for Development:** Set the "Build Context" to `/Dockerfile.dev` in your build configuration.
* **Build Context for Production:** Set the "Build Context" to /`Dockerfile.prod` in your build configuration.

## Troubleshooting

* **File Not Found**: If the build process cannot find certain files, double-check the path specified in the "Build Context" and ensure it is correct relative to the root of your repository.
* **Build Failures**: If you encounter build failures, review the build configuration and the context directory to ensure all necessary dependencies and files are included.

By following these guidelines, you can effectively configure the "Build Context" field to ensure smooth and efficient build processes for your applications, whether using Docker or Nixpacks builders.

---

**Documents**

- [Changelog](https://documentation.cloud-station.io/s/ce6e8846-8aec-4337-a850-5188b6dc6d6e/doc/changelog-r4OVtpiRA5)
- [CloudStation Tutorials](https://documentation.cloud-station.io/s/ce6e8846-8aec-4337-a850-5188b6dc6d6e/doc/cloudstation-tutorials-brnm0gaWUb)
- [Documentation](https://documentation.cloud-station.io/s/ce6e8846-8aec-4337-a850-5188b6dc6d6e/doc/documentation-4X2U9io3hK)
- [Understanding CloudStation Plans](https://documentation.cloud-station.io/s/ce6e8846-8aec-4337-a850-5188b6dc6d6e/doc/understanding-cloudstation-plans-UqY29FsY4w)