# dokploy_compose (Resource)

A Dokploy compose service: a `docker-compose` project or a Docker Swarm `stack`.

Exactly one of the `github`, `git` or `raw` source blocks is required.

~> Dokploy also supports GitLab, Bitbucket and Gitea sources. None is modelled here, for the same reason the `dokploy_github_provider` data source covers only GitHub: no instance has been available to observe their shapes against, and inferring them is what this provider's endpoint census exists to prevent.

~> `dokploy_compose` owns the whole service. Applying it rewrites the source and operational configuration wholesale, so anything changed in the Dokploy UI is replaced on the next apply.

## Example Usage

```terraform
resource "dokploy_project" "example" {
  name = "example"
}

# Inline compose file. Nothing is fetched: the YAML below is the source.
resource "dokploy_compose" "inline" {
  name           = "inline-stack"
  environment_id = [for e in dokploy_project.example.environments : e.id if e.name == "production"][0]
  description    = "Defined entirely in Terraform"

  raw = {
    compose_file = <<-YAML
      services:
        web:
          image: nginx:alpine
          ports:
            - "8080:80"
    YAML
  }
}

# From a GitHub App repository. github_id comes from the data source rather
# than a hardcoded opaque id.
data "dokploy_github_provider" "main" {
  name = "my-org"
}

resource "dokploy_compose" "from_github" {
  name           = "github-stack"
  environment_id = [for e in dokploy_project.example.environments : e.id if e.name == "production"][0]

  github = {
    repository = "my-stack"
    owner      = "my-org"
    branch     = "main"
    github_id  = data.dokploy_github_provider.main.id
  }

  compose_path = "./deploy/docker-compose.yml"
  auto_deploy  = true
  trigger_type = "push"
  watch_paths  = ["deploy/**"]
}

# From a plain git remote, run as a Docker Swarm stack.
resource "dokploy_compose" "from_git" {
  name           = "git-stack"
  environment_id = [for e in dokploy_project.example.environments : e.id if e.name == "production"][0]
  compose_type   = "stack"

  git = {
    url    = "https://github.com/my-org/my-stack.git"
    branch = "main"
  }

  env = <<-ENV
    LOG_LEVEL=info
    REGION=eu-west-1
  ENV
}

# Route traffic to one service inside the stack.
resource "dokploy_domain" "web" {
  host         = "example.com"
  compose_id   = dokploy_compose.inline.id
  service_name = "web"
  port         = 80
  https        = true
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `environment_id` (String) Id of the environment this service lives in (see `dokploy_project.environments`).
- `name` (String) Display name of the compose service.

### Optional

- `app_name` (String) Dokploy-internal app name; generated by the server when omitted.
- `auto_deploy` (Boolean) Redeploy automatically when the source repository changes. The server sets `true` on a freshly created service; an explicit null is a real, storable state.
- `command` (String) Replaces the command Dokploy runs to deploy this stack (normally `docker compose up`). It is a substitute, not an addition: setting it to anything that does not itself deploy the stack makes every deploy fail. Leave it unset unless you specifically need to override the invocation.
- `compose_path` (String) Path to the compose file inside the repository. Defaults to `./docker-compose.yml`; it cannot be set to an empty string. Ignored by the `raw` source.
- `compose_type` (String) Whether Dokploy runs this as a plain `docker-compose` project or a Docker Swarm `stack`.
- `create_env_file` (Boolean) Write the environment variables to a `.env` file for the compose project. Defaults to the server's own default for a new service.
- `deploy_on_change` (Boolean) Deploy after create and after changes to deploy-triggering attributes. Defaults to `true`.
- `deployment_timeout` (String) How long to wait for a triggered deployment to reach a terminal status, as a Go duration string. Defaults to `"15m"`. On timeout the apply fails but the server-side deployment keeps running.
- `description` (String) Free-form description.
- `enable_submodules` (Boolean) Clone git submodules alongside the repository. Defaults to `false`.
- `env` (String) Extra environment variables in Dokploy's native multiline `KEY=value` format. Use Terraform sensitive variables for secret values. Omitting this attribute and setting it to "" are indistinguishable on read - both come back null. Use omission, not "", to clear it.
- `git` (Attributes) Source the compose file from a plain git remote. (see [below for nested schema](#nestedatt--git))
- `github` (Attributes) Source the compose file from a GitHub App repository. (see [below for nested schema](#nestedatt--github))
- `icon` (String) Service icon shown in the Dokploy UI (an icon name or data URI, up to 2 MB).
- `isolated_deployment` (Boolean, Deprecated) Run the stack in an isolated Docker network. Defaults to `false`. **Deprecated upstream since Dokploy v0.30.0** - prefer `service_networks`.
- `isolated_deployments_volume` (Boolean, Deprecated) Give the isolated deployment its own volume namespace. Defaults to `false`. **Deprecated upstream since Dokploy v0.30.0** - prefer `service_networks`.
- `randomize` (Boolean) Randomise generated resource names, using `suffix`. Defaults to `false`.
- `raw` (Attributes) Supply the compose file inline instead of fetching it from a repository. (see [below for nested schema](#nestedatt--raw))
- `server_id` (String) Remote server to run the service on. Defaults to the Dokploy host.
- `service_networks` (Attributes Set) Per-service Docker network attachments (Dokploy v0.30.0). Each entry names one compose service and the Dokploy network ids to attach. Applied on the next deploy. (see [below for nested schema](#nestedatt--service_networks))
- `suffix` (String) Suffix appended to generated resource names when `randomize` is set.
- `trigger_type` (String) Which git event triggers an auto-deploy.
- `watch_paths` (List of String) Only auto-deploy when a change touches one of these paths.

### Read-Only

- `created_at` (String) Creation timestamp (server-side).
- `id` (String) Compose service id.
- `status` (String) Service status reported by Dokploy.

<a id="nestedatt--git"></a>
### Nested Schema for `git`

Required:

- `branch` (String) Branch to deploy from.
- `url` (String) Git remote URL.

Optional:

- `ssh_key_id` (String) Id of a Dokploy SSH key for private repos.


<a id="nestedatt--github"></a>
### Nested Schema for `github`

Required:

- `branch` (String) Branch to deploy from.
- `github_id` (String) Id of the Dokploy GitHub App (see the `dokploy_github_provider` data source).
- `owner` (String) Repository owner.
- `repository` (String) Repository name.


<a id="nestedatt--raw"></a>
### Nested Schema for `raw`

Required:

- `compose_file` (String) The compose YAML, verbatim.


<a id="nestedatt--service_networks"></a>
### Nested Schema for `service_networks`

Required:

- `network_ids` (Set of String) Dokploy network ids to attach to this service.
- `service_name` (String) Compose service name, as written in the compose file.

Optional:

- `detach_dokploy_network` (Boolean) Detach the shared `dokploy-network` from this service. Defaults to `false`.

## Import

Import is supported using the following syntax:

The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example:

```shell
terraform import dokploy_compose.web V1StGXR8_Z5jdHi6B-myT
```