> For the complete documentation index, see [llms.txt](https://docs.cloudscript.ai/cloudscript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cloudscript.ai/cloudscript/syntax/component-modules/service-blocks/containers.md).

# Containers

Container blocks manage containerized applications and services. Facilitating the definition of container deployments, jobs, configurations, and associated Kubernetes resources like services, probes, and volumes.

## Syntax

```hcl
resource "name" {
  containers {
    [container_type] "[container_name]" {
      type = [k8s_resource_type]
      [attribute] = [value]
    }
  }
}
```

(Note: the "\[container\_type]" has no effect on functionality)

## Defining Containers

Each container is defined by its type, name, and relevant attributes. Attributes vary based on the container type.

**Common Attributes:**

* **image**: The container image to use.
* **type**: The type of Kubernetes resource (e.g., Deployment, CronJob).
* **replicas**: Number of pod replicas.
* **command/args**: Commands and arguments to run inside the container.
* **resources**: Resource limits and requests.
* **ports**: Port configurations.
* **service**: Service-related settings.
* **auto\_scaling**: Defines auto-scaling parameters.
* **node\_selector**: Specifies node selection criteria.

## Auto Scaling

Defines the auto-scaling behavior for container deployments based on CPU utilization or other metrics.

**Attributes:**

* **min\_replicas**: Minimum number of replicas.
* **max\_replicas**: Maximum number of replicas.
* **target\_cpu\_utilization\_percentage**: Target CPU usage for scaling.

## Services

Configures Kubernetes Services to expose containers. Supports different service types like LoadBalancer, ClusterIP, etc.

**Attributes:**

* **type**: Type of service (e.g., LoadBalancer, ClusterIP).
* **annotations**: Key-value pairs for service annotations.
* **ports**: Port configurations for the service.

## Probes and Resources

* **readiness\_probe**: Defines how Kubernetes determines if a container is ready to receive traffic.
* **resources**: Sets resource limits and requests for containers.

## Example

```
service "name" {
 containers {
    app "web_app" {
      image             = "nginx:latest"
      type              = "Deployment"
      replicas          = 3
      command           = ["/bin/sh"]
      args              = ["-c", "nginx -g 'daemon off;'"]
      working_dir       = "/usr/share/nginx/html"

      readiness_probe = {
        http_get = {
          path = "/healthz"
          port = 80
        }
        initial_delay_seconds = 5
        period_seconds        = 10
      }

      resources = {
        limits = {
          cpu    = "500m"
          memory = "512Mi"
        }
        requests = {
          cpu    = "250m"
          memory = "256Mi"
        }
      }

      empty_dir_volumes = [
        {
          name       = "cache"
          size_limit = "1Gi"
        }
      ]

      volume_mounts = [
        {
          name      = "cache"
          mountPath = "/cache"
        }
      ]

      ports = [
        {
          container_port = 80
          service_port   = 80
        }
      ]

      service = {
        type        = "LoadBalancer"
        annotations = {
          "service.beta.kubernetes.io/aws-load-balancer-type" = "nlb"
        }
      }

      node_selector = {
        "kubernetes.io/os" = "linux"
        "node-type"        = "web"
      }

      auto_scaling = {
        min_replicas                      = 2
        max_replicas                      = 10
        target_cpu_utilization_percentage = 80
      }
    }
  }
}
```
